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