Added full_name.
[dbsrgits/SQL-Translator.git] / t / 13schema.t
1 #!/usr/bin/perl
2 # vim:set ft=perl:
3
4 $| = 1;
5
6 use strict;
7 use Test::More tests => 197;
8 use SQL::Translator::Schema::Constants;
9
10 require_ok( 'SQL::Translator::Schema' );
11
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' );
21
22     is( $schema->name, 'foo', 'Schema name is "foo"' );
23     is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' );
24
25     is( $schema->database, 'MySQL', 'Schema database is "MySQL"' );
26     is( $schema->database('PostgreSQL'), 'PostgreSQL', 
27         'Schema database changed to "PostgreSQL"' );
28
29     is( $schema->is_valid, undef, 'Schema not valid...' );
30     like( $schema->error, qr/no tables/i, '...because there are no tables' );
31
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"' );
37
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"' );
43
44     $schema = $bar_table->schema( $schema );
45     isa_ok( $schema, 'SQL::Translator::Schema', 'Schema' );
46
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] );
51
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, 
55         '... because "foo" exists' );
56
57     $redundant_table = $schema->add_table(name => '');
58     is( $redundant_table, undef, qq[Can't add an anonymouse table...] );
59     like( $schema->error, qr/No table name/i,
60         '... because if has no name ' );
61
62     $redundant_table = SQL::Translator::Schema::Table->new(name => '');
63     is( $redundant_table, undef, qq[Can't create an anonymouse table] );
64     like( SQL::Translator::Schema::Table->error, qr/No table name/i,
65         '... because if has no name ' );
66
67     #
68     # Table default new
69     #
70     is( $foo_table->name, 'foo', 'Table name is "foo"' );
71     is( "$foo_table", 'foo', 'Table stringifies to "foo"' );
72     is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
73
74     my $fields = $foo_table->get_fields;
75     is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
76     like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
77
78     is( $foo_table->comments, undef, 'No comments' );
79
80     #
81     # New table with args
82     #
83     my $person_table = $schema->add_table( 
84         name     => 'person', 
85         comments => 'foo',
86     );
87     is( $person_table->name, 'person', 'Table name is "person"' );
88     is( $person_table->is_valid, undef, 'Table is not yet valid' );
89     is( $person_table->comments, 'foo', 'Comments = "foo"' );
90     is( join(',', $person_table->comments('bar')), 'foo,bar', 
91         'Table comments = "foo,bar"' );
92     is( $person_table->comments, "foo\nbar", 'Table comments = "foo,bar"' );
93
94     #
95     # Field default new
96     #
97     my $f1 = $person_table->add_field(name => 'foo') or 
98         warn $person_table->error;
99     isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' );
100     is( $f1->name, 'foo', 'Field name is "foo"' );
101     is( $f1->full_name, 'person.foo', 'Field full_name is "person.foo"' );
102     is( "$f1", 'foo', 'Field stringifies to "foo"' );
103     is( $f1->data_type, '', 'Field data type is blank' );
104     is( $f1->size, 0, 'Field size is "0"' );
105     is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
106     is( $f1->is_nullable, 1, 'Field can be NULL' );
107     is( $f1->default_value, undef, 'Field default is undefined' );
108     is( $f1->comments, '', 'No comments' );
109
110     my $f2 = SQL::Translator::Schema::Field->new (
111         name     => 'f2',
112         comments => 'foo',
113     ) or warn SQL::Translator::Schema::Field->error;
114     $f2 = $person_table->add_field( $f2 );
115     isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
116     is( $f2->name, 'f2', 'Add field "f2"' );
117     is( $f2->is_nullable(0), 0, 'Field cannot be NULL' );
118     is( $f2->is_nullable(''), 0, 'Field cannot be NULL' );
119     is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' );
120     is( $f2->default_value(''), '', 'Field default is empty string' );
121     is( $f2->comments, 'foo', 'Field comment = "foo"' );
122     is( join(',', $f2->comments('bar')), 'foo,bar', 
123         'Field comment = "foo,bar"' );
124     is( $f2->comments, "foo\nbar", 'Field comment = "foo,bar"' );
125
126     $person_table = $f2->table( $person_table );
127     isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' );
128
129     is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] );
130     like( $f2->error, qr/can't use field name/i, '...because name exists' );
131
132     my $redundant_field = $person_table->add_field(name => 'f2');
133     is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
134     like( $person_table->error, qr/can't use field/i, 
135         '... because it exists' );
136
137     $redundant_field = $person_table->add_field(name => '');
138     is( $redundant_field, undef, qq[Didn't add a "" field...] );
139     like( $person_table->error, qr/No field name/i,
140         '... because it has no name' );
141
142     $redundant_field = SQL::Translator::Schema::Field->new(name => '');
143     is( $redundant_field, undef, qq[Didn't create a "" field...] );
144     like( SQL::Translator::Schema::Field->error, qr/No field name/i,
145         '... because it has no name' );
146
147     my @fields = $person_table->get_fields;
148     is( scalar @fields, 2, 'Table "foo" has 2 fields' );
149
150     is( $fields[0]->name, 'foo', 'First field is "foo"' );
151     is( $fields[1]->name, 'f2', 'Second field is "f2"' );
152
153     #
154     # Field methods
155     #
156     is( $f1->name('person_name'), 'person_name',
157         'Field name is "person_name"' );
158     is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
159     is( $f1->size('30'), '30', 'Field size is "30"' );
160     is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
161
162     $f1->extra( foo => 'bar' );
163     $f1->extra( { baz => 'quux' } );
164     my %extra = $f1->extra;
165     is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' );
166     is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' );
167
168     #
169     # New field with args
170     #
171     my $age       = $person_table->add_field(
172         name      => 'age',
173         data_type => 'float',
174         size      => '10,2',
175     );
176     is( $age->name, 'age', 'Field name is "age"' );
177     is( $age->data_type, 'float', 'Field data type is "float"' );
178     is( $age->size, '10,2', 'Field size is "10,2"' );
179     is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
180     is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
181     is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
182     is( join(':', $age->size), '10:2', 'Field size returns array' );
183
184     #
185     # Index
186     #
187     my @indices = $person_table->get_indices;
188     is( scalar @indices, 0, 'No indices' );
189     like( $person_table->error, qr/no indices/i, 'Error for no indices' );
190     my $index1 = $person_table->add_index( name => "foo" ) 
191         or warn $person_table->error;
192     isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' );
193     is( $index1->name, 'foo', 'Index name is "foo"' );
194
195     is( $index1->is_valid, undef, 'Index name is not valid...' );
196     like( $index1->error, qr/no fields/i, '...because it has no fields' );
197
198     is( join(':', $index1->fields('foo,bar')), 'foo:bar', 
199         'Index accepts fields');
200
201     is( $index1->is_valid, undef, 'Index name is not valid...' );
202     like( $index1->error, qr/does not exist in table/i, 
203         '...because it used fields not in the table' );
204
205     is( join(':', $index1->fields(qw[foo age])), 'foo:age', 
206         'Index accepts fields');
207     is( $index1->is_valid, 1, 'Index name is now valid' );
208
209     is( $index1->type, NORMAL, 'Index type is "normal"' );
210
211     my $index2 = SQL::Translator::Schema::Index->new( name => "bar" ) 
212         or warn SQL::Translator::Schema::Index->error;
213     $index2    = $person_table->add_index( $index2 );
214     isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' );
215     is( $index2->name, 'bar', 'Index name is "bar"' );
216
217     my $indices = $person_table->get_indices;
218     is( scalar @$indices, 2, 'Two indices' );
219     is( $indices->[0]->name, 'foo', '"foo" index' );
220     is( $indices->[1]->name, 'bar', '"bar" index' );
221
222     #
223     # Constraint
224     #
225     my @constraints = $person_table->get_constraints;
226     is( scalar @constraints, 0, 'No constraints' );
227     like( $person_table->error, qr/no constraints/i, 
228         'Error for no constraints' );
229     my $constraint1 = $person_table->add_constraint( name => 'foo' ) 
230         or warn $person_table->error;
231     isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' );
232     is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
233
234     $fields = join(',', $constraint1->fields('id') );
235     is( $fields, 'id', 'Constraint field = "id"' );
236
237     $fields = join(',', $constraint1->fields('id,id') );
238     is( $fields, 'id', 'Constraint field = "id"' );
239
240     $fields = join(',', $constraint1->fields('id', 'name') );
241     is( $fields, 'id,name', 'Constraint field = "id,name"' );
242
243     $fields = join(',', $constraint1->fields( 'id,name,id' ) );
244     is( $fields, 'id,name', 'Constraint field = "id,name"' );
245
246     $fields = join(',', $constraint1->fields( 'id, name' ) );
247     is( $fields, 'id,name', 'Constraint field = "id,name"' );
248
249     $fields = join(',', $constraint1->fields( [ 'id', 'name' ] ) );
250     is( $fields, 'id,name', 'Constraint field = "id,name"' );
251
252     $fields = join(',', $constraint1->fields( qw[ id name ] ) );
253     is( $fields, 'id,name', 'Constraint field = "id,name"' );
254
255     is( $constraint1->match_type, '', 'Constraint match type is empty' );
256     is( $constraint1->match_type('foo'), undef, 
257         'Constraint match type rejects bad arg...' );
258     like( $constraint1->error, qr/invalid match type/i,
259         '...because it is invalid');
260     is( $constraint1->match_type('FULL'), 'full', 
261         'Constraint match type = "full"' );
262
263     my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' );
264     $constraint2    = $person_table->add_constraint( $constraint2 );
265     isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' );
266     is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
267
268     my $constraint3 = $person_table->add_constraint(
269         type       => 'check',
270         expression => 'foo bar',
271     ) or die $person_table->error;
272     isa_ok( $constraint3, 'SQL::Translator::Schema::Constraint', 'Constraint' );
273     is( $constraint3->type, CHECK_C, 'Constraint type is "CHECK"' );
274     is( $constraint3->expression, 'foo bar', 
275         'Constraint expression is "foo bar"' );
276
277     my $constraints = $person_table->get_constraints;
278     is( scalar @$constraints, 3, 'Three constraints' );
279     is( $constraints->[0]->name, 'foo', '"foo" constraint' );
280     is( $constraints->[1]->name, 'bar', '"bar" constraint' );
281
282     #
283     # View
284     #
285     my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
286     isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
287     my $view_sql = 'select * from table';
288     is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
289
290     my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or
291         warn SQL::Translator::Schema::View->error;
292     my $check_view = $schema->add_view( $view2 );
293     is( $check_view->name, 'view2', 'Add view "view2"' );
294
295     my $redundant_view = $schema->add_view(name => 'view2');
296     is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
297     like( $schema->error, qr/can't create view/i, '... because it exists' );
298
299     #
300     # $schema->get_*
301     #
302     my $bad_table = $schema->get_table;
303     like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
304
305     $bad_table = $schema->get_table('baz');
306     like( $schema->error, qr/does not exist/i, 
307         'Error on bad arg to get_table' );
308
309     my $bad_view = $schema->get_view;
310     like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
311
312     $bad_view = $schema->get_view('bar');
313     like( $schema->error, qr/does not exist/i, 
314         'Error on bad arg to get_view' );
315
316     my $good_table = $schema->get_table('foo');
317     isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
318
319     my $good_view = $schema->get_view('view1');
320     isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
321     is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
322
323     #
324     # $schema->get_*s
325     #
326     my @tables = $schema->get_tables;
327     is( scalar @tables, 3, 'Found 2 tables' );
328
329     my @views = $schema->get_views;
330     is( scalar @views, 2, 'Found 1 view' );
331 }
332
333 #
334 # Test ability to introspect some values
335 #
336
337     my $s        =  SQL::Translator::Schema->new( 
338         name     => 'foo',
339         database => 'PostgreSQL',
340     );
341     my $t = $s->add_table( name => 'person' ) or warn $s->erro;
342     my $f = $t->add_field( name => 'person_id' ) or warn $t->error;
343     $f->data_type('serial');
344
345     my $c          = $t->add_constraint(
346         type       => PRIMARY_KEY,
347         fields     => 'person_id',
348     ) or warn $t->error;
349
350     is( $f->is_primary_key, 1, 'Field is PK' );
351     is( $f->is_auto_increment, 1, 'Field is auto inc' );
352 }
353
354 #
355 # FK constraint validity
356 #
357 {
358     my $s = SQL::Translator::Schema->new;
359     my $t = $s->add_table( name => 'person' ) or warn $s->error;
360     my $c = $t->add_constraint or warn $t->error;
361
362     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
363     like( $c->error, qr/no type/i, '...because it has no type' );
364
365     is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
366
367     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
368     like( $c->error, qr/no fields/i, '...because it has no fields' );
369
370     is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
371
372     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
373     like( $c->error, qr/non-existent field/i, 
374         q[...because field "foo" doesn't exist] );
375
376     my $fk = $t->add_field( name => 'pet_id' );
377     is( $fk->name, 'pet_id', 'Added field "pet_id"' );
378     is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
379
380     $t->add_field( name => 'f1' );
381     $t->add_field( name => 'f2' );
382     is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
383     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
384     like( $c->error, qr/only one field/i, 
385         q[...because too many fields for FK] );
386
387     $c->fields('f1');
388
389     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
390     like( $c->error, qr/no reference table/i, 
391         q[...because there's no reference table] );
392
393     is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
394     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
395     like( $c->error, qr/no table named/i, 
396         q[...because reference table "foo" doesn't exist] );
397
398     my $t2 = $s->add_table( name => 'pet' );
399     is( $t2->name, 'pet', 'Added "pet" table' );
400
401     is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
402
403     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
404     like( $c->error, qr/no reference fields/i,
405         q[...because there're no reference fields]);
406
407     is( join('', $c->reference_fields('pet_id')), 'pet_id', 
408         'Reference fields = "pet_id"' );
409
410     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
411     like( $c->error, qr/non-existent field/i,
412         q[...because there's no "pet_id" field in "pet"]);
413
414     my $pet_id = $t2->add_field( name => 'pet_id' );
415     is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
416     
417     is( $c->is_valid, 1, 'Constraint now valid' );
418 }
419
420 #
421 # $table->primary_key test
422 #
423 {
424     my $s = SQL::Translator::Schema->new;
425     my $t = $s->add_table( name => 'person' );
426
427     is( $t->primary_key, undef, 'No primary key' );
428
429     is( $t->primary_key('person_id'), undef, 
430             q[Can't make PK on "person_id"...] );
431     like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
432
433     $t->add_field( name => 'person_id' );
434     my $c = $t->primary_key('person_id');
435
436     isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' );
437
438     is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
439
440     $t->add_field( name => 'name' );
441     $c = $t->primary_key('name');
442     is( join(',', $c->fields), 'person_id,name', 
443         'Constraint now on "person_id" and "name"' );
444
445     is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
446 }
447
448 #
449 # FK finds PK
450 #
451 {
452     my $s  = SQL::Translator::Schema->new;
453     my $t1 = $s->add_table( name => 'person' );
454     my $t2 = $s->add_table( name => 'pet' );
455     $t1->add_field( name => 'id' );
456     my $c1 = $t1->primary_key( 'id' ) or warn $t1->error;
457     is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
458
459     $t2->add_field( name => 'person_id' );
460     my $c2 = $t2->add_constraint(
461         type            => PRIMARY_KEY,
462         fields          => 'person_id',
463         reference_table => 'person',
464     );
465
466     is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
467 }
468
469 #
470 # View
471 #
472 {
473     my $s      = SQL::Translator::Schema->new( name => 'ViewTest' );
474     my $name   = 'foo_view';
475     my $sql    = 'select name, age from person';
476     my $fields = 'name, age';
477     my $v      = $s->add_view(
478         name   => $name,
479         sql    => $sql,
480         fields => $fields,
481         schema => $s,
482     );
483
484     isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
485     isa_ok( $v->schema, 'SQL::Translator::Schema', 'Schema' );
486     is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
487     is( $v->name, $name, qq[Name is "$name"] );
488     is( $v->sql, $sql, qq[Name is "$sql"] );
489     is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );
490
491     my @views = $s->get_views;
492     is( scalar @views, 1, 'Number of views is 1' );
493
494     my $v1 = $s->get_view( $name );
495     isa_ok( $v1, 'SQL::Translator::Schema::View', 'View' );
496     is( $v1->name, $name, qq[Name is "$name"] );
497 }
498
499 #
500 # Trigger
501 #
502 {
503     my $s                   = SQL::Translator::Schema->new(name => 'TrigTest');
504     my $name                = 'foo_trigger';
505     my $perform_action_when = 'after';
506     my $database_event      = 'insert';
507     my $on_table            = 'foo';
508     my $action              = 'update modified=timestamp();';
509     my $t                   = $s->add_trigger(
510         name                => $name,
511         perform_action_when => $perform_action_when,
512         database_event      => $database_event,
513         on_table            => $on_table,
514         action              => $action,
515     ) or die $s->error;
516
517     isa_ok( $t, 'SQL::Translator::Schema::Trigger', 'Trigger' );
518     isa_ok( $t->schema, 'SQL::Translator::Schema', 'Schema' );
519     is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
520     is( $t->name, $name, qq[Name is "$name"] );
521     is( $t->perform_action_when, $perform_action_when, 
522         qq[Perform action when is "$perform_action_when"] );
523     is( $t->database_event, $database_event, 
524         qq[Database event is "$database_event"] );
525     is( $t->on_table, $on_table, qq[Table is "$on_table"] );
526     is( $t->action, $action, qq[Action is "$action"] );
527
528     my @triggs = $s->get_triggers;
529     is( scalar @triggs, 1, 'Number of triggers is 1' );
530
531     my $t1 = $s->get_trigger( $name );
532     isa_ok( $t1, 'SQL::Translator::Schema::Trigger', 'Trigger' );
533     is( $t1->name, $name, qq[Name is "$name"] );
534 }
535
536 #
537 # Procedure
538 #
539 {
540     my $s          = SQL::Translator::Schema->new( name => 'ProcTest' );
541     my $name       = 'foo_proc';
542     my $sql        = 'select foo from bar';
543     my $parameters = 'foo, bar';
544     my $owner      = 'Nomar';
545     my $comments   = 'Go Sox!';
546     my $p          = $s->add_procedure(
547         name       => $name,
548         sql        => $sql,
549         parameters => $parameters,
550         owner      => $owner,
551         comments   => $comments,
552     ) or die $s->error;
553
554     isa_ok( $p, 'SQL::Translator::Schema::Procedure', 'Procedure' );
555     isa_ok( $p->schema, 'SQL::Translator::Schema', 'Schema' );
556     is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
557     is( $p->name, $name, qq[Name is "$name"] );
558     is( $p->sql, $sql, qq[SQL is "$sql"] );
559     is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
560     is( $p->comments, $comments, qq[Comments = "$comments"] );
561
562     my @procs = $s->get_procedures;
563     is( scalar @procs, 1, 'Number of procedures is 1' );
564
565     my $p1 = $s->get_procedure( $name );
566     isa_ok( $p1, 'SQL::Translator::Schema::Procedure', 'Procedure' );
567     is( $p1->name, $name, qq[Name is "$name"] );
568 }