4 use Test::More; # tests => 238;
6 use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants);
8 require_ok( 'SQL::Translator' );
9 require_ok( 'SQL::Translator::Object::Column' );
10 require_ok( 'SQL::Translator::Object::Constraint' );
11 require_ok( 'SQL::Translator::Object::Index' );
12 require_ok( 'SQL::Translator::Object::Procedure' );
13 require_ok( 'SQL::Translator::Object::Schema' );
14 require_ok( 'SQL::Translator::Object::Table' );
15 require_ok( 'SQL::Translator::Object::Trigger' );
16 require_ok( 'SQL::Translator::Object::View' );
22 my $schema = SQL::Translator::Object::Schema->new(
26 isa_ok( $schema, 'SQL::Translator::Object::Schema' );
28 is( $schema->name, 'foo', 'Schema name is "foo"' );
29 is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' );
31 is( $schema->database, 'MySQL', 'Schema database is "MySQL"' );
32 is( $schema->database('PostgreSQL'), 'PostgreSQL',
33 'Schema database changed to "PostgreSQL"' );
35 is( $schema->is_valid, undef, 'Schema not valid...' );
36 # like( $schema->error, qr/no tables/i, '...because there are no tables' );
41 my $foo_table = SQL::Translator::Object::Table->new({ name => 'foo' });
42 $schema->add_table($foo_table) or warn $schema->error;
43 isa_ok( $foo_table, 'SQL::Translator::Object::Table', 'Table "foo"' );
45 my $bar_table = SQL::Translator::Object::Table->new({ name => 'bar' }) or
46 warn SQL::Translator::Object::Table->error;
47 $schema->add_table( $bar_table );
48 isa_ok( $bar_table, 'SQL::Translator::Object::Table', 'Table "bar"' );
49 is( $bar_table->name, 'bar', 'Add table "bar"' );
51 $schema = $bar_table->schema( $schema );
52 isa_ok( $schema, 'SQL::Translator::Object::Schema', 'Schema' );
54 dies_ok(sub { $bar_table->name('foo') }, '...because "foo" exists');
56 # is( $bar_table->name('foo'), undef,
57 # q[Can't change name of table "bar" to "foo"...]);
58 # like( $bar_table->error, qr/can't use table name/i,
59 # q[...because "foo" exists] );
61 my $redundant_table = new SQL::Translator::Object::Table({ name => 'foo' });
62 dies_ok(sub { $schema->add_table($redundant_table) }, '...because "foo" exists"');
63 #is( $redundant_table, undef, qq[Can't create another "foo" table...] );
64 #like( $schema->error, qr/can't use table name/i,
65 # '... because "foo" exists' );
67 $redundant_table = new SQL::Translator::Object::Table({ name => '' });
68 # is( $redundant_table, undef, qq[Can't add an anonymous table...] );
69 # like( $schema->error, qr/No table name/i,
70 # '... because it has no name ' );
71 dies_ok(sub { $schema->add_table($redundant_table) }, '...because it has no name');
73 $redundant_table = SQL::Translator::Object::Table->new({ name => '' });
74 #is( $redundant_table, undef, qq[Can't create an anonymous table] );
75 #like( SQL::Translator::Object::Table->error, qr/No table name/i,
76 # '... because it has no name ' );
77 dies_ok(sub { $schema->add_table($redundant_table) }, '...because it has no name');
80 # $schema-> drop_table
82 my $dropped_table = $schema->drop_table($foo_table->name, cascade => 1);
83 isa_ok($dropped_table, 'SQL::Translator::Object::Table', 'Dropped table "foo"' );
84 $schema->add_table($foo_table);
85 my $dropped_table2 = $schema->drop_table($foo_table, cascade => 1);
86 isa_ok($dropped_table2, 'SQL::Translator::Object::Table', 'Dropped table "foo" by object' );
87 dies_ok(sub { my $dropped_table3 = $schema->drop_table($foo_table->name, cascade => 1) }, qq[Can't drop non-existant table "foo"]);
88 # my $dropped_table3 = $schema->drop_table($foo_table->name, cascade => 1);
89 # like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant table "foo"] );
91 $schema->add_table($foo_table);
95 is( $foo_table->name, 'foo', 'Table name is "foo"' );
96 is( "$foo_table", 'foo', 'Table stringifies to "foo"' );
97 is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
99 my $fields = $foo_table->get_fields;
100 is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
101 #like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
103 is( $foo_table->comments, '', 'No comments' );
106 # New table with args
108 my $person_table = SQL::Translator::Object::Table->new({
112 $person_table->comments('foo');
113 $schema->add_table($person_table);
114 is( $person_table->name, 'person', 'Table name is "person"' );
115 is( $person_table->is_valid, undef, 'Table is not yet valid' );
116 is( $person_table->comments, 'foo', 'Comments = "foo"' );
117 is( join(',', $person_table->comments('bar')), 'foo,bar', 'Table comments = "foo,bar"' );
118 is( $person_table->comments, "foo\nbar", 'Table comments = "foo,bar"' );
123 my $person_table_column = SQL::Translator::Object::Column->new( name => 'foo' );
124 my $f1 = $person_table->add_column($person_table_column);
125 isa_ok( $f1, 'SQL::Translator::Object::Column', 'Column' );
126 is( $f1->name, 'foo', 'Field name is "foo"' );
127 is( $f1->full_name, 'person.foo', 'Field full_name is "person.foo"' );
128 is( "$f1", 'foo', 'Field stringifies to "foo"' );
129 is( $f1->data_type, '', 'Field data type is blank' );
130 is( $f1->size, 0, 'Field size is "0"' );
131 is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
132 is( $f1->is_nullable, 1, 'Field can be NULL' );
133 is( $f1->default_value, undef, 'Field default is undefined' );
134 is( $f1->comments, '', 'No comments' );
135 is( $f1->table, 'person', 'Field table is person' );
136 is( $f1->schema->database, 'PostgreSQL', 'Field schema shortcut works' );
138 my $f2 = SQL::Translator::Object::Column->new ({ name => 'f2', });
139 $f2->comments('foo');
140 $f2 = $person_table->add_column( $f2 );
141 isa_ok( $f1, 'SQL::Translator::Object::Column', 'f2' );
142 is( $f2->name, 'f2', 'Add field "f2"' );
143 is( $f2->is_nullable(0), 0, 'Field cannot be NULL' );
144 # is( $f2->is_nullable(''), 0, 'Field cannot be NULL' );
145 is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' );
146 is( $f2->default_value(''), '', 'Field default is empty string' );
147 is( $f2->comments, 'foo', 'Field comment = "foo"' );
148 is( join(',', $f2->comments('bar')), 'foo,bar', 'Field comment = "foo,bar"' );
149 is( $f2->comments, "foo\nbar", 'Field comment = "foo,bar"' );
151 $person_table = $f2->table( $person_table );
152 isa_ok( $person_table, 'SQL::Translator::Object::Table', 'person_table' );
154 #is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] );
155 dies_ok(sub { $f2->name('foo') }, q[Can't set field name of "f2" to "foo"...] . '...because name exists');
156 #like( $f2->error, qr/can't use field name/i, '...because name exists' );
158 # my $redundant_field = $person_table->add_field(name => 'f2');
159 # is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
160 # like( $person_table->error, qr/can't use field/i,
161 # '... because it exists' );
162 dies_ok(sub { my $redundant_field = $person_table->add_column({ name => 'f2' }); }, qq[Didn't create another "f2" field...] . '... because it exists' );
164 # my $redundant_field = $person_table->add_column({ name => '' });
165 # is( $redundant_field, undef, qq[Didn't add a "" field...] );
166 # like( $person_table->error, qr/No field name/i,
167 # '... because it has no name' );
168 dies_ok(sub { my $redundant_field = $person_table->add_column({ name => '' }) }, qq[Didn't add a "" field...] . '... because it has no name' );
170 # my $redundant_field = SQL::Translator::Object::Column->new(name => '');
171 # is( $redundant_field, undef, qq[Didn't create a "" field...] );
172 # like( SQL::Translator::Object::Column->error, qr/No field name/i,
173 # '... because it has no name' );
174 dies_ok(sub { my $redundant_field = SQL::Translator::Object::Column->new({ name => '' }) }, qq[Didn't create a "" field...] . '... because it has no name' );
176 my @fields = $person_table->get_fields;
177 is( scalar @fields, 2, 'Table "foo" has 2 fields' );
179 is( $fields[0]->name, 'foo', 'First field is "foo"' );
180 is( $fields[1]->name, 'f2', 'Second field is "f2"' );
181 is( join(",",$person_table->fields), 'foo,f2', 'field_names is "foo,f2"' );
184 # $table-> drop_column
186 my $dropped_field = $person_table->drop_column($f2->name, cascade => 1);
187 isa_ok($dropped_field, 'SQL::Translator::Object::Column', 'Dropped field "f2"' );
188 $person_table->add_column($f2);
189 my $dropped_field2 = $person_table->drop_column($f2, cascade => 1);
190 isa_ok($dropped_field2, 'SQL::Translator::Object::Column', 'Dropped field "f2" by object' );
191 # my $dropped_field3 = $person_table->drop_column($f2->name, cascade => 1);
192 # like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant field "f2"] );
193 dies_ok(sub { $person_table->drop_column($f2->name, cascade => 1); }, qq[Can't drop non-existant field "f2"]);
195 $person_table->add_field($f2);
200 is( $f1->name('person_name'), 'person_name', 'Field name is "person_name"' );
201 is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
202 is( $f1->size('30'), '30', 'Field size is "30"' );
203 is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
205 $f1->extra({ foo => 'bar' });
206 $f1->extra({ baz => 'quux' });
207 my %extra = $f1->extra;
208 is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' );
209 is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' );
212 # New field with args
214 my $age = $person_table->add_field({
216 data_type => 'float',
220 is( $age->name, 'age', 'Field name is "age"' );
221 is( $age->data_type, 'float', 'Field data type is "float"' );
222 is( $age->size, '10,2', 'Field size is "10,2"' );
223 is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
224 is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
225 is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
226 is( join(':', $age->size), '10:2', 'Field size returns array' );
231 my @indices = $person_table->get_indices;
232 is( scalar @indices, 0, 'No indices' );
233 #like( $person_table->error, qr/no indices/i, 'Error for no indices' );
234 # my $index1 = $person_table->add_index(SQL::Translator::Object::Index->new( name => "foo" )) ;
235 my $index1 = $person_table->add_index({ name => "foo" });
236 isa_ok( $index1, 'SQL::Translator::Object::Index', 'Index' );
237 is( $index1->name, 'foo', 'Index name is "foo"' );
239 is( $index1->is_valid, undef, 'Index name is not valid...' );
240 # like( $index1->error, qr/no fields/i, '...because it has no fields' );
242 # is( join(':', $index1->fields('foo,bar')), 'foo:bar', 'Index accepts fields');
244 # is( $index1->is_valid, undef, 'Index name is not valid...' );
245 # like( $index1->error, qr/does not exist in table/i, '...because it used fields not in the table' );
246 dies_ok(sub { $index1->fields('foo,bar') }, 'because it used fields not in the table');
248 is( join(':', $index1->fields('person_name,age')), 'person_name:age', 'Index accepts fields');
249 #is( join(':', $index1->fields(qw/foo age/)), 'foo:age', 'Index accepts fields');
250 is( $index1->is_valid, 1, 'Index name is now valid' );
252 is( $index1->type, NORMAL, 'Index type is "normal"' );
254 my $index2 = SQL::Translator::Object::Index->new( name => "bar" );
256 $index2 = $person_table->add_index( $index2 );
257 isa_ok( $index2, 'SQL::Translator::Object::Index', 'Index' );
258 is( $index2->name, 'bar', 'Index name is "bar"' );
260 @indices = $person_table->get_indices;
261 is( scalar @indices, 2, 'Two indices' );
262 is( $indices[0]->name, 'foo', '"foo" index' );
263 is( $indices[1]->name, 'bar', '"bar" index' );
266 # $table-> drop_index
268 my $dropped_index = $person_table->drop_index($index1->name);
269 isa_ok($dropped_index, 'SQL::Translator::Object::Index', 'Dropped index "foo"' );
270 $person_table->add_index($index1);
271 my $dropped_index2 = $person_table->drop_index($index1);
272 isa_ok($dropped_index2, 'SQL::Translator::Object::Index', 'Dropped index "foo" by object' );
273 is($dropped_index2->name, $index1->name, 'Dropped correct index "foo"');
274 # my $dropped_index3 = $person_table->drop_index($index1->name);
275 # like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant index "foo"] );
276 dies_ok(sub { $person_table->drop_index($index1->name) }, qq[Can't drop non-existant index "foo"] );
278 $person_table->add_index($index1);
283 my @constraints = $person_table->get_constraints;
284 is( scalar @constraints, 0, 'No constraints' );
285 #like( $person_table->error, qr/no constraints/i, 'Error for no constraints' );
286 my $constraint1 = $person_table->add_constraint({ name => 'foo', type => 'bar' });
288 isa_ok( $constraint1, 'SQL::Translator::Object::Constraint', 'Constraint' );
289 is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
291 $fields = join(',', $constraint1->fields('age') );
292 is( $fields, 'age', 'Constraint field = "age"' );
294 $fields = $constraint1->fields;
295 ok( ref $fields[0] && $fields[0]->isa("SQL::Translator::Object::Column"),
296 'Constraint fields returns a SQL::Translator::Object::Column' );
298 $fields = join(',', $constraint1->fields('age,age') );
299 is( $fields, 'age', 'Constraint field = "age"' );
301 # $fields = join(',', $constraint1->fields('age', 'name') );
302 # is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
304 $fields = join(',', $constraint1->fields( 'age,person_name,age' ) );
305 is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
307 $fields = join(',', $constraint1->fields( 'age, person_name' ) );
308 is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
310 $fields = join(',', $constraint1->fields( [ 'age', 'person_name' ] ) );
311 is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
313 # $fields = join(',', $constraint1->fields( qw[ age person_name ] ) );
314 # is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
316 $fields = join(',', $constraint1->field_names );
317 is( $fields, 'age,person_name', 'Constraint field_names = "age,name"' );
319 is( $constraint1->match_type, '', 'Constraint match type is empty' );
320 # is( $constraint1->match_type('foo'), undef,
321 # 'Constraint match type rejects bad arg...' );
322 # like( $constraint1->error, qr/invalid match type/i,
323 # '...because it is invalid');
324 dies_ok(sub { $constraint1->match_type('foo') }, '...because it is invalid');
325 is( $constraint1->match_type('FULL'), 'full',
326 'Constraint match type = "full"' );
328 my $constraint2 = SQL::Translator::Object::Constraint->new( name => 'bar', type => 'baz' );
329 $constraint2 = $person_table->add_constraint( $constraint2 );
330 isa_ok( $constraint2, 'SQL::Translator::Object::Constraint', 'Constraint' );
331 is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
333 my $constraint3 = $person_table->add_constraint({
335 expression => 'foo bar',
336 }); # or die $person_table->error;
337 isa_ok( $constraint3, 'SQL::Translator::Object::Constraint', 'Constraint' );
338 is( $constraint3->type, 'check', 'Constraint type is "CHECK"' ); ## CHECK_C
339 is( $constraint3->expression, 'foo bar', 'Constraint expression is "foo bar"' );
341 @constraints = $person_table->get_constraints;
342 is( scalar @constraints, 3, 'Three constraints' );
343 is( $constraints[0]->name, 'foo', '"foo" constraint' );
344 is( $constraints[1]->name, 'bar', '"bar" constraint' );
347 # $table-> drop_constraint
349 my $dropped_con = $person_table->drop_constraint($constraint1->name);
350 isa_ok($dropped_con, 'SQL::Translator::Object::Constraint', 'Dropped constraint "foo"' );
351 $person_table->add_constraint($constraint1);
352 my $dropped_con2 = $person_table->drop_constraint($constraint1);
353 isa_ok($dropped_con2, 'SQL::Translator::Object::Constraint', 'Dropped constraint "foo" by object' );
354 is($dropped_con2->name, $constraint1->name, 'Dropped correct constraint "foo"');
355 # my $dropped_con3 = $person_table->drop_constraint($constraint1->name);
356 # like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant constraint "foo"] );
357 dies_ok(sub { $person_table->drop_constraint($constraint1->name) }, qq[Can't drop non-existant constraint "foo"] );
359 $person_table->add_constraint($constraint1);
364 my $view = $schema->add_view({ name => 'view1' }) or warn $schema->error;
365 isa_ok( $view, 'SQL::Translator::Object::View', 'View' );
366 my $view_sql = 'select * from table';
367 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
369 my $view2 = SQL::Translator::Object::View->new(name => 'view2') or
370 warn SQL::Translator::Object::View->error;
371 my $check_view = $schema->add_view( $view2 );
372 is( $check_view->name, 'view2', 'Add view "view2"' );
374 dies_ok(sub { my $redundant_view = $schema->add_view({ name => 'view2' }) }, '... because it exists');
375 # my $redundant_view = $schema->add_view({ name => 'view2' });
376 # is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
377 # like( $schema->error, qr/can't create view/i, '... because it exists' );
380 # $schema-> drop_view
382 my $dropped_view = $schema->drop_view($view->name);
383 isa_ok($dropped_view, 'SQL::Translator::Object::View', 'Dropped view "view1"' );
384 $schema->add_view($view);
385 my $dropped_view2 = $schema->drop_view($view);
386 isa_ok($dropped_view2, 'SQL::Translator::Object::View', 'Dropped view "view1" by object' );
387 is($dropped_view2->name, $view->name, 'Dropped correct view "view1"');
388 # my $dropped_view3 = $schema->drop_view($view->name);
389 # like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant view "view1"] );
390 dies_ok(sub { my $dropped_view3 = $schema->drop_view($view->name) }, qq[Can't drop non-existant view "view1"] );
392 $schema->add_view($view);
399 local $TODO = 'patch Moose Native::Trait';
400 dies_ok( sub { my $bad_table = $schema->get_table }, 'Error on no arg to get_table' );
401 is($schema->get_table('baz'), undef, 'Nonexistant table returns undef');
403 dies_ok( sub { my $bad_view = $schema->get_view }, 'Error on no arg to get_view' );
404 is($schema->get_view('baz'), undef, 'Nonexistant view returns undef');
407 my $good_table = $schema->get_table('foo');
408 isa_ok( $good_table, 'SQL::Translator::Object::Table', 'Table "foo"' );
410 my $good_view = $schema->get_view('view1');
411 isa_ok( $good_view, 'SQL::Translator::Object::View', 'View "view1"' );
412 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
417 my @tables = $schema->get_tables;
418 is( scalar @tables, 3, 'Found 2 tables' );
420 my @views = $schema->get_views;
421 is( scalar @views, 2, 'Found 1 view' );
426 # Test ability to introspect some values
429 my $s = SQL::Translator::Object::Schema->new(
431 database => 'PostgreSQL',
433 my $t = $s->add_table({ name => 'person' }) or warn $s->error;
434 my $f = $t->add_field({ name => 'person_id' }) or warn $t->error;
435 $f->data_type('serial');
437 my $c = $t->add_constraint({
439 # fields => 'person_id',
440 }) or warn $t->error;
443 is( $f->is_primary_key, 1, 'Field is PK' );
444 is( $f->is_auto_increment, 1, 'Field is auto inc' );
448 # FK constraint validity
451 my $s = SQL::Translator::Object::Schema->new;
452 my $t = $s->add_table({ name => 'person' });
453 # my $c = $t->add_constraint({ });
455 dies_ok( sub { my $c = $t->add_constraint({ }) }, '...because it has no type' );
457 # is( $c->is_valid, undef, 'Constraint on "person" not valid...');
458 # like( $c->error, qr/no type/i, '...because it has no type' );
460 # is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
462 # is( $c->is_valid, undef, 'Constraint on "person" not valid...');
463 # like( $c->error, qr/no fields/i, '...because it has no fields' );
464 # dies_ok( sub { my $c = $t->add_constraint({ type => FOREIGN_KEY }) }, '...because it has no fields' );
466 # is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
467 dies_ok( sub { my $c = $t->add_constraint({ type => FOREIGN_KEY, fields => 'foo' }) }, q[...because field "foo" doesn't exist] );
469 # is( $c->is_valid, undef, 'Constraint on "person" not valid...');
470 # like( $c->error, qr/non-existent field/i, q[...because field "foo" doesn't exist] );
472 my $fk = $t->add_field({ name => 'pet_id' });
473 is( $fk->name, 'pet_id', 'Added field "pet_id"' );
474 # is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
476 $t->add_field({ name => 'f1' });
477 $t->add_field({ name => 'f2' });
478 # is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
479 # is( $c->is_valid, undef, 'Constraint on "person" not valid...');
480 # like( $c->error, qr/only one field/i,
481 # q[...because too many fields for FK] );
485 # is( $c->is_valid, undef, 'Constraint on "person" not valid...');
486 # like( $c->error, qr/no reference table/i,
487 # q[...because there's no reference table] );
489 # is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
490 # is( $c->is_valid, undef, 'Constraint on "person" not valid...');
491 # like( $c->error, qr/no table named/i,
492 # q[...because reference table "foo" doesn't exist] );
494 dies_ok( sub { my $c = $t->add_constraint({ type => FOREIGN_KEY, fields => [ 'f1', 'f2' ], reference_table => 'foo'}) }, "...because reference table foo doesn't exist" );
496 my $t2 = $s->add_table({ name => 'pet' });
497 is( $t2->name, 'pet', 'Added "pet" table' );
499 dies_ok( sub { my $c = $t->add_constraint({ type => FOREIGN_KEY, reference_table => 'pet' }) }, '...because there are no reference fields' );
501 # is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
503 # is( $c->is_valid, undef, 'Constraint on "person" not valid...');
504 #like( $c->error, qr/no reference fields/i,
505 # q[...because there're no reference fields]);
507 ## is( join('', $c->reference_fields('pet_id')), 'pet_id',
508 ## 'Reference fields = "pet_id"' );
510 ## is( $c->is_valid, undef, 'Constraint on "person" not valid...');
511 #like( $c->error, qr/non-existent field/i,
512 # q[...because there's no "pet_id" field in "pet"]);
514 my $pet_id = $t2->add_field({ name => 'pet_id' });
515 is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
517 ## is( $c->is_valid, 1, 'Constraint now valid' );
521 # $table->primary_key test
524 my $s = SQL::Translator::Object::Schema->new;
525 my $t = $s->add_table({ name => 'person' });
527 is( $t->primary_key, undef, 'No primary key' );
529 # is( $t->primary_key('person_id'), undef,
530 # q[Can't make PK on "person_id"...] );
531 # like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
532 dies_ok( sub { $t->primary_key }, "...because it doesn't exist" );
534 $t->add_field({ name => 'person_id' });
535 my $c = $t->primary_key('person_id');
537 isa_ok( $c, 'SQL::Translator::Object::Constraint', 'Constraint' );
539 is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
541 $t->add_field({ name => 'name' });
542 $c = $t->primary_key('name');
543 is( join(',', $c->fields), 'person_id,name',
544 'Constraint now on "person_id" and "name"' );
546 #is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
547 is( scalar $t->get_constraints, 1, 'Found 1 constraint' );
554 my $s = SQL::Translator::Object::Schema->new;
555 my $t1 = $s->add_table({ name => 'person' });
556 my $t2 = $s->add_table({ name => 'pet' });
557 $t1->add_field({ name => 'id' });
558 my $c1 = $t1->primary_key( 'id' );
559 is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
561 $t2->add_field({ name => 'person_id' });
562 my $c2 = $t2->add_constraint({
564 fields => 'person_id',
565 reference_table => 'person',
567 reference_fields => 'id',
570 is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
577 my $s = SQL::Translator::Object::Schema->new({ name => 'ViewTest' });
578 my $name = 'foo_view';
579 my $sql = 'select name, age from person';
580 my $fields = 'name, age';
581 my $v = $s->add_view({
585 # columns => $fields,
589 # $v->add_field({ name => 'name' });
590 # $v->add_field({ name => 'age' });
592 isa_ok( $v, 'SQL::Translator::Object::View', 'View' );
593 isa_ok( $v->schema, 'SQL::Translator::Object::Schema', 'Schema' );
594 is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
595 is( $v->name, $name, qq[Name is "$name"] );
596 is( $v->sql, $sql, qq[Name is "$sql"] );
597 is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );
599 my @views = $s->get_views;
600 is( scalar @views, 1, 'Number of views is 1' );
602 my $v1 = $s->get_view( $name );
603 isa_ok( $v1, 'SQL::Translator::Object::View', 'View' );
604 is( $v1->name, $name, qq[Name is "$name"] );
611 my $s = SQL::Translator::Object::Schema->new(name => 'TrigTest');
612 $s->add_table({ name => 'foo' }) or die "Couldn't create table: ", $s->error;
613 my $name = 'foo_trigger';
614 my $perform_action_when = 'after';
615 my $database_events = 'insert';
616 my $on_table = 'foo';
617 my $action = 'update modified=timestamp();';
618 my $t = $s->add_trigger({
620 perform_action_when => $perform_action_when,
621 database_events => $database_events,
622 on_table => $on_table,
623 table => $s->get_table('foo'),
627 $t->add_database_event('insert');
629 isa_ok( $t, 'SQL::Translator::Object::Trigger', 'Trigger' );
630 isa_ok( $t->schema, 'SQL::Translator::Object::Schema', 'Schema' );
631 is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
632 is( $t->name, $name, qq[Name is "$name"] );
633 is( $t->perform_action_when, $perform_action_when,
634 qq[Perform action when is "$perform_action_when"] );
636 is( join(',', $t->database_events), $database_events,
637 qq[Database event is "$database_events"] );
638 isa_ok( $t->table, 'SQL::Translator::Object::Table', qq[table is a Table"] );
639 is( $t->action, $action, qq[Action is "$action"] );
641 my @triggs = $s->get_triggers;
642 is( scalar @triggs, 1, 'Number of triggers is 1' );
644 my $t1 = $s->get_trigger( $name );
645 isa_ok( $t1, 'SQL::Translator::Object::Trigger', 'Trigger' );
646 is( $t1->name, $name, qq[Name is "$name"] );
650 my $s2 = SQL::Translator::Object::Schema->new(name => 'TrigTest2');
651 $s2->add_table({ name => 'foo' }) or die "Couldn't create table: ", $s2->error;
652 my $t2 = $s2->add_trigger({
653 name => 'foo_trigger',
654 perform_action_when => 'after',
655 database_events => [qw/insert update/],
657 action => 'update modified=timestamp();',
658 }) or die $s2->error;
659 $t2->add_database_event('insert');
660 $t2->add_database_event('update');
661 isa_ok( $t2, 'SQL::Translator::Object::Trigger', 'Trigger' );
662 isa_ok( $t2->schema, 'SQL::Translator::Object::Schema', 'Schema' );
663 is( $t2->schema->name, 'TrigTest2', qq[Schema name is "'TrigTest2'"] );
664 is( $t2->name, 'foo_trigger', qq[Name is "foo_trigger"] );
666 [$t2->database_events],
668 "Database events are [qw/insert update/] "
671 isa_ok($t2->database_events,'ARRAY','Database events');
674 # Trigger equal tests
679 # 'Compare two Triggers with database_event and database_events'
682 $t1->database_events($database_events);
683 $t2->database_events($database_events);
684 # is($t1->equals($t2),1,'Compare two Triggers with database_event');
686 $t2->database_events('');
687 $t1->database_events([qw/update insert/]);
688 $t2->database_events([qw/insert update/]);
689 # is($t1->equals($t2),1,'Compare two Triggers with database_events');
692 # $schema-> drop_trigger
694 my $dropped_trig = $s->drop_trigger($t->name);
695 isa_ok($dropped_trig, 'SQL::Translator::Object::Trigger', 'Dropped trigger "foo_trigger"' );
697 my $dropped_trig2 = $s->drop_trigger($t);
698 isa_ok($dropped_trig2, 'SQL::Translator::Object::Trigger', 'Dropped trigger "foo_trigger" by object' );
699 #is($dropped_trig2->name, $t->name, 'Dropped correct trigger "foo_trigger"');
700 my $dropped_trig3 = $s->drop_trigger($t->name);
701 #like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant trigger "foo_trigger"] );
710 my $s = SQL::Translator::Object::Schema->new( name => 'ProcTest' );
711 my $name = 'foo_proc';
712 my $sql = 'select foo from bar';
713 my $parameters = 'foo, bar';
715 my $comments = 'Go Sox!';
716 my $p = $s->add_procedure({
719 parameters => $parameters,
721 comments => $comments,
722 }); # or die $s->error;
723 $p->parameters([ qw/foo bar/ ]);
724 $p->comments($comments);
726 isa_ok( $p, 'SQL::Translator::Object::Procedure', 'Procedure' );
727 isa_ok( $p->schema, 'SQL::Translator::Object::Schema', 'Schema' );
728 is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
729 is( $p->name, $name, qq[Name is "$name"] );
730 is( $p->sql, $sql, qq[SQL is "$sql"] );
731 is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
732 is( $p->comments, $comments, qq[Comments = "$comments"] );
734 my @procs = $s->get_procedures;
735 is( scalar @procs, 1, 'Number of procedures is 1' );
737 my $p1 = $s->get_procedure( $name );
738 isa_ok( $p1, 'SQL::Translator::Object::Procedure', 'Procedure' );
739 is( $p1->name, $name, qq[Name is "$name"] );
742 # $schema-> drop_procedure
744 my $dropped_proc = $s->drop_procedure($p->name);
745 isa_ok($dropped_proc, 'SQL::Translator::Object::Procedure', 'Dropped procedure "foo_proc"' );
746 $s->add_procedure($p);
747 my $dropped_proc2 = $s->drop_procedure($p);
748 isa_ok($dropped_proc2, 'SQL::Translator::Object::Procedure', 'Dropped procedure "foo_proc" by object' );
749 #is($dropped_proc2->name, $p->name, 'Dropped correct procedure "foo_proc"');
750 my $dropped_proc3 = $s->drop_procedure($p->name);
751 #like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant procedure "foo_proc"] );
753 $s->add_procedure($p);