Add support for USING and WHERE on indexes in PostgreSQL producer
[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 warnings;
8 use Test::More;
9 use Test::Exception;
10 use SQL::Translator::Schema::Constants;
11
12 require_ok( 'SQL::Translator' );
13 require_ok( 'SQL::Translator::Schema' );
14
15 {
16     #
17     # Schema
18     #
19     my $schema = SQL::Translator::Schema->new(
20         name => 'foo',
21         database => 'MySQL',
22     );
23     isa_ok( $schema, 'SQL::Translator::Schema' );
24
25     is( $schema->name, 'foo', 'Schema name is "foo"' );
26     is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' );
27
28     is( $schema->database, 'MySQL', 'Schema database is "MySQL"' );
29     is( $schema->database('PostgreSQL'), 'PostgreSQL',
30         'Schema database changed to "PostgreSQL"' );
31
32     is( $schema->is_valid, undef, 'Schema not valid...' );
33     like( $schema->error, qr/no tables/i, '...because there are no tables' );
34
35     #
36     # $schema->add_*
37     #
38     my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error;
39     isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
40
41     my $bar_table = SQL::Translator::Schema::Table->new( name => 'bar' ) or
42         warn SQL::Translator::Schema::Table->error;
43     $bar_table = $schema->add_table( $bar_table );
44     isa_ok( $bar_table, 'SQL::Translator::Schema::Table', 'Table "bar"' );
45     is( $bar_table->name, 'bar', 'Add table "bar"' );
46
47     $schema = $bar_table->schema( $schema );
48     isa_ok( $schema, 'SQL::Translator::Schema', 'Schema' );
49
50     is( $bar_table->name('foo'), undef,
51         q[Can't change name of table "bar" to "foo"...]);
52     like( $bar_table->error, qr/can't use table name/i,
53         q[...because "foo" exists] );
54
55     my $redundant_table = $schema->add_table(name => 'foo');
56     is( $redundant_table, undef, qq[Can't create another "foo" table...] );
57     like( $schema->error, qr/can't use table name/i,
58         '... because "foo" exists' );
59
60     $redundant_table = $schema->add_table(name => '');
61     is( $redundant_table, undef, qq[Can't add an anonymous table...] );
62     like( $schema->error, qr/No table name/i,
63         '... because it has no name ' );
64
65     $redundant_table = SQL::Translator::Schema::Table->new(name => '');
66     is( $redundant_table, undef, qq[Can't create an anonymous table] );
67     like( SQL::Translator::Schema::Table->error, qr/No table name/i,
68         '... because it has no name ' );
69
70     #
71     # $schema-> drop_table
72     #
73     my $dropped_table = $schema->drop_table($foo_table->name, cascade => 1);
74     isa_ok($dropped_table, 'SQL::Translator::Schema::Table', 'Dropped table "foo"' );
75     $schema->add_table($foo_table);
76     my $dropped_table2 = $schema->drop_table($foo_table, cascade => 1);
77     isa_ok($dropped_table2, 'SQL::Translator::Schema::Table', 'Dropped table "foo" by object' );
78     my $dropped_table3 = $schema->drop_table($foo_table->name, cascade => 1);
79     like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant table "foo"] );
80
81     $schema->add_table($foo_table);
82     #
83     # Table default new
84     #
85     is( $foo_table->name, 'foo', 'Table name is "foo"' );
86     is( "$foo_table", 'foo', 'Table stringifies to "foo"' );
87     is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
88
89     my $fields = $foo_table->get_fields;
90     is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
91     like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
92
93     is( $foo_table->comments, undef, 'No comments' );
94
95     #
96     # New table with args
97     #
98     my $person_table = $schema->add_table(
99         name     => 'person',
100         comments => 'foo',
101     );
102     is( $person_table->name, 'person', 'Table name is "person"' );
103     is( $person_table->is_valid, undef, 'Table is not yet valid' );
104     is( $person_table->comments, 'foo', 'Comments = "foo"' );
105     is( join(',', $person_table->comments('bar')), 'foo,bar',
106         'Table comments = "foo,bar"' );
107     is( $person_table->comments, "foo\nbar", 'Table comments = "foo,bar"' );
108
109     #
110     # Field default new
111     #
112     my $f1 = $person_table->add_field(name => 'foo') or
113         warn $person_table->error;
114     isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' );
115     is( $f1->name, 'foo', 'Field name is "foo"' );
116     is( $f1->full_name, 'person.foo', 'Field full_name is "person.foo"' );
117     is( "$f1", 'foo', 'Field stringifies to "foo"' );
118     is( $f1->data_type, '', 'Field data type is blank' );
119     is( $f1->size, 0, 'Field size is "0"' );
120     is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
121     is( $f1->is_nullable, 1, 'Field can be NULL' );
122     is( $f1->default_value, undef, 'Field default is undefined' );
123     is( $f1->comments, '', 'No comments' );
124     is( $f1->table, 'person', 'Field table is person' );
125     is( $f1->schema->database, 'PostgreSQL', 'Field schema shortcut works' );
126
127     my $f2 = SQL::Translator::Schema::Field->new (
128         name     => 'f2',
129         comments => 'foo',
130     ) or warn SQL::Translator::Schema::Field->error;
131     $f2 = $person_table->add_field( $f2 );
132     isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
133     is( $f2->name, 'f2', 'Add field "f2"' );
134     is( $f2->is_nullable(0), 0, 'Field cannot be NULL' );
135     is( $f2->is_nullable(''), 0, 'Field cannot be NULL' );
136     is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' );
137     is( $f2->default_value(''), '', 'Field default is empty string' );
138     is( $f2->comments, 'foo', 'Field comment = "foo"' );
139     is( join(',', $f2->comments('bar')), 'foo,bar',
140         'Field comment = "foo,bar"' );
141     is( $f2->comments, "foo\nbar", 'Field comment = "foo,bar"' );
142
143     $person_table = $f2->table( $person_table );
144     isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' );
145
146     is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] );
147     like( $f2->error, qr/can't use field name/i, '...because name exists' );
148
149     my $redundant_field = $person_table->add_field(name => 'f2');
150     is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
151     like( $person_table->error, qr/can't use field/i,
152         '... because it exists' );
153
154     $redundant_field = $person_table->add_field(name => '');
155     is( $redundant_field, undef, qq[Didn't add a "" field...] );
156     like( $person_table->error, qr/No field name/i,
157         '... because it has no name' );
158
159     $redundant_field = SQL::Translator::Schema::Field->new(name => '');
160     is( $redundant_field, undef, qq[Didn't create a "" field...] );
161     like( SQL::Translator::Schema::Field->error, qr/No field name/i,
162         '... because it has no name' );
163
164     my @fields = $person_table->get_fields;
165     is( scalar @fields, 2, 'Table "foo" has 2 fields' );
166
167     is( $fields[0]->name, 'foo', 'First field is "foo"' );
168     is( $fields[1]->name, 'f2', 'Second field is "f2"' );
169     is( join(",",$person_table->field_names), 'foo,f2',
170         'field_names is "foo,f2"' );
171
172     my $ci_field = $person_table->get_field('FOO', 'case_insensitive');
173     is( $ci_field->name, 'foo', 'Got field case-insensitively' );
174     #
175     # $table-> drop_field
176     #
177     my $dropped_field = $person_table->drop_field($f2->name, cascade => 1);
178     isa_ok($dropped_field, 'SQL::Translator::Schema::Field', 'Dropped field "f2"' );
179     $person_table->add_field($f2);
180     my $dropped_field2 = $person_table->drop_field($f2, cascade => 1);
181     isa_ok($dropped_field2, 'SQL::Translator::Schema::Field', 'Dropped field "f2" by object' );
182     my $dropped_field3 = $person_table->drop_field($f2->name, cascade => 1);
183     like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant field "f2"] );
184
185     $person_table->add_field($f2);
186
187     #
188     # Field methods
189     #
190     is( $f1->name('person_name'), 'person_name',
191         'Field name is "person_name"' );
192     is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
193     is( $f1->size('30'), '30', 'Field size is "30"' );
194     is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
195
196     $f1->extra( foo => 'bar' );
197     $f1->extra( { baz => 'quux' } );
198     my %extra = $f1->extra;
199     is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' );
200     is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' );
201
202     #
203     # New field with args
204     #
205     my $age       = $person_table->add_field(
206         name      => 'age',
207         data_type => 'float',
208         size      => '10,2',
209     );
210     is( $age->name, 'age', 'Field name is "age"' );
211     is( $age->data_type, 'float', 'Field data type is "float"' );
212     is( $age->size, '10,2', 'Field size is "10,2"' );
213     is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
214     is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
215     is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
216     is( join(':', $age->size), '10:2', 'Field size returns array' );
217
218     #
219     # Index
220     #
221     my @indices = $person_table->get_indices;
222     is( scalar @indices, 0, 'No indices' );
223     like( $person_table->error, qr/no indices/i, 'Error for no indices' );
224     my $index1 = $person_table->add_index( name => "foo" )
225         or warn $person_table->error;
226     isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' );
227     is( $index1->name, 'foo', 'Index name is "foo"' );
228
229     is( $index1->is_valid, undef, 'Index name is not valid...' );
230     like( $index1->error, qr/no fields/i, '...because it has no fields' );
231
232     is( join(':', $index1->fields('foo,bar')), 'foo:bar',
233         'Index accepts fields');
234
235     is( $index1->is_valid, undef, 'Index name is not valid...' );
236     like( $index1->error, qr/does not exist in table/i,
237         '...because it used fields not in the table' );
238
239     is( join(':', $index1->fields(qw[foo age])), 'foo:age',
240         'Index accepts fields');
241     is( $index1->is_valid, 1, 'Index name is now valid' );
242
243     is( $index1->type, NORMAL, 'Index type is "normal"' );
244
245     my $index2 = SQL::Translator::Schema::Index->new( name => "bar" )
246         or warn SQL::Translator::Schema::Index->error;
247     $index2    = $person_table->add_index( $index2 );
248     isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' );
249     is( $index2->name, 'bar', 'Index name is "bar"' );
250
251     my $indices = $person_table->get_indices;
252     is( scalar @$indices, 2, 'Two indices' );
253     is( $indices->[0]->name, 'foo', '"foo" index' );
254     is( $indices->[1]->name, 'bar', '"bar" index' );
255
256     #
257     # $table-> drop_index
258     #
259     my $dropped_index = $person_table->drop_index($index1->name);
260     isa_ok($dropped_index, 'SQL::Translator::Schema::Index', 'Dropped index "foo"' );
261     $person_table->add_index($index1);
262     my $dropped_index2 = $person_table->drop_index($index1);
263     isa_ok($dropped_index2, 'SQL::Translator::Schema::Index', 'Dropped index "foo" by object' );
264     is($dropped_index2->name, $index1->name, 'Dropped correct index "foo"');
265     my $dropped_index3 = $person_table->drop_index($index1->name);
266     like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant index "foo"] );
267
268     $person_table->add_index($index1);
269
270     #
271     # Constraint
272     #
273     my @constraints = $person_table->get_constraints;
274     is( scalar @constraints, 0, 'No constraints' );
275     like( $person_table->error, qr/no constraints/i,
276         'Error for no constraints' );
277     my $constraint1 = $person_table->add_constraint( name => 'foo' )
278         or warn $person_table->error;
279     isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' );
280     is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
281
282     $fields = join(',', $constraint1->fields('age') );
283     is( $fields, 'age', 'Constraint field = "age"' );
284
285     $fields = $constraint1->fields;
286     ok( ref $fields[0] && $fields[0]->isa("SQL::Translator::Schema::Field"),
287         'Constraint fields returns a SQL::Translator::Schema::Field' );
288
289     $fields = join(',', $constraint1->fields('age,age') );
290     is( $fields, 'age', 'Constraint field = "age"' );
291
292     $fields = join(',', $constraint1->fields('age', 'name') );
293     is( $fields, 'age,name', 'Constraint field = "age,name"' );
294
295     $fields = join(',', $constraint1->fields( 'age,name,age' ) );
296     is( $fields, 'age,name', 'Constraint field = "age,name"' );
297
298     $fields = join(',', $constraint1->fields( 'age, name' ) );
299     is( $fields, 'age,name', 'Constraint field = "age,name"' );
300
301     $fields = join(',', $constraint1->fields( [ 'age', 'name' ] ) );
302     is( $fields, 'age,name', 'Constraint field = "age,name"' );
303
304     $fields = join(',', $constraint1->fields( qw[ age name ] ) );
305     is( $fields, 'age,name', 'Constraint field = "age,name"' );
306
307     $fields = join(',', $constraint1->field_names );
308     is( $fields, 'age,name', 'Constraint field_names = "age,name"' );
309
310     is( $constraint1->match_type, '', 'Constraint match type is empty' );
311     is( $constraint1->match_type('foo'), undef,
312         'Constraint match type rejects bad arg...' );
313     like( $constraint1->error, qr/invalid match type/i,
314         '...because it is invalid');
315     is( $constraint1->match_type('FULL'), 'full',
316         'Constraint match type = "full"' );
317
318     my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' );
319     $constraint2    = $person_table->add_constraint( $constraint2 );
320     isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' );
321     is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
322
323     my $constraint3 = $person_table->add_constraint(
324         type       => 'check',
325         expression => 'foo bar',
326     ) or die $person_table->error;
327     isa_ok( $constraint3, 'SQL::Translator::Schema::Constraint', 'Constraint' );
328     is( $constraint3->type, CHECK_C, 'Constraint type is "CHECK"' );
329     is( $constraint3->expression, 'foo bar',
330         'Constraint expression is "foo bar"' );
331
332     my $constraints = $person_table->get_constraints;
333     is( scalar @$constraints, 3, 'Three constraints' );
334     is( $constraints->[0]->name, 'foo', '"foo" constraint' );
335     is( $constraints->[1]->name, 'bar', '"bar" constraint' );
336
337     #
338     # $table-> drop_constraint
339     #
340     my $dropped_con = $person_table->drop_constraint($constraint1->name);
341     isa_ok($dropped_con, 'SQL::Translator::Schema::Constraint', 'Dropped constraint "foo"' );
342     $person_table->add_constraint($constraint1);
343     my $dropped_con2 = $person_table->drop_constraint($constraint1);
344     isa_ok($dropped_con2, 'SQL::Translator::Schema::Constraint', 'Dropped constraint "foo" by object' );
345     is($dropped_con2->name, $constraint1->name, 'Dropped correct constraint "foo"');
346     my $dropped_con3 = $person_table->drop_constraint($constraint1->name);
347     like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant constraint "foo"] );
348
349     $person_table->add_constraint($constraint1);
350
351     #
352     # View
353     #
354     my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
355     isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
356     my $view_sql = 'select * from table';
357     is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
358
359     my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or
360         warn SQL::Translator::Schema::View->error;
361     my $check_view = $schema->add_view( $view2 );
362     is( $check_view->name, 'view2', 'Add view "view2"' );
363
364     my $redundant_view = $schema->add_view(name => 'view2');
365     is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
366     like( $schema->error, qr/can't create view/i, '... because it exists' );
367
368     #
369     # $schema-> drop_view
370     #
371     my $dropped_view = $schema->drop_view($view->name);
372     isa_ok($dropped_view, 'SQL::Translator::Schema::View', 'Dropped view "view1"' );
373     $schema->add_view($view);
374     my $dropped_view2 = $schema->drop_view($view);
375     isa_ok($dropped_view2, 'SQL::Translator::Schema::View', 'Dropped view "view1" by object' );
376     is($dropped_view2->name, $view->name, 'Dropped correct view "view1"');
377     my $dropped_view3 = $schema->drop_view($view->name);
378     like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant view "view1"] );
379
380     $schema->add_view($view);
381
382     #
383     # $schema->get_*
384     #
385     my $bad_table = $schema->get_table;
386     like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
387
388     $bad_table = $schema->get_table('baz');
389     like( $schema->error, qr/does not exist/i,
390         'Error on bad arg to get_table' );
391
392     my $bad_view = $schema->get_view;
393     like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
394
395     $bad_view = $schema->get_view('bar');
396     like( $schema->error, qr/does not exist/i,
397         'Error on bad arg to get_view' );
398
399     my $good_table = $schema->get_table('foo');
400     isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
401
402     my $good_view = $schema->get_view('view1');
403     isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
404     is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
405
406     #
407     # $schema->get_*s
408     #
409     my @tables = $schema->get_tables;
410     is( scalar @tables, 3, 'Found 2 tables' );
411
412     my @views = $schema->get_views;
413     is( scalar @views, 2, 'Found 1 view' );
414
415 }
416
417 #
418 # Test ability to introspect some values
419 #
420 {
421     my $s        =  SQL::Translator::Schema->new(
422         name     => 'foo',
423         database => 'PostgreSQL',
424     );
425     my $t = $s->add_table( name => 'person' ) or warn $s->error;
426     my $f = $t->add_field( name => 'person_id' ) or warn $t->error;
427     $f->data_type('serial');
428
429     my $c          = $t->add_constraint(
430         type       => PRIMARY_KEY,
431         fields     => 'person_id',
432     ) or warn $t->error;
433
434     is( $f->is_primary_key, 1, 'Field is PK' );
435     is( $f->is_auto_increment, 1, 'Field is auto inc' );
436 }
437
438 #
439 # FK constraint validity
440 #
441 {
442     my $s = SQL::Translator::Schema->new;
443     my $t = $s->add_table( name => 'person' ) or warn $s->error;
444     my $c = $t->add_constraint or warn $t->error;
445
446     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
447     like( $c->error, qr/no type/i, '...because it has no type' );
448
449     is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
450
451     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
452     like( $c->error, qr/no fields/i, '...because it has no fields' );
453
454     is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
455
456     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
457     like( $c->error, qr/non-existent field/i,
458         q[...because field "foo" doesn't exist] );
459
460     my $fk = $t->add_field( name => 'pet_id' );
461     is( $fk->name, 'pet_id', 'Added field "pet_id"' );
462     is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
463
464     $t->add_field( name => 'f1' );
465     $t->add_field( name => 'f2' );
466     is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
467     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
468     like( $c->error, qr/only one field/i,
469         q[...because too many fields for FK] );
470
471     $c->fields('f1');
472
473     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
474     like( $c->error, qr/no reference table/i,
475         q[...because there's no reference table] );
476
477     is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
478     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
479     like( $c->error, qr/no table named/i,
480         q[...because reference table "foo" doesn't exist] );
481
482     my $t2 = $s->add_table( name => 'pet' );
483     is( $t2->name, 'pet', 'Added "pet" table' );
484
485     is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
486
487     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
488     like( $c->error, qr/no reference fields/i,
489         q[...because there're no reference fields]);
490
491     is( join('', $c->reference_fields('pet_id')), 'pet_id',
492         'Reference fields = "pet_id"' );
493
494     is( $c->is_valid, undef, 'Constraint on "person" not valid...');
495     like( $c->error, qr/non-existent field/i,
496         q[...because there's no "pet_id" field in "pet"]);
497
498     my $pet_id = $t2->add_field( name => 'pet_id' );
499     is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
500
501     is( $c->is_valid, 1, 'Constraint now valid' );
502 }
503
504 #
505 # $table->primary_key test
506 #
507 {
508     my $s = SQL::Translator::Schema->new;
509     my $t = $s->add_table( name => 'person' );
510
511     is( $t->primary_key, undef, 'No primary key' );
512
513     is( $t->primary_key('person_id'), undef,
514             q[Can't make PK on "person_id"...] );
515     like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
516
517     $t->add_field( name => 'person_id' );
518     my $c = $t->primary_key('person_id');
519
520     isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' );
521
522     is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
523
524     $t->add_field( name => 'name' );
525     $c = $t->primary_key('name');
526     is( join(',', $c->fields), 'person_id,name',
527         'Constraint now on "person_id" and "name"' );
528
529     is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
530 }
531
532 #
533 # FK finds PK
534 #
535 {
536     my $s  = SQL::Translator::Schema->new;
537     my $t1 = $s->add_table( name => 'person' );
538     my $t2 = $s->add_table( name => 'pet' );
539     $t1->add_field( name => 'id' );
540     my $c1 = $t1->primary_key( 'id' ) or warn $t1->error;
541     is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
542
543     $t2->add_field( name => 'person_id' );
544     my $c2 = $t2->add_constraint(
545         type            => PRIMARY_KEY,
546         fields          => 'person_id',
547         reference_table => 'person',
548     );
549
550     is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
551 }
552
553 #
554 # View
555 #
556 {
557     my $s      = SQL::Translator::Schema->new( name => 'ViewTest' );
558     my $name   = 'foo_view';
559     my $sql    = 'select name, age from person';
560     my $fields = 'name, age';
561     my $v      = $s->add_view(
562         name   => $name,
563         sql    => $sql,
564         fields => $fields,
565         schema => $s,
566     );
567
568     isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
569     isa_ok( $v->schema, 'SQL::Translator::Schema', 'Schema' );
570     is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
571     is( $v->name, $name, qq[Name is "$name"] );
572     is( $v->sql, $sql, qq[Name is "$sql"] );
573     is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );
574
575     my @views = $s->get_views;
576     is( scalar @views, 1, 'Number of views is 1' );
577
578     my $v1 = $s->get_view( $name );
579     isa_ok( $v1, 'SQL::Translator::Schema::View', 'View' );
580     is( $v1->name, $name, qq[Name is "$name"] );
581 }
582
583 #
584 # Trigger
585 #
586 {
587     my $s                   = SQL::Translator::Schema->new(name => 'TrigTest');
588     $s->add_table(name=>'foo') or die "Couldn't create table: ", $s->error;
589     my $name                = 'foo_trigger';
590     my $perform_action_when = 'after';
591     my $database_events     = 'insert';
592     my $on_table            = 'foo';
593     my $action              = 'update modified=timestamp();';
594     my $t                   = $s->add_trigger(
595         name                => $name,
596         perform_action_when => $perform_action_when,
597         database_events     => $database_events,
598         on_table            => $on_table,
599         action              => $action,
600     ) or die $s->error;
601
602     isa_ok( $t, 'SQL::Translator::Schema::Trigger', 'Trigger' );
603     isa_ok( $t->schema, 'SQL::Translator::Schema', 'Schema' );
604     is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
605     is( $t->name, $name, qq[Name is "$name"] );
606     is( $t->perform_action_when, $perform_action_when,
607         qq[Perform action when is "$perform_action_when"] );
608     is( join(',', $t->database_events), $database_events,
609         qq[Database event is "$database_events"] );
610     isa_ok( $t->table, 'SQL::Translator::Schema::Table', qq[table is a Table"] );
611     is( $t->action, $action, qq[Action is "$action"] );
612
613     my @triggs = $s->get_triggers;
614     is( scalar @triggs, 1, 'Number of triggers is 1' );
615
616     my $t1 = $s->get_trigger( $name );
617     isa_ok( $t1, 'SQL::Translator::Schema::Trigger', 'Trigger' );
618     is( $t1->name, $name, qq[Name is "$name"] );
619
620
621
622     my $s2                   = SQL::Translator::Schema->new(name => 'TrigTest2');
623     $s2->add_table(name=>'foo') or die "Couldn't create table: ", $s2->error;
624     my $t2                   = $s2->add_trigger(
625         name                => 'foo_trigger',
626         perform_action_when => 'after',
627         database_events     => [qw/insert update/],
628         on_table            => 'foo',
629         action              => 'update modified=timestamp();',
630     ) or die $s2->error;
631     isa_ok( $t2, 'SQL::Translator::Schema::Trigger', 'Trigger' );
632     isa_ok( $t2->schema, 'SQL::Translator::Schema', 'Schema' );
633     is( $t2->schema->name, 'TrigTest2', qq[Schema name is "'TrigTest2'"] );
634     is( $t2->name, 'foo_trigger', qq[Name is "foo_trigger"] );
635     is_deeply(
636         [$t2->database_events],
637         [qw/insert update/],
638         "Database events are [qw/insert update/] "
639     );
640     isa_ok($t2->database_events,'ARRAY','Database events');
641
642     #
643     # Trigger equal tests
644     #
645     isnt(
646         $t1->equals($t2),
647         1,
648         'Compare two Triggers with database_event and database_events'
649     );
650
651     $t1->database_events($database_events);
652     $t2->database_events($database_events);
653     is($t1->equals($t2),1,'Compare two Triggers with database_event');
654
655     $t2->database_events('');
656     $t1->database_events([qw/update insert/]);
657     $t2->database_events([qw/insert update/]);
658     is($t1->equals($t2),1,'Compare two Triggers with database_events');
659
660     #
661     # $schema-> drop_trigger
662     #
663     my $dropped_trig = $s->drop_trigger($t->name);
664     isa_ok($dropped_trig, 'SQL::Translator::Schema::Trigger', 'Dropped trigger "foo_trigger"' );
665     $s->add_trigger($t);
666     my $dropped_trig2 = $s->drop_trigger($t);
667     isa_ok($dropped_trig2, 'SQL::Translator::Schema::Trigger', 'Dropped trigger "foo_trigger" by object' );
668     is($dropped_trig2->name, $t->name, 'Dropped correct trigger "foo_trigger"');
669     my $dropped_trig3 = $s->drop_trigger($t->name);
670     like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant trigger "foo_trigger"] );
671
672     $s->add_trigger($t);
673 }
674
675 #
676 # Procedure
677 #
678 {
679     my $s          = SQL::Translator::Schema->new( name => 'ProcTest' );
680     my $name       = 'foo_proc';
681     my $sql        = 'select foo from bar';
682     my $parameters = 'foo, bar';
683     my $owner      = 'Nomar';
684     my $comments   = 'Go Sox!';
685     my $p          = $s->add_procedure(
686         name       => $name,
687         sql        => $sql,
688         parameters => $parameters,
689         owner      => $owner,
690         comments   => $comments,
691     ) or die $s->error;
692
693     isa_ok( $p, 'SQL::Translator::Schema::Procedure', 'Procedure' );
694     isa_ok( $p->schema, 'SQL::Translator::Schema', 'Schema' );
695     is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
696     is( $p->name, $name, qq[Name is "$name"] );
697     is( $p->sql, $sql, qq[SQL is "$sql"] );
698     is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
699     is( $p->comments, $comments, qq[Comments = "$comments"] );
700
701     my @procs = $s->get_procedures;
702     is( scalar @procs, 1, 'Number of procedures is 1' );
703
704     my $p1 = $s->get_procedure( $name );
705     isa_ok( $p1, 'SQL::Translator::Schema::Procedure', 'Procedure' );
706     is( $p1->name, $name, qq[Name is "$name"] );
707
708     #
709     # $schema-> drop_procedure
710     #
711     my $dropped_proc = $s->drop_procedure($p->name);
712     isa_ok($dropped_proc, 'SQL::Translator::Schema::Procedure', 'Dropped procedure "foo_proc"' );
713     $s->add_procedure($p);
714     my $dropped_proc2 = $s->drop_procedure($p);
715     isa_ok($dropped_proc2, 'SQL::Translator::Schema::Procedure', 'Dropped procedure "foo_proc" by object' );
716     is($dropped_proc2->name, $p->name, 'Dropped correct procedure "foo_proc"');
717     my $dropped_proc3 = $s->drop_procedure($p->name);
718     like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant procedure "foo_proc"] );
719
720     $s->add_procedure($p);
721 }
722
723 #
724 # Test field order
725 #
726 {
727     my $s  = SQL::Translator::Schema->new;
728     my $t  = $s->add_table( name => 'person'          );
729     my $f3 = $t->add_field( name => 'age', order => 3 );
730     my $f1 = $t->add_field( name => 'person_id', order => 1 );
731     my $f2 = $t->add_field( name => 'name', order => 2 );
732     my $f4 = $t->add_field( name => 'gender' );
733     my $f5 = $t->add_field( name => 'alias' );
734
735     is( $f1->order, 1, 'Field order is passed, order is 1' );
736     is( $f2->order, 2, 'Field order is passed, order is 2' );
737     is( $f3->order, 3, 'Field order is passed, order is 3' );
738     is( $f4->order, 4, 'Field order is not passed, order is 4' );
739     is( $f5->order, 5, 'Field order is not passed, order is 5' );
740
741     my $t2  = $s->add_table( name => 'place'                );
742     $f2 = $t2->add_field( name => 'name', order => 2 );
743
744     throws_ok { my $f22 = $t2->add_field( name => 'name2', order => 2 ) }
745         qr/\QRequested order '2' for column 'name2' conflicts with already existing column 'name'/;
746
747     throws_ok { $f1 = $t2->add_field( name => 'location' ) }
748         qr/field order incomplete/;
749 }
750
751 #
752 # Test link tables
753 #
754
755 {
756     my $s = SQL::Translator::Schema->new;
757     my $t1 = $s->add_table( name => 'person' );
758     $t1->add_field( name => 'id' );
759     $t1->primary_key( 'id' );
760     $t1->add_field( name => 'name' );
761
762     ok( $t1->is_data, 'Person table has data' );
763     ok( !$t1->is_trivial_link, 'Person table is not trivial' );
764
765     my $t2 = $s->add_table( name => 'pet' );
766     $t2->add_field( name => 'id' );
767     $t2->primary_key( 'id' );
768     $t2->add_field( name => 'name' );
769
770     ok( $t2->is_data, 'Pet table has data' );
771     ok( !$t1->is_trivial_link, 'Pet table is trivial' );
772
773     my $t3 = $s->add_table( name => 'person_pet' );
774     $t3->add_field( name => 'id' );
775     my $f1 = $t3->add_field( name => 'person_id' );
776     my $f2 = $t3->add_field( name => 'pet_id' );
777     $t3->primary_key( 'id' );
778
779     $t3->add_constraint(
780         type => FOREIGN_KEY,
781         fields => 'person_id',
782         reference_table => $t1,
783     );
784
785     $t3->add_constraint(
786         type => FOREIGN_KEY,
787         fields => 'pet_id',
788         reference_table => $t2,
789     );
790
791     ok( $f1->is_foreign_key, "person_id is foreign key" );
792     ok( $f2->is_foreign_key, "pet_id is foreign key" );
793
794     ok( !$t3->is_data, 'Link table has no data' );
795     ok( $t3->is_trivial_link, 'Link table is trivial' );
796     is( $t3->can_link($t1, $t2)->[0], 'one2one', 'Link table can link' );
797
798     my $t4 = $s->add_table( name => 'fans' );
799     my $f3 = $t4->add_field( name => 'fan_id' );
800     my $f4 = $t4->add_field( name => 'idol_id' );
801     $t4->primary_key( 'fan_id', 'idol_id' );
802
803     $t4->add_constraint(
804         type => FOREIGN_KEY,
805         name => 'fan_fan_fk',
806         fields => 'fan_id',
807         reference_table => $t1,
808     );
809
810     $t4->add_constraint(
811         type => FOREIGN_KEY,
812         name => 'fan_idol_fk',
813         fields => 'idol_id',
814         reference_table => $t1,
815     );
816
817     ok( $f3->is_foreign_key, "fan_id is foreign key" );
818     ok( $f4->is_foreign_key, "idol_id is foreign key" );
819
820     ok( !$t4->is_data, 'Self-link table has no data' );
821     ok( !$t4->is_trivial_link, 'Self-link table is not trivial' );
822     is( $t4->can_link($t1, $t1)->[0], 'many2many', 'Self-link table can link' );
823     ok( !$t4->can_link($t1, $t2)->[0], 'Self-link table can\'t link other' );
824 }
825
826 done_testing;