Added stringify to name and error check to stop creation of object without a name.
[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;
22d567ca 7use Test::More tests => 186;
07680493 8use SQL::Translator::Schema::Constants;
4d878d2f 9
10require_ok( 'SQL::Translator::Schema' );
11
07680493 12{
13 #
14 # Schema
15 #
16 my $schema = SQL::Translator::Schema->new(
17 name => 'foo',
18 database => 'MySQL',
19 );
20 isa_ok( $schema, 'SQL::Translator::Schema' );
4d878d2f 21
07680493 22 is( $schema->name, 'foo', 'Schema name is "foo"' );
23 is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' );
4d878d2f 24
07680493 25 is( $schema->database, 'MySQL', 'Schema database is "MySQL"' );
26 is( $schema->database('PostgreSQL'), 'PostgreSQL',
27 'Schema database changed to "PostgreSQL"' );
4d878d2f 28
07680493 29 is( $schema->is_valid, undef, 'Schema not valid...' );
30 like( $schema->error, qr/no tables/i, '...because there are no tables' );
4d878d2f 31
07680493 32 #
33 # $schema->add_*
34 #
35 my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error;
36 isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
4d878d2f 37
07680493 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"' );
4d878d2f 43
07680493 44 $schema = $bar_table->schema( $schema );
45 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema' );
4d878d2f 46
07680493 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] );
4d878d2f 51
07680493 52 my $redundant_table = $schema->add_table(name => 'foo');
53 is( $redundant_table, undef, qq[Can't create another "foo" table...] );
3166386a 54 like( $schema->error, qr/can't use table name/i,
55 '... because "foo" exists' );
07680493 56
57 #
58 # Table default new
59 #
60 is( $foo_table->name, 'foo', 'Table name is "foo"' );
61 is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
62
63 my $fields = $foo_table->get_fields;
64 is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
65 like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
66
7a7e9080 67 is( $foo_table->comments, undef, 'No comments' );
3166386a 68
07680493 69 #
70 # New table with args
71 #
3166386a 72 my $person_table = $schema->add_table(
73 name => 'person',
74 comments => 'foo',
75 );
07680493 76 is( $person_table->name, 'person', 'Table name is "person"' );
77 is( $person_table->is_valid, undef, 'Table is not yet valid' );
3166386a 78 is( $person_table->comments, 'foo', 'Comments = "foo"' );
79 is( join(',', $person_table->comments('bar')), 'foo,bar',
80 'Table comments = "foo,bar"' );
81 is( $person_table->comments, "foo\nbar", 'Table comments = "foo,bar"' );
07680493 82
83 #
84 # Field default new
85 #
86 my $f1 = $person_table->add_field(name => 'foo') or
87 warn $person_table->error;
88 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' );
89 is( $f1->name, 'foo', 'Field name is "foo"' );
90 is( $f1->data_type, '', 'Field data type is blank' );
91 is( $f1->size, 0, 'Field size is "0"' );
92 is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
14f8758f 93 is( $f1->is_nullable, 1, 'Field can be NULL' );
07680493 94 is( $f1->default_value, undef, 'Field default is undefined' );
3166386a 95 is( $f1->comments, '', 'No comments' );
07680493 96
3166386a 97 my $f2 = SQL::Translator::Schema::Field->new (
98 name => 'f2',
99 comments => 'foo',
100 ) or warn SQL::Translator::Schema::Field->error;
07680493 101 $f2 = $person_table->add_field( $f2 );
102 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
103 is( $f2->name, 'f2', 'Add field "f2"' );
14f8758f 104 is( $f2->is_nullable(0), 0, 'Field cannot be NULL' );
105 is( $f2->is_nullable(''), 0, 'Field cannot be NULL' );
106 is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' );
3166386a 107 is( $f2->default_value(''), '', 'Field default is empty string' );
108 is( $f2->comments, 'foo', 'Field comment = "foo"' );
109 is( join(',', $f2->comments('bar')), 'foo,bar',
110 'Field comment = "foo,bar"' );
111 is( $f2->comments, "foo\nbar", 'Field comment = "foo,bar"' );
07680493 112
113 $person_table = $f2->table( $person_table );
114 isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' );
115
116 is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] );
117 like( $f2->error, qr/can't use field name/i, '...because name exists' );
118
119 my $redundant_field = $person_table->add_field(name => 'f2');
120 is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
14f8758f 121 like( $person_table->error, qr/can't use field/i,
07680493 122 '... because it exists' );
123
14f8758f 124 my @fields = $person_table->get_fields;
125 is( scalar @fields, 2, 'Table "foo" has 2 fields' );
126
127 is( $fields[0]->name, 'foo', 'First field is "foo"' );
128 is( $fields[1]->name, 'f2', 'Second field is "f2"' );
129
07680493 130 #
131 # Field methods
132 #
133 is( $f1->name('person_name'), 'person_name',
134 'Field name is "person_name"' );
135 is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
136 is( $f1->size('30'), '30', 'Field size is "30"' );
137 is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
138
adfdb552 139 $f1->extra( foo => 'bar' );
140 $f1->extra( { baz => 'quux' } );
141 my %extra = $f1->extra;
142 is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' );
143 is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' );
144
07680493 145 #
146 # New field with args
147 #
148 my $age = $person_table->add_field(
149 name => 'age',
150 data_type => 'float',
151 size => '10,2',
152 );
153 is( $age->name, 'age', 'Field name is "age"' );
154 is( $age->data_type, 'float', 'Field data type is "float"' );
155 is( $age->size, '10,2', 'Field size is "10,2"' );
156 is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
157 is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
158 is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
14f8758f 159 is( join(':', $age->size), '10:2', 'Field size returns array' );
07680493 160
161 #
162 # Index
163 #
164 my @indices = $person_table->get_indices;
165 is( scalar @indices, 0, 'No indices' );
166 like( $person_table->error, qr/no indices/i, 'Error for no indices' );
167 my $index1 = $person_table->add_index( name => "foo" )
168 or warn $person_table->error;
169 isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' );
170 is( $index1->name, 'foo', 'Index name is "foo"' );
171
14f8758f 172 is( $index1->is_valid, undef, 'Index name is not valid...' );
173 like( $index1->error, qr/no fields/i, '...because it has no fields' );
174
175 is( join(':', $index1->fields('foo,bar')), 'foo:bar',
176 'Index accepts fields');
177
178 is( $index1->is_valid, undef, 'Index name is not valid...' );
179 like( $index1->error, qr/does not exist in table/i,
180 '...because it used fields not in the table' );
181
182 is( join(':', $index1->fields(qw[foo age])), 'foo:age',
183 'Index accepts fields');
184 is( $index1->is_valid, 1, 'Index name is now valid' );
185
186 is( $index1->type, NORMAL, 'Index type is "normal"' );
187
07680493 188 my $index2 = SQL::Translator::Schema::Index->new( name => "bar" )
189 or warn SQL::Translator::Schema::Index->error;
190 $index2 = $person_table->add_index( $index2 );
191 isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' );
192 is( $index2->name, 'bar', 'Index name is "bar"' );
193
194 my $indices = $person_table->get_indices;
195 is( scalar @$indices, 2, 'Two indices' );
196 is( $indices->[0]->name, 'foo', '"foo" index' );
197 is( $indices->[1]->name, 'bar', '"bar" index' );
198
199 #
200 # Constraint
201 #
202 my @constraints = $person_table->get_constraints;
203 is( scalar @constraints, 0, 'No constraints' );
204 like( $person_table->error, qr/no constraints/i,
205 'Error for no constraints' );
206 my $constraint1 = $person_table->add_constraint( name => 'foo' )
207 or warn $person_table->error;
208 isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' );
209 is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
210
211 $fields = join(',', $constraint1->fields('id') );
212 is( $fields, 'id', 'Constraint field = "id"' );
213
214 $fields = join(',', $constraint1->fields('id,id') );
215 is( $fields, 'id', 'Constraint field = "id"' );
216
217 $fields = join(',', $constraint1->fields('id', 'name') );
218 is( $fields, 'id,name', 'Constraint field = "id,name"' );
219
220 $fields = join(',', $constraint1->fields( 'id,name,id' ) );
221 is( $fields, 'id,name', 'Constraint field = "id,name"' );
222
223 $fields = join(',', $constraint1->fields( 'id, name' ) );
224 is( $fields, 'id,name', 'Constraint field = "id,name"' );
225
226 $fields = join(',', $constraint1->fields( [ 'id', 'name' ] ) );
227 is( $fields, 'id,name', 'Constraint field = "id,name"' );
228
229 $fields = join(',', $constraint1->fields( qw[ id name ] ) );
230 is( $fields, 'id,name', 'Constraint field = "id,name"' );
231
14f8758f 232 is( $constraint1->match_type, '', 'Constraint match type is empty' );
233 is( $constraint1->match_type('foo'), undef,
234 'Constraint match type rejects bad arg...' );
235 like( $constraint1->error, qr/invalid match type/i,
236 '...because it is invalid');
237 is( $constraint1->match_type('FULL'), 'full',
238 'Constraint match type = "full"' );
239
07680493 240 my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' );
241 $constraint2 = $person_table->add_constraint( $constraint2 );
242 isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' );
243 is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
244
637782f9 245 my $constraint3 = $person_table->add_constraint(
246 type => 'check',
247 expression => 'foo bar',
248 ) or die $person_table->error;
249 isa_ok( $constraint3, 'SQL::Translator::Schema::Constraint', 'Constraint' );
250 is( $constraint3->type, CHECK_C, 'Constraint type is "CHECK"' );
251 is( $constraint3->expression, 'foo bar',
252 'Constraint expression is "foo bar"' );
253
07680493 254 my $constraints = $person_table->get_constraints;
637782f9 255 is( scalar @$constraints, 3, 'Three constraints' );
07680493 256 is( $constraints->[0]->name, 'foo', '"foo" constraint' );
257 is( $constraints->[1]->name, 'bar', '"bar" constraint' );
258
259 #
260 # View
261 #
262 my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
263 isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
264 my $view_sql = 'select * from table';
265 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
266
267 my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or
268 warn SQL::Translator::Schema::View->error;
269 my $check_view = $schema->add_view( $view2 );
270 is( $check_view->name, 'view2', 'Add view "view2"' );
271
272 my $redundant_view = $schema->add_view(name => 'view2');
273 is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
274 like( $schema->error, qr/can't create view/i, '... because it exists' );
275
276 #
277 # $schema->get_*
278 #
279 my $bad_table = $schema->get_table;
280 like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
281
282 $bad_table = $schema->get_table('baz');
283 like( $schema->error, qr/does not exist/i,
284 'Error on bad arg to get_table' );
285
286 my $bad_view = $schema->get_view;
287 like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
288
289 $bad_view = $schema->get_view('bar');
290 like( $schema->error, qr/does not exist/i,
291 'Error on bad arg to get_view' );
292
293 my $good_table = $schema->get_table('foo');
294 isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
295
296 my $good_view = $schema->get_view('view1');
297 isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
298 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
299
300 #
301 # $schema->get_*s
302 #
303 my @tables = $schema->get_tables;
304 is( scalar @tables, 3, 'Found 2 tables' );
305
306 my @views = $schema->get_views;
307 is( scalar @views, 2, 'Found 1 view' );
308}
4d878d2f 309
310#
07680493 311# Test ability to introspect some values
4d878d2f 312#
07680493 313{
314 my $s = SQL::Translator::Schema->new(
315 name => 'foo',
316 database => 'PostgreSQL',
317 );
318 my $t = $s->add_table( name => 'person' ) or warn $s->erro;
319 my $f = $t->add_field( name => 'person_id' ) or warn $t->error;
320 $f->data_type('serial');
321
322 my $c = $t->add_constraint(
323 type => PRIMARY_KEY,
324 fields => 'person_id',
325 ) or warn $t->error;
326
327 is( $f->is_primary_key, 1, 'Field is PK' );
328 is( $f->is_auto_increment, 1, 'Field is auto inc' );
329}
4d878d2f 330
331#
07680493 332# FK constraint validity
4d878d2f 333#
07680493 334{
335 my $s = SQL::Translator::Schema->new;
336 my $t = $s->add_table( name => 'person' ) or warn $s->error;
337 my $c = $t->add_constraint or warn $t->error;
338
339 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
340 like( $c->error, qr/no type/i, '...because it has no type' );
341
342 is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
343
344 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
345 like( $c->error, qr/no fields/i, '...because it has no fields' );
346
347 is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
348
349 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
350 like( $c->error, qr/non-existent field/i,
351 q[...because field "foo" doesn't exist] );
352
353 my $fk = $t->add_field( name => 'pet_id' );
354 is( $fk->name, 'pet_id', 'Added field "pet_id"' );
355 is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
356
357 $t->add_field( name => 'f1' );
358 $t->add_field( name => 'f2' );
359 is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
360 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
361 like( $c->error, qr/only one field/i,
362 q[...because too many fields for FK] );
363
364 $c->fields('f1');
365
366 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
367 like( $c->error, qr/no reference table/i,
368 q[...because there's no reference table] );
369
370 is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
371 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
372 like( $c->error, qr/no table named/i,
373 q[...because reference table "foo" doesn't exist] );
374
375 my $t2 = $s->add_table( name => 'pet' );
376 is( $t2->name, 'pet', 'Added "pet" table' );
377
378 is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
379
380 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
381 like( $c->error, qr/no reference fields/i,
382 q[...because there're no reference fields]);
383
384 is( join('', $c->reference_fields('pet_id')), 'pet_id',
385 'Reference fields = "pet_id"' );
386
387 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
388 like( $c->error, qr/non-existent field/i,
389 q[...because there's no "pet_id" field in "pet"]);
390
391 my $pet_id = $t2->add_field( name => 'pet_id' );
392 is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
393
394 is( $c->is_valid, 1, 'Constraint now valid' );
07680493 395}
586fba3f 396
397#
07680493 398# $table->primary_key test
586fba3f 399#
07680493 400{
401 my $s = SQL::Translator::Schema->new;
402 my $t = $s->add_table( name => 'person' );
403
404 is( $t->primary_key, undef, 'No primary key' );
586fba3f 405
07680493 406 is( $t->primary_key('person_id'), undef,
407 q[Can't make PK on "person_id"...] );
408 like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
586fba3f 409
07680493 410 $t->add_field( name => 'person_id' );
411 my $c = $t->primary_key('person_id');
586fba3f 412
07680493 413 isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' );
586fba3f 414
07680493 415 is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
586fba3f 416
07680493 417 $t->add_field( name => 'name' );
418 $c = $t->primary_key('name');
419 is( join(',', $c->fields), 'person_id,name',
420 'Constraint now on "person_id" and "name"' );
421
422 is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
423}
586fba3f 424
425#
07680493 426# FK finds PK
586fba3f 427#
07680493 428{
429 my $s = SQL::Translator::Schema->new;
430 my $t1 = $s->add_table( name => 'person' );
431 my $t2 = $s->add_table( name => 'pet' );
432 $t1->add_field( name => 'id' );
433 my $c1 = $t1->primary_key( 'id' ) or warn $t1->error;
434 is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
435
436 $t2->add_field( name => 'person_id' );
437 my $c2 = $t2->add_constraint(
438 type => PRIMARY_KEY,
439 fields => 'person_id',
440 reference_table => 'person',
441 );
586fba3f 442
07680493 443 is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
444}
14f8758f 445
446#
447# View
448#
449{
22d567ca 450 my $s = SQL::Translator::Schema->new( name => 'ViewTest' );
14f8758f 451 my $name = 'foo_view';
452 my $sql = 'select name, age from person';
453 my $fields = 'name, age';
14f8758f 454 my $v = $s->add_view(
455 name => $name,
456 sql => $sql,
457 fields => $fields,
22d567ca 458 schema => $s,
14f8758f 459 );
460
461 isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
22d567ca 462 isa_ok( $v->schema, 'SQL::Translator::Schema', 'Schema' );
463 is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
14f8758f 464 is( $v->name, $name, qq[Name is "$name"] );
465 is( $v->sql, $sql, qq[Name is "$sql"] );
466 is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );
22d567ca 467
468 my @views = $s->get_views;
469 is( scalar @views, 1, 'Number of views is 1' );
470
471 my $v1 = $s->get_view( $name );
472 isa_ok( $v1, 'SQL::Translator::Schema::View', 'View' );
473 is( $v1->name, $name, qq[Name is "$name"] );
14f8758f 474}
90fd4bf5 475
476#
477# Trigger
478#
479{
22d567ca 480 my $s = SQL::Translator::Schema->new(name => 'TrigTest');
90fd4bf5 481 my $name = 'foo_trigger';
482 my $perform_action_when = 'after';
483 my $database_event = 'insert';
484 my $on_table = 'foo';
485 my $action = 'update modified=timestamp();';
90fd4bf5 486 my $t = $s->add_trigger(
487 name => $name,
488 perform_action_when => $perform_action_when,
489 database_event => $database_event,
490 on_table => $on_table,
491 action => $action,
492 ) or die $s->error;
493
494 isa_ok( $t, 'SQL::Translator::Schema::Trigger', 'Trigger' );
22d567ca 495 isa_ok( $t->schema, 'SQL::Translator::Schema', 'Schema' );
496 is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
90fd4bf5 497 is( $t->name, $name, qq[Name is "$name"] );
498 is( $t->perform_action_when, $perform_action_when,
499 qq[Perform action when is "$perform_action_when"] );
500 is( $t->database_event, $database_event,
501 qq[Database event is "$database_event"] );
502 is( $t->on_table, $on_table, qq[Table is "$on_table"] );
503 is( $t->action, $action, qq[Action is "$action"] );
22d567ca 504
505 my @triggs = $s->get_triggers;
506 is( scalar @triggs, 1, 'Number of triggers is 1' );
507
508 my $t1 = $s->get_trigger( $name );
509 isa_ok( $t1, 'SQL::Translator::Schema::Trigger', 'Trigger' );
510 is( $t1->name, $name, qq[Name is "$name"] );
511}
512
513#
514# Procedure
515#
516{
517 my $s = SQL::Translator::Schema->new( name => 'ProcTest' );
518 my $name = 'foo_proc';
519 my $sql = 'select foo from bar';
520 my $parameters = 'foo, bar';
521 my $owner = 'Nomar';
522 my $comments = 'Go Sox!';
523 my $p = $s->add_procedure(
524 name => $name,
525 sql => $sql,
526 parameters => $parameters,
527 owner => $owner,
528 comments => $comments,
529 ) or die $s->error;
530
531 isa_ok( $p, 'SQL::Translator::Schema::Procedure', 'Procedure' );
532 isa_ok( $p->schema, 'SQL::Translator::Schema', 'Schema' );
533 is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
534 is( $p->name, $name, qq[Name is "$name"] );
535 is( $p->sql, $sql, qq[SQL is "$sql"] );
536 is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
537 is( $p->comments, $comments, qq[Comments = "$comments"] );
538
539 my @procs = $s->get_procedures;
540 is( scalar @procs, 1, 'Number of procedures is 1' );
541
542 my $p1 = $s->get_procedure( $name );
543 isa_ok( $p1, 'SQL::Translator::Schema::Procedure', 'Procedure' );
544 is( $p1->name, $name, qq[Name is "$name"] );
90fd4bf5 545}