Commit | Line | Data |
4d878d2f |
1 | #!/usr/bin/perl |
5dada97b |
2 | # vim:set ft=perl: |
4d878d2f |
3 | |
586fba3f |
4 | $| = 1; |
5 | |
4d878d2f |
6 | use strict; |
adfdb552 |
7 | use Test::More tests => 150; |
07680493 |
8 | use SQL::Translator::Schema::Constants; |
4d878d2f |
9 | |
10 | require_ok( 'SQL::Translator::Schema' ); |
11 | |
07680493 |
12 | { |
13 | # |
14 | # Schema |
15 | # |
16 | my $schema = SQL::Translator::Schema->new( |
17 | name => 'foo', |
18 | database => 'MySQL', |
19 | ); |
20 | isa_ok( $schema, 'SQL::Translator::Schema' ); |
4d878d2f |
21 | |
07680493 |
22 | is( $schema->name, 'foo', 'Schema name is "foo"' ); |
23 | is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' ); |
4d878d2f |
24 | |
07680493 |
25 | is( $schema->database, 'MySQL', 'Schema database is "MySQL"' ); |
26 | is( $schema->database('PostgreSQL'), 'PostgreSQL', |
27 | 'Schema database changed to "PostgreSQL"' ); |
4d878d2f |
28 | |
07680493 |
29 | is( $schema->is_valid, undef, 'Schema not valid...' ); |
30 | like( $schema->error, qr/no tables/i, '...because there are no tables' ); |
4d878d2f |
31 | |
07680493 |
32 | # |
33 | # $schema->add_* |
34 | # |
35 | my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error; |
36 | isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Table "foo"' ); |
4d878d2f |
37 | |
07680493 |
38 | my $bar_table = SQL::Translator::Schema::Table->new( name => 'bar' ) or |
39 | warn SQL::Translator::Schema::Table->error; |
40 | $bar_table = $schema->add_table( $bar_table ); |
41 | isa_ok( $bar_table, 'SQL::Translator::Schema::Table', 'Table "bar"' ); |
42 | is( $bar_table->name, 'bar', 'Add table "bar"' ); |
4d878d2f |
43 | |
07680493 |
44 | $schema = $bar_table->schema( $schema ); |
45 | isa_ok( $schema, 'SQL::Translator::Schema', 'Schema' ); |
4d878d2f |
46 | |
07680493 |
47 | is( $bar_table->name('foo'), undef, |
48 | q[Can't change name of table "bar" to "foo"...]); |
49 | like( $bar_table->error, qr/can't use table name/i, |
50 | q[...because "foo" exists] ); |
4d878d2f |
51 | |
07680493 |
52 | my $redundant_table = $schema->add_table(name => 'foo'); |
53 | is( $redundant_table, undef, qq[Can't create another "foo" table...] ); |
54 | like( $schema->error, qr/can't use table name/i, '... because "foo" exists' ); |
55 | |
56 | # |
57 | # Table default new |
58 | # |
59 | is( $foo_table->name, 'foo', 'Table name is "foo"' ); |
60 | is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' ); |
61 | |
62 | my $fields = $foo_table->get_fields; |
63 | is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' ); |
64 | like( $foo_table->error, qr/no fields/i, 'Error for no fields' ); |
65 | |
66 | # |
67 | # New table with args |
68 | # |
69 | my $person_table = $schema->add_table( name => 'person' ); |
70 | is( $person_table->name, 'person', 'Table name is "person"' ); |
71 | is( $person_table->is_valid, undef, 'Table is not yet valid' ); |
72 | |
73 | # |
74 | # Field default new |
75 | # |
76 | my $f1 = $person_table->add_field(name => 'foo') or |
77 | warn $person_table->error; |
78 | isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' ); |
79 | is( $f1->name, 'foo', 'Field name is "foo"' ); |
80 | is( $f1->data_type, '', 'Field data type is blank' ); |
81 | is( $f1->size, 0, 'Field size is "0"' ); |
82 | is( $f1->is_primary_key, '0', 'Field is_primary_key is false' ); |
14f8758f |
83 | is( $f1->is_nullable, 1, 'Field can be NULL' ); |
07680493 |
84 | is( $f1->default_value, undef, 'Field default is undefined' ); |
85 | |
86 | my $f2 = SQL::Translator::Schema::Field->new (name => 'f2') or |
87 | warn SQL::Translator::Schema::Field->error; |
88 | $f2 = $person_table->add_field( $f2 ); |
89 | isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' ); |
90 | is( $f2->name, 'f2', 'Add field "f2"' ); |
14f8758f |
91 | is( $f2->is_nullable(0), 0, 'Field cannot be NULL' ); |
92 | is( $f2->is_nullable(''), 0, 'Field cannot be NULL' ); |
93 | is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' ); |
07680493 |
94 | is( $f1->default_value(''), '', 'Field default is empty string' ); |
95 | |
96 | $person_table = $f2->table( $person_table ); |
97 | isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' ); |
98 | |
99 | is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] ); |
100 | like( $f2->error, qr/can't use field name/i, '...because name exists' ); |
101 | |
102 | my $redundant_field = $person_table->add_field(name => 'f2'); |
103 | is( $redundant_field, undef, qq[Didn't create another "f2" field...] ); |
14f8758f |
104 | like( $person_table->error, qr/can't use field/i, |
07680493 |
105 | '... because it exists' ); |
106 | |
14f8758f |
107 | my @fields = $person_table->get_fields; |
108 | is( scalar @fields, 2, 'Table "foo" has 2 fields' ); |
109 | |
110 | is( $fields[0]->name, 'foo', 'First field is "foo"' ); |
111 | is( $fields[1]->name, 'f2', 'Second field is "f2"' ); |
112 | |
07680493 |
113 | # |
114 | # Field methods |
115 | # |
116 | is( $f1->name('person_name'), 'person_name', |
117 | 'Field name is "person_name"' ); |
118 | is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' ); |
119 | is( $f1->size('30'), '30', 'Field size is "30"' ); |
120 | is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' ); |
121 | |
adfdb552 |
122 | $f1->extra( foo => 'bar' ); |
123 | $f1->extra( { baz => 'quux' } ); |
124 | my %extra = $f1->extra; |
125 | is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' ); |
126 | is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' ); |
127 | |
07680493 |
128 | # |
129 | # New field with args |
130 | # |
131 | my $age = $person_table->add_field( |
132 | name => 'age', |
133 | data_type => 'float', |
134 | size => '10,2', |
135 | ); |
136 | is( $age->name, 'age', 'Field name is "age"' ); |
137 | is( $age->data_type, 'float', 'Field data type is "float"' ); |
138 | is( $age->size, '10,2', 'Field size is "10,2"' ); |
139 | is( $age->size(10,2), '10,2', 'Field size still "10,2"' ); |
140 | is( $age->size([10,2]), '10,2', 'Field size still "10,2"' ); |
141 | is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' ); |
14f8758f |
142 | is( join(':', $age->size), '10:2', 'Field size returns array' ); |
07680493 |
143 | |
144 | # |
145 | # Index |
146 | # |
147 | my @indices = $person_table->get_indices; |
148 | is( scalar @indices, 0, 'No indices' ); |
149 | like( $person_table->error, qr/no indices/i, 'Error for no indices' ); |
150 | my $index1 = $person_table->add_index( name => "foo" ) |
151 | or warn $person_table->error; |
152 | isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' ); |
153 | is( $index1->name, 'foo', 'Index name is "foo"' ); |
154 | |
14f8758f |
155 | is( $index1->is_valid, undef, 'Index name is not valid...' ); |
156 | like( $index1->error, qr/no fields/i, '...because it has no fields' ); |
157 | |
158 | is( join(':', $index1->fields('foo,bar')), 'foo:bar', |
159 | 'Index accepts fields'); |
160 | |
161 | is( $index1->is_valid, undef, 'Index name is not valid...' ); |
162 | like( $index1->error, qr/does not exist in table/i, |
163 | '...because it used fields not in the table' ); |
164 | |
165 | is( join(':', $index1->fields(qw[foo age])), 'foo:age', |
166 | 'Index accepts fields'); |
167 | is( $index1->is_valid, 1, 'Index name is now valid' ); |
168 | |
169 | is( $index1->type, NORMAL, 'Index type is "normal"' ); |
170 | |
07680493 |
171 | my $index2 = SQL::Translator::Schema::Index->new( name => "bar" ) |
172 | or warn SQL::Translator::Schema::Index->error; |
173 | $index2 = $person_table->add_index( $index2 ); |
174 | isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' ); |
175 | is( $index2->name, 'bar', 'Index name is "bar"' ); |
176 | |
177 | my $indices = $person_table->get_indices; |
178 | is( scalar @$indices, 2, 'Two indices' ); |
179 | is( $indices->[0]->name, 'foo', '"foo" index' ); |
180 | is( $indices->[1]->name, 'bar', '"bar" index' ); |
181 | |
182 | # |
183 | # Constraint |
184 | # |
185 | my @constraints = $person_table->get_constraints; |
186 | is( scalar @constraints, 0, 'No constraints' ); |
187 | like( $person_table->error, qr/no constraints/i, |
188 | 'Error for no constraints' ); |
189 | my $constraint1 = $person_table->add_constraint( name => 'foo' ) |
190 | or warn $person_table->error; |
191 | isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' ); |
192 | is( $constraint1->name, 'foo', 'Constraint name is "foo"' ); |
193 | |
194 | $fields = join(',', $constraint1->fields('id') ); |
195 | is( $fields, 'id', 'Constraint field = "id"' ); |
196 | |
197 | $fields = join(',', $constraint1->fields('id,id') ); |
198 | is( $fields, 'id', 'Constraint field = "id"' ); |
199 | |
200 | $fields = join(',', $constraint1->fields('id', 'name') ); |
201 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
202 | |
203 | $fields = join(',', $constraint1->fields( 'id,name,id' ) ); |
204 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
205 | |
206 | $fields = join(',', $constraint1->fields( 'id, name' ) ); |
207 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
208 | |
209 | $fields = join(',', $constraint1->fields( [ 'id', 'name' ] ) ); |
210 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
211 | |
212 | $fields = join(',', $constraint1->fields( qw[ id name ] ) ); |
213 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
214 | |
14f8758f |
215 | is( $constraint1->match_type, '', 'Constraint match type is empty' ); |
216 | is( $constraint1->match_type('foo'), undef, |
217 | 'Constraint match type rejects bad arg...' ); |
218 | like( $constraint1->error, qr/invalid match type/i, |
219 | '...because it is invalid'); |
220 | is( $constraint1->match_type('FULL'), 'full', |
221 | 'Constraint match type = "full"' ); |
222 | |
07680493 |
223 | my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' ); |
224 | $constraint2 = $person_table->add_constraint( $constraint2 ); |
225 | isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' ); |
226 | is( $constraint2->name, 'bar', 'Constraint name is "bar"' ); |
227 | |
228 | my $constraints = $person_table->get_constraints; |
229 | is( scalar @$constraints, 2, 'Two constraints' ); |
230 | is( $constraints->[0]->name, 'foo', '"foo" constraint' ); |
231 | is( $constraints->[1]->name, 'bar', '"bar" constraint' ); |
232 | |
233 | # |
234 | # View |
235 | # |
236 | my $view = $schema->add_view( name => 'view1' ) or warn $schema->error; |
237 | isa_ok( $view, 'SQL::Translator::Schema::View', 'View' ); |
238 | my $view_sql = 'select * from table'; |
239 | is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' ); |
240 | |
241 | my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or |
242 | warn SQL::Translator::Schema::View->error; |
243 | my $check_view = $schema->add_view( $view2 ); |
244 | is( $check_view->name, 'view2', 'Add view "view2"' ); |
245 | |
246 | my $redundant_view = $schema->add_view(name => 'view2'); |
247 | is( $redundant_view, undef, qq[Didn't create another "view2" view...] ); |
248 | like( $schema->error, qr/can't create view/i, '... because it exists' ); |
249 | |
250 | # |
251 | # $schema->get_* |
252 | # |
253 | my $bad_table = $schema->get_table; |
254 | like( $schema->error, qr/no table/i, 'Error on no arg to get_table' ); |
255 | |
256 | $bad_table = $schema->get_table('baz'); |
257 | like( $schema->error, qr/does not exist/i, |
258 | 'Error on bad arg to get_table' ); |
259 | |
260 | my $bad_view = $schema->get_view; |
261 | like( $schema->error, qr/no view/i, 'Error on no arg to get_view' ); |
262 | |
263 | $bad_view = $schema->get_view('bar'); |
264 | like( $schema->error, qr/does not exist/i, |
265 | 'Error on bad arg to get_view' ); |
266 | |
267 | my $good_table = $schema->get_table('foo'); |
268 | isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' ); |
269 | |
270 | my $good_view = $schema->get_view('view1'); |
271 | isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' ); |
272 | is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' ); |
273 | |
274 | # |
275 | # $schema->get_*s |
276 | # |
277 | my @tables = $schema->get_tables; |
278 | is( scalar @tables, 3, 'Found 2 tables' ); |
279 | |
280 | my @views = $schema->get_views; |
281 | is( scalar @views, 2, 'Found 1 view' ); |
282 | } |
4d878d2f |
283 | |
284 | # |
07680493 |
285 | # Test ability to introspect some values |
4d878d2f |
286 | # |
07680493 |
287 | { |
288 | my $s = SQL::Translator::Schema->new( |
289 | name => 'foo', |
290 | database => 'PostgreSQL', |
291 | ); |
292 | my $t = $s->add_table( name => 'person' ) or warn $s->erro; |
293 | my $f = $t->add_field( name => 'person_id' ) or warn $t->error; |
294 | $f->data_type('serial'); |
295 | |
296 | my $c = $t->add_constraint( |
297 | type => PRIMARY_KEY, |
298 | fields => 'person_id', |
299 | ) or warn $t->error; |
300 | |
301 | is( $f->is_primary_key, 1, 'Field is PK' ); |
302 | is( $f->is_auto_increment, 1, 'Field is auto inc' ); |
303 | } |
4d878d2f |
304 | |
305 | # |
07680493 |
306 | # FK constraint validity |
4d878d2f |
307 | # |
07680493 |
308 | { |
309 | my $s = SQL::Translator::Schema->new; |
310 | my $t = $s->add_table( name => 'person' ) or warn $s->error; |
311 | my $c = $t->add_constraint or warn $t->error; |
312 | |
313 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
314 | like( $c->error, qr/no type/i, '...because it has no type' ); |
315 | |
316 | is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' ); |
317 | |
318 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
319 | like( $c->error, qr/no fields/i, '...because it has no fields' ); |
320 | |
321 | is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' ); |
322 | |
323 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
324 | like( $c->error, qr/non-existent field/i, |
325 | q[...because field "foo" doesn't exist] ); |
326 | |
327 | my $fk = $t->add_field( name => 'pet_id' ); |
328 | is( $fk->name, 'pet_id', 'Added field "pet_id"' ); |
329 | is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' ); |
330 | |
331 | $t->add_field( name => 'f1' ); |
332 | $t->add_field( name => 'f2' ); |
333 | is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' ); |
334 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
335 | like( $c->error, qr/only one field/i, |
336 | q[...because too many fields for FK] ); |
337 | |
338 | $c->fields('f1'); |
339 | |
340 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
341 | like( $c->error, qr/no reference table/i, |
342 | q[...because there's no reference table] ); |
343 | |
344 | is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' ); |
345 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
346 | like( $c->error, qr/no table named/i, |
347 | q[...because reference table "foo" doesn't exist] ); |
348 | |
349 | my $t2 = $s->add_table( name => 'pet' ); |
350 | is( $t2->name, 'pet', 'Added "pet" table' ); |
351 | |
352 | is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' ); |
353 | |
354 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
355 | like( $c->error, qr/no reference fields/i, |
356 | q[...because there're no reference fields]); |
357 | |
358 | is( join('', $c->reference_fields('pet_id')), 'pet_id', |
359 | 'Reference fields = "pet_id"' ); |
360 | |
361 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
362 | like( $c->error, qr/non-existent field/i, |
363 | q[...because there's no "pet_id" field in "pet"]); |
364 | |
365 | my $pet_id = $t2->add_field( name => 'pet_id' ); |
366 | is( $pet_id->name, 'pet_id', 'Added field "pet_id"' ); |
367 | |
368 | is( $c->is_valid, 1, 'Constraint now valid' ); |
07680493 |
369 | } |
586fba3f |
370 | |
371 | # |
07680493 |
372 | # $table->primary_key test |
586fba3f |
373 | # |
07680493 |
374 | { |
375 | my $s = SQL::Translator::Schema->new; |
376 | my $t = $s->add_table( name => 'person' ); |
377 | |
378 | is( $t->primary_key, undef, 'No primary key' ); |
379 | like( $t->error, qr/no primary key/i, 'No primary key error' ); |
586fba3f |
380 | |
07680493 |
381 | is( $t->primary_key('person_id'), undef, |
382 | q[Can't make PK on "person_id"...] ); |
383 | like( $t->error, qr/invalid field/i, "...because it doesn't exist" ); |
586fba3f |
384 | |
07680493 |
385 | $t->add_field( name => 'person_id' ); |
386 | my $c = $t->primary_key('person_id'); |
586fba3f |
387 | |
07680493 |
388 | isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' ); |
586fba3f |
389 | |
07680493 |
390 | is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' ); |
586fba3f |
391 | |
07680493 |
392 | $t->add_field( name => 'name' ); |
393 | $c = $t->primary_key('name'); |
394 | is( join(',', $c->fields), 'person_id,name', |
395 | 'Constraint now on "person_id" and "name"' ); |
396 | |
397 | is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' ); |
398 | } |
586fba3f |
399 | |
400 | # |
07680493 |
401 | # FK finds PK |
586fba3f |
402 | # |
07680493 |
403 | { |
404 | my $s = SQL::Translator::Schema->new; |
405 | my $t1 = $s->add_table( name => 'person' ); |
406 | my $t2 = $s->add_table( name => 'pet' ); |
407 | $t1->add_field( name => 'id' ); |
408 | my $c1 = $t1->primary_key( 'id' ) or warn $t1->error; |
409 | is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' ); |
410 | |
411 | $t2->add_field( name => 'person_id' ); |
412 | my $c2 = $t2->add_constraint( |
413 | type => PRIMARY_KEY, |
414 | fields => 'person_id', |
415 | reference_table => 'person', |
416 | ); |
586fba3f |
417 | |
07680493 |
418 | is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' ); |
419 | } |
14f8758f |
420 | |
421 | # |
422 | # View |
423 | # |
424 | { |
425 | my $name = 'foo_view'; |
426 | my $sql = 'select name, age from person'; |
427 | my $fields = 'name, age'; |
428 | my $s = SQL::Translator::Schema->new; |
429 | my $v = $s->add_view( |
430 | name => $name, |
431 | sql => $sql, |
432 | fields => $fields, |
433 | ); |
434 | |
435 | isa_ok( $v, 'SQL::Translator::Schema::View', 'View' ); |
436 | is( $v->name, $name, qq[Name is "$name"] ); |
437 | is( $v->sql, $sql, qq[Name is "$sql"] ); |
438 | is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] ); |
439 | } |