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