7 use SQL::Translator::Schema::Constants;
8 use Test::SQL::Translator qw(maybe_plan);
10 maybe_plan(85, '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) );
59 CREATE TABLE qtl_trait_synonym
61 qtl_trait_synonym_id NUMBER(11) NOT NULL
62 CONSTRAINT pk_qtl_trait_synonym PRIMARY KEY,
63 trait_synonym VARCHAR2(200) NOT NULL,
64 qtl_trait_id NUMBER(11) NOT NULL,
65 UNIQUE( qtl_trait_id, trait_synonym ),
66 FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait ON DELETE SET NULL
72 my $data = parse( $t, $sql );
73 my $schema = $t->schema;
75 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
76 my @tables = $schema->get_tables;
77 is( scalar @tables, 4, 'Found four tables' );
82 my $t1 = shift @tables;
83 is( $t1->name, 'qtl_trait_category', 'First table is "qtl_trait_category"' );
84 is( $t1->comments, 'hey, hey, hey, hey', 'Comment = "hey, hey, hey, hey"' );
86 my @t1_fields = $t1->get_fields;
87 is( scalar @t1_fields, 2, '2 fields in table' );
89 my $f1 = shift @t1_fields;
90 is( $f1->name, 'qtl_trait_category_id',
91 'First field is "qtl_trait_category_id"' );
92 is( $f1->data_type, 'number', 'Field is a number' );
93 is( $f1->size, 11, 'Size is "11"' );
94 is( $f1->is_nullable, 0, 'Field cannot be null' );
95 is( $f1->default_value, undef, 'Default value is undefined' );
96 is( $f1->is_primary_key, 1, 'Field is PK' );
97 is( join(',', $f1->comments), 'the primary key!', 'Comment = "the primary key!"' );
99 my $f2 = shift @t1_fields;
100 is( $f2->name, 'trait_category', 'Second field is "trait_category"' );
101 is( $f2->data_type, 'varchar2', 'Field is a varchar2' );
102 is( $f2->size, 100, 'Size is "100"' );
103 is( $f2->is_nullable, 0, 'Field cannot be null' );
104 is( $f2->default_value, undef, 'Default value is undefined' );
105 is( $f2->is_primary_key, 0, 'Field is not PK' );
107 my @t1_indices = $t1->get_indices;
108 is( scalar @t1_indices, 0, '0 indices on table' );
110 my @t1_constraints = $t1->get_constraints;
111 is( scalar @t1_constraints, 2, '2 constraints on table' );
113 my $c1 = $t1_constraints[0];
114 is( $c1->name, 'pk_qtl_trait_category',
115 'Constraint name is "pk_qtl_trait_category"' );
116 is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
117 is( join(',', $c1->fields), 'qtl_trait_category_id',
118 'Constraint is on field "qtl_trait_category_id"' );
120 my $c2 = $t1_constraints[1];
121 is( $c2->type, UNIQUE, 'Second constraint is unique' );
122 is( join(',', $c2->fields), 'trait_category',
123 'Constraint is on field "trait_category"' );
128 my $t2 = shift @tables;
129 is( $t2->name, 'qtl_trait', 'Table "qtl_trait" exists' );
130 is( $t2->comments, 'foo bar comment', 'Comment "foo bar" exists' );
132 my @t2_fields = $t2->get_fields;
133 is( scalar @t2_fields, 4, '4 fields in table' );
135 my $t2_f1 = shift @t2_fields;
136 is( $t2_f1->name, 'qtl_trait_id', 'First field is "qtl_trait_id"' );
137 is( $t2_f1->data_type, 'number', 'Field is a number' );
138 is( $t2_f1->size, 11, 'Size is "11"' );
139 is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
140 is( $t2_f1->default_value, undef, 'Default value is undefined' );
141 is( $t2_f1->is_primary_key, 1, 'Field is PK' );
143 my $t2_f2 = shift @t2_fields;
144 is( $t2_f2->name, 'trait_symbol', 'Second field is "trait_symbol"' );
145 is( $t2_f2->data_type, 'varchar2', 'Field is a varchar2' );
146 is( $t2_f2->size, 100, 'Size is "100"' );
147 is( $t2_f2->is_nullable, 0, 'Field cannot be null' );
148 is( $t2_f2->is_foreign_key, 0, 'Field is not a FK' );
150 my $t2_f3 = shift @t2_fields;
151 is( $t2_f3->name, 'trait_name', 'Third field is "trait_name"' );
152 is( $t2_f3->data_type, 'varchar2', 'Field is a varchar2' );
153 is( $t2_f3->size, 200, 'Size is "200"' );
154 is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
155 is( $t2_f3->is_foreign_key, 0, 'Field is not a FK' );
157 my $t2_f4 = shift @t2_fields;
158 is( $t2_f4->name, 'qtl_trait_category_id',
159 'Fourth field is "qtl_trait_category_id"' );
160 is( $t2_f4->data_type, 'number', 'Field is a number' );
161 is( $t2_f4->size, 11, 'Size is "11"' );
162 is( $t2_f4->is_nullable, 0, 'Field cannot be null' );
163 is( $t2_f4->is_foreign_key, 1, 'Field is a FK' );
164 my $f4_fk = $t2_f4->foreign_key_reference;
165 isa_ok( $f4_fk, 'SQL::Translator::Schema::Constraint', 'FK' );
166 is( $f4_fk->reference_table, 'qtl_trait_category',
167 'FK references table "qtl_trait_category"' );
168 is( join(',', $f4_fk->reference_fields), 'qtl_trait_category_id',
169 'FK references field "qtl_trait_category_id"' );
171 my @t2_constraints = $t2->get_constraints;
172 is( scalar @t2_constraints, 4, '4 constraints on table' );
174 my $t2_c1 = shift @t2_constraints;
175 is( $t2_c1->type, PRIMARY_KEY, 'First constraint is PK' );
176 is( $t2_c1->name, 'pk_qtl_trait', 'Name is "pk_qtl_trait"' );
177 is( join(',', $t2_c1->fields), 'qtl_trait_id', 'Fields = "qtl_trait_id"' );
179 my $t2_c2 = shift @t2_constraints;
180 is( $t2_c2->type, UNIQUE, 'Second constraint is unique' );
181 is( $t2_c2->name, '', 'No name' );
182 is( join(',', $t2_c2->fields), 'trait_symbol', 'Fields = "trait_symbol"' );
184 my $t2_c3 = shift @t2_constraints;
185 is( $t2_c3->type, UNIQUE, 'Third constraint is unique' );
186 is( $t2_c3->name, '', 'No name' );
187 is( join(',', $t2_c3->fields), 'trait_name', 'Fields = "trait_name"' );
189 my $t2_c4 = shift @t2_constraints;
190 is( $t2_c4->type, FOREIGN_KEY, 'Fourth constraint is FK' );
191 is( $t2_c4->name, '', 'No name' );
192 is( join(',', $t2_c4->fields), 'qtl_trait_category_id',
193 'Fields = "qtl_trait_category_id"' );
194 is( $t2_c4->reference_table, 'qtl_trait_category',
195 'Reference table = "qtl_trait_category"' );
196 is( join(',', $t2_c4->reference_fields), 'qtl_trait_category_id',
197 'Reference fields = "qtl_trait_category_id"' );
203 my $t3 = shift @tables;
204 is( $t3->name, 'qtl', 'Table "qtl" exists' );
206 my @t3_fields = $t3->get_fields;
207 is( scalar @t3_fields, 8, '8 fields in table' );
209 my @t3_constraints = $t3->get_constraints;
210 is( scalar @t3_constraints, 4, '4 constraints on table' );
211 my $t3_c4 = $t3_constraints[3];
212 is( $t3_c4->type, UNIQUE, 'Fourth constraint is unique' );
213 is( $t3_c4->name, 'qtl_accession_upper', 'Name = "qtl_accession_upper"' );
214 is( join(',', $t3_c4->fields), 'UPPER(qtl_accession_id)', 'Fields = "UPPER(qtl_accession_id)"' );
216 is( $t3->comments, 'qtl table comment', 'Comment "qtl table comment" exists' );
218 my $t3_f1 = shift @t3_fields;
219 is( $t3_f1->comments, 'qtl_id comment', 'Comment "qtl_id comment" exists' );
221 my $t3_f2 = shift @t3_fields;
222 is( $t3_f2->comments, 'accession comment',
223 'Comment "accession comment" exists' );
228 my $t4 = shift @tables;
229 is( $t4->name, 'qtl_trait_synonym', 'Table "qtl_trait_synonym" exists' );
231 my @t4_fields = $t4->get_fields;
232 is( scalar @t4_fields, 3, '3 fields in table' );
234 my @t4_constraints = $t4->get_constraints;
235 is( scalar @t4_constraints, 3, '3 constraints on table' );
236 my $t4_c3 = $t4_constraints[2];
237 is( $t4_c3->type, FOREIGN_KEY, 'Third constraint is FK' );
238 is( $t4_c3->name, '', 'No name' );
239 is( join(',', $t4_c3->fields), 'qtl_trait_id',
240 'Fields = "qtl_trait_id"' );
241 is( $t4_c3->reference_table, 'qtl_trait',
242 'Reference table = "qtl_trait"' );
243 is( join(',', $t4_c3->reference_fields), 'qtl_trait_id',
244 'Reference fields = "qtl_trait_id"' );
245 is( $t4_c3->on_delete, 'SET NULL',
246 'on_delete = "SET NULL"' );