7 use Test::More tests => 202;
8 use SQL::Translator::Schema::Constants;
10 require_ok( 'SQL::Translator::Schema' );
16 my $schema = SQL::Translator::Schema->new(
20 isa_ok( $schema, 'SQL::Translator::Schema' );
22 is( $schema->name, 'foo', 'Schema name is "foo"' );
23 is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' );
25 is( $schema->database, 'MySQL', 'Schema database is "MySQL"' );
26 is( $schema->database('PostgreSQL'), 'PostgreSQL',
27 'Schema database changed to "PostgreSQL"' );
29 is( $schema->is_valid, undef, 'Schema not valid...' );
30 like( $schema->error, qr/no tables/i, '...because there are no tables' );
35 my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error;
36 isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
38 my $bar_table = SQL::Translator::Schema::Table->new( name => 'bar' ) or
39 warn SQL::Translator::Schema::Table->error;
40 $bar_table = $schema->add_table( $bar_table );
41 isa_ok( $bar_table, 'SQL::Translator::Schema::Table', 'Table "bar"' );
42 is( $bar_table->name, 'bar', 'Add table "bar"' );
44 $schema = $bar_table->schema( $schema );
45 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema' );
47 is( $bar_table->name('foo'), undef,
48 q[Can't change name of table "bar" to "foo"...]);
49 like( $bar_table->error, qr/can't use table name/i,
50 q[...because "foo" exists] );
52 my $redundant_table = $schema->add_table(name => 'foo');
53 is( $redundant_table, undef, qq[Can't create another "foo" table...] );
54 like( $schema->error, qr/can't use table name/i,
55 '... because "foo" exists' );
57 $redundant_table = $schema->add_table(name => '');
58 is( $redundant_table, undef, qq[Can't add an anonymouse table...] );
59 like( $schema->error, qr/No table name/i,
60 '... because if has no name ' );
62 $redundant_table = SQL::Translator::Schema::Table->new(name => '');
63 is( $redundant_table, undef, qq[Can't create an anonymouse table] );
64 like( SQL::Translator::Schema::Table->error, qr/No table name/i,
65 '... because if has no name ' );
70 is( $foo_table->name, 'foo', 'Table name is "foo"' );
71 is( "$foo_table", 'foo', 'Table stringifies to "foo"' );
72 is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
74 my $fields = $foo_table->get_fields;
75 is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
76 like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
78 is( $foo_table->comments, undef, 'No comments' );
83 my $person_table = $schema->add_table(
87 is( $person_table->name, 'person', 'Table name is "person"' );
88 is( $person_table->is_valid, undef, 'Table is not yet valid' );
89 is( $person_table->comments, 'foo', 'Comments = "foo"' );
90 is( join(',', $person_table->comments('bar')), 'foo,bar',
91 'Table comments = "foo,bar"' );
92 is( $person_table->comments, "foo\nbar", 'Table comments = "foo,bar"' );
97 my $f1 = $person_table->add_field(name => 'foo') or
98 warn $person_table->error;
99 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' );
100 is( $f1->name, 'foo', 'Field name is "foo"' );
101 is( $f1->full_name, 'person.foo', 'Field full_name is "person.foo"' );
102 is( "$f1", 'foo', 'Field stringifies to "foo"' );
103 is( $f1->data_type, '', 'Field data type is blank' );
104 is( $f1->size, 0, 'Field size is "0"' );
105 is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
106 is( $f1->is_nullable, 1, 'Field can be NULL' );
107 is( $f1->default_value, undef, 'Field default is undefined' );
108 is( $f1->comments, '', 'No comments' );
109 is( $f1->table, 'person', 'Field table is person' );
110 is( $f1->schema->database, 'PostgreSQL', 'Field schema shortcut works' );
112 my $f2 = SQL::Translator::Schema::Field->new (
115 ) or warn SQL::Translator::Schema::Field->error;
116 $f2 = $person_table->add_field( $f2 );
117 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
118 is( $f2->name, 'f2', 'Add field "f2"' );
119 is( $f2->is_nullable(0), 0, 'Field cannot be NULL' );
120 is( $f2->is_nullable(''), 0, 'Field cannot be NULL' );
121 is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' );
122 is( $f2->default_value(''), '', 'Field default is empty string' );
123 is( $f2->comments, 'foo', 'Field comment = "foo"' );
124 is( join(',', $f2->comments('bar')), 'foo,bar',
125 'Field comment = "foo,bar"' );
126 is( $f2->comments, "foo\nbar", 'Field comment = "foo,bar"' );
128 $person_table = $f2->table( $person_table );
129 isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' );
131 is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] );
132 like( $f2->error, qr/can't use field name/i, '...because name exists' );
134 my $redundant_field = $person_table->add_field(name => 'f2');
135 is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
136 like( $person_table->error, qr/can't use field/i,
137 '... because it exists' );
139 $redundant_field = $person_table->add_field(name => '');
140 is( $redundant_field, undef, qq[Didn't add a "" field...] );
141 like( $person_table->error, qr/No field name/i,
142 '... because it has no name' );
144 $redundant_field = SQL::Translator::Schema::Field->new(name => '');
145 is( $redundant_field, undef, qq[Didn't create a "" field...] );
146 like( SQL::Translator::Schema::Field->error, qr/No field name/i,
147 '... because it has no name' );
149 my @fields = $person_table->get_fields;
150 is( scalar @fields, 2, 'Table "foo" has 2 fields' );
152 is( $fields[0]->name, 'foo', 'First field is "foo"' );
153 is( $fields[1]->name, 'f2', 'Second field is "f2"' );
154 is( join(",",$person_table->field_names), 'foo,f2',
155 'field_names is "foo,f2"' );
160 is( $f1->name('person_name'), 'person_name',
161 'Field name is "person_name"' );
162 is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
163 is( $f1->size('30'), '30', 'Field size is "30"' );
164 is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
166 $f1->extra( foo => 'bar' );
167 $f1->extra( { baz => 'quux' } );
168 my %extra = $f1->extra;
169 is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' );
170 is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' );
173 # New field with args
175 my $age = $person_table->add_field(
177 data_type => 'float',
180 is( $age->name, 'age', 'Field name is "age"' );
181 is( $age->data_type, 'float', 'Field data type is "float"' );
182 is( $age->size, '10,2', 'Field size is "10,2"' );
183 is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
184 is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
185 is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
186 is( join(':', $age->size), '10:2', 'Field size returns array' );
191 my @indices = $person_table->get_indices;
192 is( scalar @indices, 0, 'No indices' );
193 like( $person_table->error, qr/no indices/i, 'Error for no indices' );
194 my $index1 = $person_table->add_index( name => "foo" )
195 or warn $person_table->error;
196 isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' );
197 is( $index1->name, 'foo', 'Index name is "foo"' );
199 is( $index1->is_valid, undef, 'Index name is not valid...' );
200 like( $index1->error, qr/no fields/i, '...because it has no fields' );
202 is( join(':', $index1->fields('foo,bar')), 'foo:bar',
203 'Index accepts fields');
205 is( $index1->is_valid, undef, 'Index name is not valid...' );
206 like( $index1->error, qr/does not exist in table/i,
207 '...because it used fields not in the table' );
209 is( join(':', $index1->fields(qw[foo age])), 'foo:age',
210 'Index accepts fields');
211 is( $index1->is_valid, 1, 'Index name is now valid' );
213 is( $index1->type, NORMAL, 'Index type is "normal"' );
215 my $index2 = SQL::Translator::Schema::Index->new( name => "bar" )
216 or warn SQL::Translator::Schema::Index->error;
217 $index2 = $person_table->add_index( $index2 );
218 isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' );
219 is( $index2->name, 'bar', 'Index name is "bar"' );
221 my $indices = $person_table->get_indices;
222 is( scalar @$indices, 2, 'Two indices' );
223 is( $indices->[0]->name, 'foo', '"foo" index' );
224 is( $indices->[1]->name, 'bar', '"bar" index' );
229 my @constraints = $person_table->get_constraints;
230 is( scalar @constraints, 0, 'No constraints' );
231 like( $person_table->error, qr/no constraints/i,
232 'Error for no constraints' );
233 my $constraint1 = $person_table->add_constraint( name => 'foo' )
234 or warn $person_table->error;
235 isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' );
236 is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
238 $fields = join(',', $constraint1->fields('age') );
239 is( $fields, 'age', 'Constraint field = "age"' );
241 $fields = $constraint1->fields;
242 ok( ref $fields[0] && $fields[0]->isa("SQL::Translator::Schema::Field"),
243 'Constraint fields returns a SQL::Translator::Schema::Field' );
245 $fields = join(',', $constraint1->fields('age,age') );
246 is( $fields, 'age', 'Constraint field = "age"' );
248 $fields = join(',', $constraint1->fields('age', 'name') );
249 is( $fields, 'age,name', 'Constraint field = "age,name"' );
251 $fields = join(',', $constraint1->fields( 'age,name,age' ) );
252 is( $fields, 'age,name', 'Constraint field = "age,name"' );
254 $fields = join(',', $constraint1->fields( 'age, name' ) );
255 is( $fields, 'age,name', 'Constraint field = "age,name"' );
257 $fields = join(',', $constraint1->fields( [ 'age', 'name' ] ) );
258 is( $fields, 'age,name', 'Constraint field = "age,name"' );
260 $fields = join(',', $constraint1->fields( qw[ age name ] ) );
261 is( $fields, 'age,name', 'Constraint field = "age,name"' );
263 $fields = join(',', $constraint1->field_names );
264 is( $fields, 'age,name', 'Constraint field_names = "age,name"' );
266 is( $constraint1->match_type, '', 'Constraint match type is empty' );
267 is( $constraint1->match_type('foo'), undef,
268 'Constraint match type rejects bad arg...' );
269 like( $constraint1->error, qr/invalid match type/i,
270 '...because it is invalid');
271 is( $constraint1->match_type('FULL'), 'full',
272 'Constraint match type = "full"' );
274 my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' );
275 $constraint2 = $person_table->add_constraint( $constraint2 );
276 isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' );
277 is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
279 my $constraint3 = $person_table->add_constraint(
281 expression => 'foo bar',
282 ) or die $person_table->error;
283 isa_ok( $constraint3, 'SQL::Translator::Schema::Constraint', 'Constraint' );
284 is( $constraint3->type, CHECK_C, 'Constraint type is "CHECK"' );
285 is( $constraint3->expression, 'foo bar',
286 'Constraint expression is "foo bar"' );
288 my $constraints = $person_table->get_constraints;
289 is( scalar @$constraints, 3, 'Three constraints' );
290 is( $constraints->[0]->name, 'foo', '"foo" constraint' );
291 is( $constraints->[1]->name, 'bar', '"bar" constraint' );
296 my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
297 isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
298 my $view_sql = 'select * from table';
299 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
301 my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or
302 warn SQL::Translator::Schema::View->error;
303 my $check_view = $schema->add_view( $view2 );
304 is( $check_view->name, 'view2', 'Add view "view2"' );
306 my $redundant_view = $schema->add_view(name => 'view2');
307 is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
308 like( $schema->error, qr/can't create view/i, '... because it exists' );
313 my $bad_table = $schema->get_table;
314 like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
316 $bad_table = $schema->get_table('baz');
317 like( $schema->error, qr/does not exist/i,
318 'Error on bad arg to get_table' );
320 my $bad_view = $schema->get_view;
321 like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
323 $bad_view = $schema->get_view('bar');
324 like( $schema->error, qr/does not exist/i,
325 'Error on bad arg to get_view' );
327 my $good_table = $schema->get_table('foo');
328 isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
330 my $good_view = $schema->get_view('view1');
331 isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
332 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
337 my @tables = $schema->get_tables;
338 is( scalar @tables, 3, 'Found 2 tables' );
340 my @views = $schema->get_views;
341 is( scalar @views, 2, 'Found 1 view' );
345 # Test ability to introspect some values
348 my $s = SQL::Translator::Schema->new(
350 database => 'PostgreSQL',
352 my $t = $s->add_table( name => 'person' ) or warn $s->erro;
353 my $f = $t->add_field( name => 'person_id' ) or warn $t->error;
354 $f->data_type('serial');
356 my $c = $t->add_constraint(
358 fields => 'person_id',
361 is( $f->is_primary_key, 1, 'Field is PK' );
362 is( $f->is_auto_increment, 1, 'Field is auto inc' );
366 # FK constraint validity
369 my $s = SQL::Translator::Schema->new;
370 my $t = $s->add_table( name => 'person' ) or warn $s->error;
371 my $c = $t->add_constraint or warn $t->error;
373 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
374 like( $c->error, qr/no type/i, '...because it has no type' );
376 is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
378 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
379 like( $c->error, qr/no fields/i, '...because it has no fields' );
381 is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
383 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
384 like( $c->error, qr/non-existent field/i,
385 q[...because field "foo" doesn't exist] );
387 my $fk = $t->add_field( name => 'pet_id' );
388 is( $fk->name, 'pet_id', 'Added field "pet_id"' );
389 is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
391 $t->add_field( name => 'f1' );
392 $t->add_field( name => 'f2' );
393 is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
394 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
395 like( $c->error, qr/only one field/i,
396 q[...because too many fields for FK] );
400 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
401 like( $c->error, qr/no reference table/i,
402 q[...because there's no reference table] );
404 is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
405 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
406 like( $c->error, qr/no table named/i,
407 q[...because reference table "foo" doesn't exist] );
409 my $t2 = $s->add_table( name => 'pet' );
410 is( $t2->name, 'pet', 'Added "pet" table' );
412 is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
414 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
415 like( $c->error, qr/no reference fields/i,
416 q[...because there're no reference fields]);
418 is( join('', $c->reference_fields('pet_id')), 'pet_id',
419 'Reference fields = "pet_id"' );
421 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
422 like( $c->error, qr/non-existent field/i,
423 q[...because there's no "pet_id" field in "pet"]);
425 my $pet_id = $t2->add_field( name => 'pet_id' );
426 is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
428 is( $c->is_valid, 1, 'Constraint now valid' );
432 # $table->primary_key test
435 my $s = SQL::Translator::Schema->new;
436 my $t = $s->add_table( name => 'person' );
438 is( $t->primary_key, undef, 'No primary key' );
440 is( $t->primary_key('person_id'), undef,
441 q[Can't make PK on "person_id"...] );
442 like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
444 $t->add_field( name => 'person_id' );
445 my $c = $t->primary_key('person_id');
447 isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' );
449 is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
451 $t->add_field( name => 'name' );
452 $c = $t->primary_key('name');
453 is( join(',', $c->fields), 'person_id,name',
454 'Constraint now on "person_id" and "name"' );
456 is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
463 my $s = SQL::Translator::Schema->new;
464 my $t1 = $s->add_table( name => 'person' );
465 my $t2 = $s->add_table( name => 'pet' );
466 $t1->add_field( name => 'id' );
467 my $c1 = $t1->primary_key( 'id' ) or warn $t1->error;
468 is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
470 $t2->add_field( name => 'person_id' );
471 my $c2 = $t2->add_constraint(
473 fields => 'person_id',
474 reference_table => 'person',
477 is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
484 my $s = SQL::Translator::Schema->new( name => 'ViewTest' );
485 my $name = 'foo_view';
486 my $sql = 'select name, age from person';
487 my $fields = 'name, age';
488 my $v = $s->add_view(
495 isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
496 isa_ok( $v->schema, 'SQL::Translator::Schema', 'Schema' );
497 is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
498 is( $v->name, $name, qq[Name is "$name"] );
499 is( $v->sql, $sql, qq[Name is "$sql"] );
500 is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );
502 my @views = $s->get_views;
503 is( scalar @views, 1, 'Number of views is 1' );
505 my $v1 = $s->get_view( $name );
506 isa_ok( $v1, 'SQL::Translator::Schema::View', 'View' );
507 is( $v1->name, $name, qq[Name is "$name"] );
514 my $s = SQL::Translator::Schema->new(name => 'TrigTest');
515 my $name = 'foo_trigger';
516 my $perform_action_when = 'after';
517 my $database_event = 'insert';
518 my $on_table = 'foo';
519 my $action = 'update modified=timestamp();';
520 my $t = $s->add_trigger(
522 perform_action_when => $perform_action_when,
523 database_event => $database_event,
524 on_table => $on_table,
528 isa_ok( $t, 'SQL::Translator::Schema::Trigger', 'Trigger' );
529 isa_ok( $t->schema, 'SQL::Translator::Schema', 'Schema' );
530 is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
531 is( $t->name, $name, qq[Name is "$name"] );
532 is( $t->perform_action_when, $perform_action_when,
533 qq[Perform action when is "$perform_action_when"] );
534 is( $t->database_event, $database_event,
535 qq[Database event is "$database_event"] );
536 is( $t->on_table, $on_table, qq[Table is "$on_table"] );
537 is( $t->action, $action, qq[Action is "$action"] );
539 my @triggs = $s->get_triggers;
540 is( scalar @triggs, 1, 'Number of triggers is 1' );
542 my $t1 = $s->get_trigger( $name );
543 isa_ok( $t1, 'SQL::Translator::Schema::Trigger', 'Trigger' );
544 is( $t1->name, $name, qq[Name is "$name"] );
551 my $s = SQL::Translator::Schema->new( name => 'ProcTest' );
552 my $name = 'foo_proc';
553 my $sql = 'select foo from bar';
554 my $parameters = 'foo, bar';
556 my $comments = 'Go Sox!';
557 my $p = $s->add_procedure(
560 parameters => $parameters,
562 comments => $comments,
565 isa_ok( $p, 'SQL::Translator::Schema::Procedure', 'Procedure' );
566 isa_ok( $p->schema, 'SQL::Translator::Schema', 'Schema' );
567 is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
568 is( $p->name, $name, qq[Name is "$name"] );
569 is( $p->sql, $sql, qq[SQL is "$sql"] );
570 is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
571 is( $p->comments, $comments, qq[Comments = "$comments"] );
573 my @procs = $s->get_procedures;
574 is( scalar @procs, 1, 'Number of procedures is 1' );
576 my $p1 = $s->get_procedure( $name );
577 isa_ok( $p1, 'SQL::Translator::Schema::Procedure', 'Procedure' );
578 is( $p1->name, $name, qq[Name is "$name"] );