Update tests to use maybe_plan.
[dbsrgits/SQL-Translator.git] / t / 15oracle-parser.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 use strict;
5 use Test::More;
6 use SQL::Translator;
7 use SQL::Translator::Schema::Constants;
8 use Test::SQL::Translator qw(maybe_plan);
9
10 maybe_plan(76, 'SQL::Translator::Parser::Oracle');
11 SQL::Translator::Parser::Oracle->import('parse');
12
13 my $t   = SQL::Translator->new( trace => 0 );
14 my $sql = q[
15     CREATE TABLE qtl_trait_category
16     (
17         qtl_trait_category_id       NUMBER(11)      NOT NULL    
18             CONSTRAINT pk_qtl_trait_category PRIMARY KEY,
19         trait_category              VARCHAR2(100)   NOT NULL,
20         UNIQUE ( trait_category )
21     );
22     comment on table qtl_trait_category is 'hey, hey, hey, hey';
23     comment on column qtl_trait_category.qtl_trait_category_id 
24         is 'the primary key!';
25
26     -- foo bar comment
27     CREATE TABLE qtl_trait
28     (
29         qtl_trait_id            NUMBER(11)      NOT NULL    
30             CONSTRAINT pk_qtl_trait PRIMARY KEY,
31         trait_symbol            VARCHAR2(100)   NOT NULL,
32         trait_name              VARCHAR2(200)   NOT NULL,
33         qtl_trait_category_id   NUMBER(11)      NOT NULL,
34         UNIQUE ( trait_symbol ),
35         UNIQUE ( trait_name ),
36         FOREIGN KEY ( qtl_trait_category_id ) REFERENCES qtl_trait_category
37     );
38
39     /* qtl table comment */
40     CREATE TABLE qtl
41     (
42         /* qtl_id comment */
43         qtl_id              NUMBER(11)      NOT NULL    
44             CONSTRAINT pk_qtl PRIMARY KEY,
45         qtl_accession_id    VARCHAR2(20)    NOT NULL /* accession comment */,
46         published_symbol    VARCHAR2(100),
47         qtl_trait_id        NUMBER(11)      NOT NULL,
48         linkage_group       VARCHAR2(32)    NOT NULL,
49         start_position      NUMBER(11,2)    NOT NULL,
50         stop_position       NUMBER(11,2)    NOT NULL,
51         comments            long,
52         UNIQUE ( qtl_accession_id ),
53         FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait
54     );
55
56     CREATE TABLE qtl_trait_synonym
57     (
58         qtl_trait_synonym_id    NUMBER(11)      NOT NULL    
59             CONSTRAINT pk_qtl_trait_synonym PRIMARY KEY,
60         trait_synonym           VARCHAR2(200)   NOT NULL,
61         qtl_trait_id            NUMBER(11)      NOT NULL,
62         UNIQUE( qtl_trait_id, trait_synonym ),
63         FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait
64     );
65 ];
66
67 $| = 1;
68
69 my $data   = parse( $t, $sql );
70 my $schema = $t->schema;
71
72 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
73 my @tables = $schema->get_tables;
74 is( scalar @tables, 4, 'Found four tables' );
75
76 #
77 # qtl_trait_category
78 #
79 my $t1 = shift @tables;
80 is( $t1->name, 'qtl_trait_category', 'First table is "qtl_trait_category"' );
81 is( $t1->comments, 'hey, hey, hey, hey', 'Comment = "hey, hey, hey, hey"' );
82
83 my @t1_fields = $t1->get_fields;
84 is( scalar @t1_fields, 2, '2 fields in table' );
85
86 my $f1 = shift @t1_fields;
87 is( $f1->name, 'qtl_trait_category_id', 
88     'First field is "qtl_trait_category_id"' );
89 is( $f1->data_type, 'number', 'Field is a number' );
90 is( $f1->size, 11, 'Size is "11"' );
91 is( $f1->is_nullable, 0, 'Field cannot be null' );
92 is( $f1->default_value, undef, 'Default value is undefined' );
93 is( $f1->is_primary_key, 1, 'Field is PK' );
94 is( join(',', $f1->comments), 'the primary key!', 'Comment = "the primary key!"' );
95
96 my $f2 = shift @t1_fields;
97 is( $f2->name, 'trait_category', 'Second field is "trait_category"' );
98 is( $f2->data_type, 'varchar2', 'Field is a varchar2' );
99 is( $f2->size, 100, 'Size is "100"' );
100 is( $f2->is_nullable, 0, 'Field cannot be null' );
101 is( $f2->default_value, undef, 'Default value is undefined' );
102 is( $f2->is_primary_key, 0, 'Field is not PK' );
103
104 my @t1_indices = $t1->get_indices;
105 is( scalar @t1_indices, 0, '0 indices on table' );
106
107 my @t1_constraints = $t1->get_constraints;
108 is( scalar @t1_constraints, 2, '2 constraints on table' );
109
110 my $c1 = $t1_constraints[0];
111 is( $c1->name, 'pk_qtl_trait_category', 
112     'Constraint name is "pk_qtl_trait_category"' );
113 is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
114 is( join(',', $c1->fields), 'qtl_trait_category_id', 
115     'Constraint is on field "qtl_trait_category_id"' );
116
117 my $c2 = $t1_constraints[1];
118 is( $c2->type, UNIQUE, 'Second constraint is unique' );
119 is( join(',', $c2->fields), 'trait_category', 
120     'Constraint is on field "trait_category"' );
121
122 #
123 # qtl_trait
124 #
125 my $t2 = shift @tables;
126 is( $t2->name, 'qtl_trait', 'Table "qtl_trait" exists' );
127 is( $t2->comments, 'foo bar comment', 'Comment "foo bar" exists' );
128
129 my @t2_fields = $t2->get_fields;
130 is( scalar @t2_fields, 4, '4 fields in table' );
131
132 my $t2_f1 = shift @t2_fields;
133 is( $t2_f1->name, 'qtl_trait_id', 'First field is "qtl_trait_id"' );
134 is( $t2_f1->data_type, 'number', 'Field is a number' );
135 is( $t2_f1->size, 11, 'Size is "11"' );
136 is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
137 is( $t2_f1->default_value, undef, 'Default value is undefined' );
138 is( $t2_f1->is_primary_key, 1, 'Field is PK' );
139
140 my $t2_f2 = shift @t2_fields;
141 is( $t2_f2->name, 'trait_symbol', 'Second field is "trait_symbol"' );
142 is( $t2_f2->data_type, 'varchar2', 'Field is a varchar2' );
143 is( $t2_f2->size, 100, 'Size is "100"' );
144 is( $t2_f2->is_nullable, 0, 'Field cannot be null' );
145 is( $t2_f2->is_foreign_key, 0, 'Field is not a FK' );
146
147 my $t2_f3 = shift @t2_fields;
148 is( $t2_f3->name, 'trait_name', 'Third field is "trait_name"' );
149 is( $t2_f3->data_type, 'varchar2', 'Field is a varchar2' );
150 is( $t2_f3->size, 200, 'Size is "200"' );
151 is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
152 is( $t2_f3->is_foreign_key, 0, 'Field is not a FK' );
153
154 my $t2_f4 = shift @t2_fields;
155 is( $t2_f4->name, 'qtl_trait_category_id', 
156     'Fourth field is "qtl_trait_category_id"' );
157 is( $t2_f4->data_type, 'number', 'Field is a number' );
158 is( $t2_f4->size, 11, 'Size is "11"' );
159 is( $t2_f4->is_nullable, 0, 'Field cannot be null' );
160 is( $t2_f4->is_foreign_key, 1, 'Field is a FK' );
161 my $f4_fk = $t2_f4->foreign_key_reference;
162 isa_ok( $f4_fk, 'SQL::Translator::Schema::Constraint', 'FK' );
163 is( $f4_fk->reference_table, 'qtl_trait_category', 
164     'FK references table "qtl_trait_category"' );
165 is( join(',', $f4_fk->reference_fields), 'qtl_trait_category_id', 
166     'FK references field "qtl_trait_category_id"' );
167
168 my @t2_constraints = $t2->get_constraints;
169 is( scalar @t2_constraints, 4, '4 constraints on table' );
170
171 my $t2_c1 = shift @t2_constraints;
172 is( $t2_c1->type, PRIMARY_KEY, 'First constraint is PK' );
173 is( $t2_c1->name, 'pk_qtl_trait', 'Name is "pk_qtl_trait"' );
174 is( join(',', $t2_c1->fields), 'qtl_trait_id', 'Fields = "qtl_trait_id"' );
175
176 my $t2_c2 = shift @t2_constraints;
177 is( $t2_c2->type, UNIQUE, 'Second constraint is unique' );
178 is( $t2_c2->name, '', 'No name' );
179 is( join(',', $t2_c2->fields), 'trait_symbol', 'Fields = "trait_symbol"' );
180
181 my $t2_c3 = shift @t2_constraints;
182 is( $t2_c3->type, UNIQUE, 'Third constraint is unique' );
183 is( $t2_c3->name, '', 'No name' );
184 is( join(',', $t2_c3->fields), 'trait_name', 'Fields = "trait_name"' );
185
186 my $t2_c4 = shift @t2_constraints;
187 is( $t2_c4->type, FOREIGN_KEY, 'Fourth constraint is FK' );
188 is( $t2_c4->name, '', 'No name' );
189 is( join(',', $t2_c4->fields), 'qtl_trait_category_id', 
190     'Fields = "qtl_trait_category_id"' );
191 is( $t2_c4->reference_table, 'qtl_trait_category', 
192     'Reference table = "qtl_trait_category"' );
193 is( join(',', $t2_c4->reference_fields), 'qtl_trait_category_id', 
194     'Reference fields = "qtl_trait_category_id"' );
195
196
197 #
198 # qtl
199 #
200 my $t3 = shift @tables;
201 is( $t3->name, 'qtl', 'Table "qtl" exists' );
202
203 my @t3_fields = $t3->get_fields;
204 is( scalar @t3_fields, 8, '8 fields in table' );
205
206 my @t3_constraints = $t3->get_constraints;
207 is( scalar @t3_constraints, 3, '3 constraints on table' );
208
209 is( $t3->comments, 'qtl table comment', 'Comment "qtl table comment" exists' );
210
211 my $t3_f1     = shift @t3_fields;
212 is( $t3_f1->comments, 'qtl_id comment', 'Comment "qtl_id comment" exists' );
213
214 my $t3_f2     = shift @t3_fields;
215 is( $t3_f2->comments, 'accession comment', 
216     'Comment "accession comment" exists' );
217
218 #
219 # qtl_trait_synonym
220 #
221 my $t4 = shift @tables;
222 is( $t4->name, 'qtl_trait_synonym', 'Table "qtl_trait_synonym" exists' );
223
224 my @t4_fields = $t4->get_fields;
225 is( scalar @t4_fields, 3, '3 fields in table' );
226
227 my @t4_constraints = $t4->get_constraints;
228 is( scalar @t4_constraints, 3, '3 constraints on table' );