Update Trigger to insist on a valid table for on_table
[dbsrgits/SQL-Translator.git] / t / 13schema.t
CommitLineData
4d878d2f 1#!/usr/bin/perl
5dada97b 2# vim:set ft=perl:
4d878d2f 3
586fba3f 4$| = 1;
5
4d878d2f 6use strict;
0c879b5d 7use Test::More tests => 229;
07680493 8use SQL::Translator::Schema::Constants;
4d878d2f 9
10f36920 10require_ok( 'SQL::Translator' );
4d878d2f 11require_ok( 'SQL::Translator::Schema' );
12
07680493 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' );
4d878d2f 22
07680493 23 is( $schema->name, 'foo', 'Schema name is "foo"' );
24 is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' );
4d878d2f 25
07680493 26 is( $schema->database, 'MySQL', 'Schema database is "MySQL"' );
27 is( $schema->database('PostgreSQL'), 'PostgreSQL',
28 'Schema database changed to "PostgreSQL"' );
4d878d2f 29
07680493 30 is( $schema->is_valid, undef, 'Schema not valid...' );
31 like( $schema->error, qr/no tables/i, '...because there are no tables' );
4d878d2f 32
07680493 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"' );
4d878d2f 38
07680493 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"' );
4d878d2f 44
07680493 45 $schema = $bar_table->schema( $schema );
46 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema' );
4d878d2f 47
07680493 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] );
4d878d2f 52
07680493 53 my $redundant_table = $schema->add_table(name => 'foo');
54 is( $redundant_table, undef, qq[Can't create another "foo" table...] );
3166386a 55 like( $schema->error, qr/can't use table name/i,
56 '... because "foo" exists' );
07680493 57
5ab9f4fd 58 $redundant_table = $schema->add_table(name => '');
650f87eb 59 is( $redundant_table, undef, qq[Can't add an anonymous table...] );
5ab9f4fd 60 like( $schema->error, qr/No table name/i,
650f87eb 61 '... because it has no name ' );
5ab9f4fd 62
63 $redundant_table = SQL::Translator::Schema::Table->new(name => '');
650f87eb 64 is( $redundant_table, undef, qq[Can't create an anonymous table] );
5ab9f4fd 65 like( SQL::Translator::Schema::Table->error, qr/No table name/i,
650f87eb 66 '... because it has no name ' );
5ab9f4fd 67
07680493 68 #
650f87eb 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 #
07680493 81 # Table default new
82 #
83 is( $foo_table->name, 'foo', 'Table name is "foo"' );
5ab9f4fd 84 is( "$foo_table", 'foo', 'Table stringifies to "foo"' );
07680493 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
7a7e9080 91 is( $foo_table->comments, undef, 'No comments' );
3166386a 92
07680493 93 #
94 # New table with args
95 #
3166386a 96 my $person_table = $schema->add_table(
97 name => 'person',
98 comments => 'foo',
99 );
07680493 100 is( $person_table->name, 'person', 'Table name is "person"' );
101 is( $person_table->is_valid, undef, 'Table is not yet valid' );
3166386a 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"' );
07680493 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"' );
4809213f 114 is( $f1->full_name, 'person.foo', 'Field full_name is "person.foo"' );
5ab9f4fd 115 is( "$f1", 'foo', 'Field stringifies to "foo"' );
07680493 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' );
14f8758f 119 is( $f1->is_nullable, 1, 'Field can be NULL' );
07680493 120 is( $f1->default_value, undef, 'Field default is undefined' );
3166386a 121 is( $f1->comments, '', 'No comments' );
57fd48f1 122 is( $f1->table, 'person', 'Field table is person' );
123 is( $f1->schema->database, 'PostgreSQL', 'Field schema shortcut works' );
07680493 124
3166386a 125 my $f2 = SQL::Translator::Schema::Field->new (
126 name => 'f2',
127 comments => 'foo',
128 ) or warn SQL::Translator::Schema::Field->error;
07680493 129 $f2 = $person_table->add_field( $f2 );
130 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
131 is( $f2->name, 'f2', 'Add field "f2"' );
14f8758f 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' );
3166386a 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"' );
07680493 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...] );
14f8758f 149 like( $person_table->error, qr/can't use field/i,
07680493 150 '... because it exists' );
151
5ab9f4fd 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
14f8758f 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"' );
81e0fe82 167 is( join(",",$person_table->field_names), 'foo,f2',
168 'field_names is "foo,f2"' );
14f8758f 169
07680493 170 #
650f87eb 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 #
07680493 184 # Field methods
185 #
5ab9f4fd 186 is( $f1->name('person_name'), 'person_name',
07680493 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
adfdb552 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
07680493 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"' );
14f8758f 212 is( join(':', $age->size), '10:2', 'Field size returns array' );
07680493 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
14f8758f 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
07680493 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 #
650f87eb 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 #
07680493 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
481b05ff 278 $fields = join(',', $constraint1->fields('age') );
279 is( $fields, 'age', 'Constraint field = "age"' );
07680493 280
e21fb9dd 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
481b05ff 285 $fields = join(',', $constraint1->fields('age,age') );
286 is( $fields, 'age', 'Constraint field = "age"' );
07680493 287
481b05ff 288 $fields = join(',', $constraint1->fields('age', 'name') );
289 is( $fields, 'age,name', 'Constraint field = "age,name"' );
07680493 290
481b05ff 291 $fields = join(',', $constraint1->fields( 'age,name,age' ) );
292 is( $fields, 'age,name', 'Constraint field = "age,name"' );
07680493 293
481b05ff 294 $fields = join(',', $constraint1->fields( 'age, name' ) );
295 is( $fields, 'age,name', 'Constraint field = "age,name"' );
07680493 296
481b05ff 297 $fields = join(',', $constraint1->fields( [ 'age', 'name' ] ) );
298 is( $fields, 'age,name', 'Constraint field = "age,name"' );
07680493 299
481b05ff 300 $fields = join(',', $constraint1->fields( qw[ age name ] ) );
301 is( $fields, 'age,name', 'Constraint field = "age,name"' );
07680493 302
e21fb9dd 303 $fields = join(',', $constraint1->field_names );
304 is( $fields, 'age,name', 'Constraint field_names = "age,name"' );
305
14f8758f 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
07680493 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
637782f9 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
07680493 328 my $constraints = $person_table->get_constraints;
637782f9 329 is( scalar @$constraints, 3, 'Three constraints' );
07680493 330 is( $constraints->[0]->name, 'foo', '"foo" constraint' );
331 is( $constraints->[1]->name, 'bar', '"bar" constraint' );
332
333 #
650f87eb 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 #
07680493 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 #
650f87eb 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 #
07680493 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' );
10f36920 410
411}
412
413#
07680493 414# Test ability to introspect some values
4d878d2f 415#
07680493 416{
417 my $s = SQL::Translator::Schema->new(
418 name => 'foo',
419 database => 'PostgreSQL',
420 );
0c879b5d 421 my $t = $s->add_table( name => 'person' ) or warn $s->error;
07680493 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}
4d878d2f 433
434#
07680493 435# FK constraint validity
4d878d2f 436#
07680493 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' );
07680493 498}
586fba3f 499
500#
07680493 501# $table->primary_key test
586fba3f 502#
07680493 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' );
586fba3f 508
07680493 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" );
586fba3f 512
07680493 513 $t->add_field( name => 'person_id' );
514 my $c = $t->primary_key('person_id');
586fba3f 515
07680493 516 isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' );
586fba3f 517
07680493 518 is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
586fba3f 519
07680493 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}
586fba3f 527
528#
07680493 529# FK finds PK
586fba3f 530#
07680493 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 );
586fba3f 545
07680493 546 is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
547}
14f8758f 548
549#
550# View
551#
552{
22d567ca 553 my $s = SQL::Translator::Schema->new( name => 'ViewTest' );
14f8758f 554 my $name = 'foo_view';
555 my $sql = 'select name, age from person';
556 my $fields = 'name, age';
14f8758f 557 my $v = $s->add_view(
558 name => $name,
559 sql => $sql,
560 fields => $fields,
22d567ca 561 schema => $s,
14f8758f 562 );
563
564 isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
22d567ca 565 isa_ok( $v->schema, 'SQL::Translator::Schema', 'Schema' );
566 is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
14f8758f 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"] );
22d567ca 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"] );
14f8758f 577}
90fd4bf5 578
579#
580# Trigger
581#
582{
22d567ca 583 my $s = SQL::Translator::Schema->new(name => 'TrigTest');
8ce5d615 584 $s->add_table(name=>'foo') or die "Couldn't create table: ", $s->error;
90fd4bf5 585 my $name = 'foo_trigger';
586 my $perform_action_when = 'after';
587 my $database_event = 'insert';
588 my $on_table = 'foo';
589 my $action = 'update modified=timestamp();';
90fd4bf5 590 my $t = $s->add_trigger(
591 name => $name,
592 perform_action_when => $perform_action_when,
593 database_event => $database_event,
594 on_table => $on_table,
595 action => $action,
596 ) or die $s->error;
597
598 isa_ok( $t, 'SQL::Translator::Schema::Trigger', 'Trigger' );
22d567ca 599 isa_ok( $t->schema, 'SQL::Translator::Schema', 'Schema' );
600 is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
90fd4bf5 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( $t->database_event, $database_event,
605 qq[Database event is "$database_event"] );
8ce5d615 606 isa_ok( $t->table, 'SQL::Translator::Schema::Table', qq[table is a Table"] );
90fd4bf5 607 is( $t->action, $action, qq[Action is "$action"] );
22d567ca 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"] );
22d567ca 615
650f87eb 616 #
617 # $schema-> drop_trigger
618 #
619 my $dropped_trig = $s->drop_trigger($t->name);
620 isa_ok($dropped_trig, 'SQL::Translator::Schema::Trigger', 'Dropped trigger "foo_trigger"' );
621 $s->add_trigger($t);
622 my $dropped_trig2 = $s->drop_trigger($t);
623 isa_ok($dropped_trig2, 'SQL::Translator::Schema::Trigger', 'Dropped trigger "foo_trigger" by object' );
624 is($dropped_trig2->name, $t->name, 'Dropped correct trigger "foo_trigger"');
625 my $dropped_trig3 = $s->drop_trigger($t->name);
626 like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant trigger "foo_trigger"] );
627
628 $s->add_trigger($t);
629}
630
22d567ca 631#
632# Procedure
633#
634{
635 my $s = SQL::Translator::Schema->new( name => 'ProcTest' );
636 my $name = 'foo_proc';
637 my $sql = 'select foo from bar';
638 my $parameters = 'foo, bar';
639 my $owner = 'Nomar';
640 my $comments = 'Go Sox!';
641 my $p = $s->add_procedure(
642 name => $name,
643 sql => $sql,
644 parameters => $parameters,
645 owner => $owner,
646 comments => $comments,
647 ) or die $s->error;
648
649 isa_ok( $p, 'SQL::Translator::Schema::Procedure', 'Procedure' );
650 isa_ok( $p->schema, 'SQL::Translator::Schema', 'Schema' );
651 is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
652 is( $p->name, $name, qq[Name is "$name"] );
653 is( $p->sql, $sql, qq[SQL is "$sql"] );
654 is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
655 is( $p->comments, $comments, qq[Comments = "$comments"] );
656
657 my @procs = $s->get_procedures;
658 is( scalar @procs, 1, 'Number of procedures is 1' );
659
660 my $p1 = $s->get_procedure( $name );
661 isa_ok( $p1, 'SQL::Translator::Schema::Procedure', 'Procedure' );
662 is( $p1->name, $name, qq[Name is "$name"] );
650f87eb 663
664 #
665 # $schema-> drop_procedure
666 #
667 my $dropped_proc = $s->drop_procedure($p->name);
668 isa_ok($dropped_proc, 'SQL::Translator::Schema::Procedure', 'Dropped procedure "foo_proc"' );
669 $s->add_procedure($p);
670 my $dropped_proc2 = $s->drop_procedure($p);
671 isa_ok($dropped_proc2, 'SQL::Translator::Schema::Procedure', 'Dropped procedure "foo_proc" by object' );
672 is($dropped_proc2->name, $p->name, 'Dropped correct procedure "foo_proc"');
673 my $dropped_proc3 = $s->drop_procedure($p->name);
674 like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant procedure "foo_proc"] );
675
676 $s->add_procedure($p);
90fd4bf5 677}