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