7 use SQL::Translator::Schema::Constants;
8 use Test::SQL::Translator qw(maybe_plan);
10 maybe_plan(89, 'SQL::Translator::Parser::Oracle');
11 SQL::Translator::Parser::Oracle->import('parse');
13 my $t = SQL::Translator->new( trace => 0 );
15 CREATE TABLE qtl_trait_category
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 )
22 COMMENT ON TABLE qtl_trait_category IS
24 comment on column qtl_trait_category.qtl_trait_category_id
25 is 'the primary key!';
28 CREATE TABLE qtl_trait
30 qtl_trait_id NUMBER(11) NOT NULL
31 CONSTRAINT pk_qtl_trait PRIMARY KEY,
32 trait_symbol VARCHAR2(100 BYTE) NOT NULL,
33 trait_name VARCHAR2(200 CHAR) NOT NULL,
34 qtl_trait_category_id NUMBER(11) NOT NULL,
35 UNIQUE ( trait_symbol ),
36 UNIQUE ( trait_name ),
37 FOREIGN KEY ( qtl_trait_category_id ) REFERENCES qtl_trait_category
40 /* qtl table comment */
44 qtl_id NUMBER(11) NOT NULL
45 CONSTRAINT pk_qtl PRIMARY KEY,
46 qtl_accession_id VARCHAR2(20) NOT NULL /* accession comment */,
47 published_symbol VARCHAR2(100),
48 qtl_trait_id NUMBER(11) NOT NULL,
49 linkage_group VARCHAR2(32) NOT NULL,
50 start_position NUMBER(11,2) NOT NULL,
51 stop_position NUMBER(11,2) NOT NULL,
53 FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait
56 CREATE UNIQUE INDEX qtl_accession ON qtl ( qtl_accession_id );
57 CREATE UNIQUE INDEX qtl_accession_upper ON qtl ( UPPER(qtl_accession_id) );
58 CREATE INDEX qtl_index ON qtl ( qtl_accession_id );
60 CREATE TABLE qtl_trait_synonym
62 qtl_trait_synonym_id NUMBER(11) NOT NULL
63 CONSTRAINT pk_qtl_trait_synonym PRIMARY KEY,
64 trait_synonym VARCHAR2(200) NOT NULL,
65 qtl_trait_id NUMBER(11) NOT NULL,
66 UNIQUE( qtl_trait_id, trait_synonym ),
67 FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait ON DELETE SET NULL
73 my $data = parse( $t, $sql );
74 my $schema = $t->schema;
76 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
77 my @tables = $schema->get_tables;
78 is( scalar @tables, 4, 'Found four tables' );
83 my $t1 = shift @tables;
84 is( $t1->name, 'qtl_trait_category', 'First table is "qtl_trait_category"' );
85 is( $t1->comments, 'hey, hey, hey, hey', 'Comment = "hey, hey, hey, hey"' );
87 my @t1_fields = $t1->get_fields;
88 is( scalar @t1_fields, 2, '2 fields in table' );
90 my $f1 = shift @t1_fields;
91 is( $f1->name, 'qtl_trait_category_id',
92 'First field is "qtl_trait_category_id"' );
93 is( $f1->data_type, 'number', 'Field is a number' );
94 is( $f1->size, 11, 'Size is "11"' );
95 is( $f1->is_nullable, 0, 'Field cannot be null' );
96 is( $f1->default_value, undef, 'Default value is undefined' );
97 is( $f1->is_primary_key, 1, 'Field is PK' );
98 is( join(',', $f1->comments), 'the primary key!', 'Comment = "the primary key!"' );
100 my $f2 = shift @t1_fields;
101 is( $f2->name, 'trait_category', 'Second field is "trait_category"' );
102 is( $f2->data_type, 'varchar2', 'Field is a varchar2' );
103 is( $f2->size, 100, 'Size is "100"' );
104 is( $f2->is_nullable, 0, 'Field cannot be null' );
105 is( $f2->default_value, undef, 'Default value is undefined' );
106 is( $f2->is_primary_key, 0, 'Field is not PK' );
108 my @t1_indices = $t1->get_indices;
109 is( scalar @t1_indices, 0, '0 indices on table' );
111 my @t1_constraints = $t1->get_constraints;
112 is( scalar @t1_constraints, 2, '2 constraints on table' );
114 my $c1 = $t1_constraints[0];
115 is( $c1->name, 'pk_qtl_trait_category',
116 'Constraint name is "pk_qtl_trait_category"' );
117 is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
118 is( join(',', $c1->fields), 'qtl_trait_category_id',
119 'Constraint is on field "qtl_trait_category_id"' );
121 my $c2 = $t1_constraints[1];
122 is( $c2->type, UNIQUE, 'Second constraint is unique' );
123 is( join(',', $c2->fields), 'trait_category',
124 'Constraint is on field "trait_category"' );
129 my $t2 = shift @tables;
130 is( $t2->name, 'qtl_trait', 'Table "qtl_trait" exists' );
131 is( $t2->comments, 'foo bar comment', 'Comment "foo bar" exists' );
133 my @t2_fields = $t2->get_fields;
134 is( scalar @t2_fields, 4, '4 fields in table' );
136 my $t2_f1 = shift @t2_fields;
137 is( $t2_f1->name, 'qtl_trait_id', 'First field is "qtl_trait_id"' );
138 is( $t2_f1->data_type, 'number', 'Field is a number' );
139 is( $t2_f1->size, 11, 'Size is "11"' );
140 is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
141 is( $t2_f1->default_value, undef, 'Default value is undefined' );
142 is( $t2_f1->is_primary_key, 1, 'Field is PK' );
144 my $t2_f2 = shift @t2_fields;
145 is( $t2_f2->name, 'trait_symbol', 'Second field is "trait_symbol"' );
146 is( $t2_f2->data_type, 'varchar2', 'Field is a varchar2' );
147 is( $t2_f2->size, 100, 'Size is "100"' );
148 is( $t2_f2->is_nullable, 0, 'Field cannot be null' );
149 is( $t2_f2->is_foreign_key, 0, 'Field is not a FK' );
151 my $t2_f3 = shift @t2_fields;
152 is( $t2_f3->name, 'trait_name', 'Third field is "trait_name"' );
153 is( $t2_f3->data_type, 'varchar2', 'Field is a varchar2' );
154 is( $t2_f3->size, 200, 'Size is "200"' );
155 is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
156 is( $t2_f3->is_foreign_key, 0, 'Field is not a FK' );
158 my $t2_f4 = shift @t2_fields;
159 is( $t2_f4->name, 'qtl_trait_category_id',
160 'Fourth field is "qtl_trait_category_id"' );
161 is( $t2_f4->data_type, 'number', 'Field is a number' );
162 is( $t2_f4->size, 11, 'Size is "11"' );
163 is( $t2_f4->is_nullable, 0, 'Field cannot be null' );
164 is( $t2_f4->is_foreign_key, 1, 'Field is a FK' );
165 my $f4_fk = $t2_f4->foreign_key_reference;
166 isa_ok( $f4_fk, 'SQL::Translator::Schema::Constraint', 'FK' );
167 is( $f4_fk->reference_table, 'qtl_trait_category',
168 'FK references table "qtl_trait_category"' );
169 is( join(',', $f4_fk->reference_fields), 'qtl_trait_category_id',
170 'FK references field "qtl_trait_category_id"' );
172 my @t2_constraints = $t2->get_constraints;
173 is( scalar @t2_constraints, 4, '4 constraints on table' );
175 my $t2_c1 = shift @t2_constraints;
176 is( $t2_c1->type, PRIMARY_KEY, 'First constraint is PK' );
177 is( $t2_c1->name, 'pk_qtl_trait', 'Name is "pk_qtl_trait"' );
178 is( join(',', $t2_c1->fields), 'qtl_trait_id', 'Fields = "qtl_trait_id"' );
180 my $t2_c2 = shift @t2_constraints;
181 is( $t2_c2->type, UNIQUE, 'Second constraint is unique' );
182 is( $t2_c2->name, '', 'No name' );
183 is( join(',', $t2_c2->fields), 'trait_symbol', 'Fields = "trait_symbol"' );
185 my $t2_c3 = shift @t2_constraints;
186 is( $t2_c3->type, UNIQUE, 'Third constraint is unique' );
187 is( $t2_c3->name, '', 'No name' );
188 is( join(',', $t2_c3->fields), 'trait_name', 'Fields = "trait_name"' );
190 my $t2_c4 = shift @t2_constraints;
191 is( $t2_c4->type, FOREIGN_KEY, 'Fourth constraint is FK' );
192 is( $t2_c4->name, '', 'No name' );
193 is( join(',', $t2_c4->fields), 'qtl_trait_category_id',
194 'Fields = "qtl_trait_category_id"' );
195 is( $t2_c4->reference_table, 'qtl_trait_category',
196 'Reference table = "qtl_trait_category"' );
197 is( join(',', $t2_c4->reference_fields), 'qtl_trait_category_id',
198 'Reference fields = "qtl_trait_category_id"' );
204 my $t3 = shift @tables;
205 is( $t3->name, 'qtl', 'Table "qtl" exists' );
207 my @t3_fields = $t3->get_fields;
208 is( scalar @t3_fields, 8, '8 fields in table' );
210 my @t3_constraints = $t3->get_constraints;
211 is( scalar @t3_constraints, 4, '4 constraints on table' );
212 my $t3_c4 = $t3_constraints[3];
213 is( $t3_c4->type, UNIQUE, 'Fourth constraint is unique' );
214 is( $t3_c4->name, 'qtl_accession_upper', 'Name = "qtl_accession_upper"' );
215 is( join(',', $t3_c4->fields), 'UPPER(qtl_accession_id)', 'Fields = "UPPER(qtl_accession_id)"' );
217 is( $t3->comments, 'qtl table comment', 'Comment "qtl table comment" exists' );
219 my $t3_f1 = shift @t3_fields;
220 is( $t3_f1->comments, 'qtl_id comment', 'Comment "qtl_id comment" exists' );
222 my $t3_f2 = shift @t3_fields;
223 is( $t3_f2->comments, 'accession comment',
224 'Comment "accession comment" exists' );
226 my @t3_indices = $t3->get_indices;
227 is( scalar @t3_indices, 1, '1 index on table' );
229 my $t3_i1 = shift @t3_indices;
230 is( $t3_i1->type, 'NORMAL', 'First index is normal' );
231 is( $t3_i1->name, 'qtl_index', 'Name is "qtl_index"' );
232 is( join(',', $t3_i1->fields), 'qtl_accession_id', 'Fields = "qtl_accession_id"' );
237 my $t4 = shift @tables;
238 is( $t4->name, 'qtl_trait_synonym', 'Table "qtl_trait_synonym" exists' );
240 my @t4_fields = $t4->get_fields;
241 is( scalar @t4_fields, 3, '3 fields in table' );
243 my @t4_constraints = $t4->get_constraints;
244 is( scalar @t4_constraints, 3, '3 constraints on table' );
245 my $t4_c3 = $t4_constraints[2];
246 is( $t4_c3->type, FOREIGN_KEY, 'Third constraint is FK' );
247 is( $t4_c3->name, '', 'No name' );
248 is( join(',', $t4_c3->fields), 'qtl_trait_id',
249 'Fields = "qtl_trait_id"' );
250 is( $t4_c3->reference_table, 'qtl_trait',
251 'Reference table = "qtl_trait"' );
252 is( join(',', $t4_c3->reference_fields), 'qtl_trait_id',
253 'Reference fields = "qtl_trait_id"' );
254 is( $t4_c3->on_delete, 'SET NULL',
255 'on_delete = "SET NULL"' );