7 use Test::More tests => 157;
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' );
60 is( $foo_table->name, 'foo', 'Table name is "foo"' );
61 is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
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' );
67 is( $foo_table->comments, '', 'No comments' );
72 my $person_table = $schema->add_table(
76 is( $person_table->name, 'person', 'Table name is "person"' );
77 is( $person_table->is_valid, undef, 'Table is not yet valid' );
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"' );
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' );
93 is( $f1->is_nullable, 1, 'Field can be NULL' );
94 is( $f1->default_value, undef, 'Field default is undefined' );
95 is( $f1->comments, '', 'No comments' );
97 my $f2 = SQL::Translator::Schema::Field->new (
100 ) or warn SQL::Translator::Schema::Field->error;
101 $f2 = $person_table->add_field( $f2 );
102 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
103 is( $f2->name, 'f2', 'Add field "f2"' );
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' );
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"' );
113 $person_table = $f2->table( $person_table );
114 isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' );
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' );
119 my $redundant_field = $person_table->add_field(name => 'f2');
120 is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
121 like( $person_table->error, qr/can't use field/i,
122 '... because it exists' );
124 my @fields = $person_table->get_fields;
125 is( scalar @fields, 2, 'Table "foo" has 2 fields' );
127 is( $fields[0]->name, 'foo', 'First field is "foo"' );
128 is( $fields[1]->name, 'f2', 'Second field is "f2"' );
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' );
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"' );
146 # New field with args
148 my $age = $person_table->add_field(
150 data_type => 'float',
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"' );
159 is( join(':', $age->size), '10:2', 'Field size returns array' );
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"' );
172 is( $index1->is_valid, undef, 'Index name is not valid...' );
173 like( $index1->error, qr/no fields/i, '...because it has no fields' );
175 is( join(':', $index1->fields('foo,bar')), 'foo:bar',
176 'Index accepts fields');
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' );
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' );
186 is( $index1->type, NORMAL, 'Index type is "normal"' );
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"' );
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' );
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"' );
211 $fields = join(',', $constraint1->fields('id') );
212 is( $fields, 'id', 'Constraint field = "id"' );
214 $fields = join(',', $constraint1->fields('id,id') );
215 is( $fields, 'id', 'Constraint field = "id"' );
217 $fields = join(',', $constraint1->fields('id', 'name') );
218 is( $fields, 'id,name', 'Constraint field = "id,name"' );
220 $fields = join(',', $constraint1->fields( 'id,name,id' ) );
221 is( $fields, 'id,name', 'Constraint field = "id,name"' );
223 $fields = join(',', $constraint1->fields( 'id, name' ) );
224 is( $fields, 'id,name', 'Constraint field = "id,name"' );
226 $fields = join(',', $constraint1->fields( [ 'id', 'name' ] ) );
227 is( $fields, 'id,name', 'Constraint field = "id,name"' );
229 $fields = join(',', $constraint1->fields( qw[ id name ] ) );
230 is( $fields, 'id,name', 'Constraint field = "id,name"' );
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"' );
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"' );
245 my $constraints = $person_table->get_constraints;
246 is( scalar @$constraints, 2, 'Two constraints' );
247 is( $constraints->[0]->name, 'foo', '"foo" constraint' );
248 is( $constraints->[1]->name, 'bar', '"bar" constraint' );
253 my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
254 isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
255 my $view_sql = 'select * from table';
256 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
258 my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or
259 warn SQL::Translator::Schema::View->error;
260 my $check_view = $schema->add_view( $view2 );
261 is( $check_view->name, 'view2', 'Add view "view2"' );
263 my $redundant_view = $schema->add_view(name => 'view2');
264 is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
265 like( $schema->error, qr/can't create view/i, '... because it exists' );
270 my $bad_table = $schema->get_table;
271 like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
273 $bad_table = $schema->get_table('baz');
274 like( $schema->error, qr/does not exist/i,
275 'Error on bad arg to get_table' );
277 my $bad_view = $schema->get_view;
278 like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
280 $bad_view = $schema->get_view('bar');
281 like( $schema->error, qr/does not exist/i,
282 'Error on bad arg to get_view' );
284 my $good_table = $schema->get_table('foo');
285 isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
287 my $good_view = $schema->get_view('view1');
288 isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
289 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
294 my @tables = $schema->get_tables;
295 is( scalar @tables, 3, 'Found 2 tables' );
297 my @views = $schema->get_views;
298 is( scalar @views, 2, 'Found 1 view' );
302 # Test ability to introspect some values
305 my $s = SQL::Translator::Schema->new(
307 database => 'PostgreSQL',
309 my $t = $s->add_table( name => 'person' ) or warn $s->erro;
310 my $f = $t->add_field( name => 'person_id' ) or warn $t->error;
311 $f->data_type('serial');
313 my $c = $t->add_constraint(
315 fields => 'person_id',
318 is( $f->is_primary_key, 1, 'Field is PK' );
319 is( $f->is_auto_increment, 1, 'Field is auto inc' );
323 # FK constraint validity
326 my $s = SQL::Translator::Schema->new;
327 my $t = $s->add_table( name => 'person' ) or warn $s->error;
328 my $c = $t->add_constraint or warn $t->error;
330 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
331 like( $c->error, qr/no type/i, '...because it has no type' );
333 is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
335 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
336 like( $c->error, qr/no fields/i, '...because it has no fields' );
338 is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
340 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
341 like( $c->error, qr/non-existent field/i,
342 q[...because field "foo" doesn't exist] );
344 my $fk = $t->add_field( name => 'pet_id' );
345 is( $fk->name, 'pet_id', 'Added field "pet_id"' );
346 is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
348 $t->add_field( name => 'f1' );
349 $t->add_field( name => 'f2' );
350 is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
351 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
352 like( $c->error, qr/only one field/i,
353 q[...because too many fields for FK] );
357 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
358 like( $c->error, qr/no reference table/i,
359 q[...because there's no reference table] );
361 is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
362 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
363 like( $c->error, qr/no table named/i,
364 q[...because reference table "foo" doesn't exist] );
366 my $t2 = $s->add_table( name => 'pet' );
367 is( $t2->name, 'pet', 'Added "pet" table' );
369 is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
371 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
372 like( $c->error, qr/no reference fields/i,
373 q[...because there're no reference fields]);
375 is( join('', $c->reference_fields('pet_id')), 'pet_id',
376 'Reference fields = "pet_id"' );
378 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
379 like( $c->error, qr/non-existent field/i,
380 q[...because there's no "pet_id" field in "pet"]);
382 my $pet_id = $t2->add_field( name => 'pet_id' );
383 is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
385 is( $c->is_valid, 1, 'Constraint now valid' );
389 # $table->primary_key test
392 my $s = SQL::Translator::Schema->new;
393 my $t = $s->add_table( name => 'person' );
395 is( $t->primary_key, undef, 'No primary key' );
397 is( $t->primary_key('person_id'), undef,
398 q[Can't make PK on "person_id"...] );
399 like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
401 $t->add_field( name => 'person_id' );
402 my $c = $t->primary_key('person_id');
404 isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' );
406 is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
408 $t->add_field( name => 'name' );
409 $c = $t->primary_key('name');
410 is( join(',', $c->fields), 'person_id,name',
411 'Constraint now on "person_id" and "name"' );
413 is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
420 my $s = SQL::Translator::Schema->new;
421 my $t1 = $s->add_table( name => 'person' );
422 my $t2 = $s->add_table( name => 'pet' );
423 $t1->add_field( name => 'id' );
424 my $c1 = $t1->primary_key( 'id' ) or warn $t1->error;
425 is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
427 $t2->add_field( name => 'person_id' );
428 my $c2 = $t2->add_constraint(
430 fields => 'person_id',
431 reference_table => 'person',
434 is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
441 my $name = 'foo_view';
442 my $sql = 'select name, age from person';
443 my $fields = 'name, age';
444 my $s = SQL::Translator::Schema->new;
445 my $v = $s->add_view(
451 isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
452 is( $v->name, $name, qq[Name is "$name"] );
453 is( $v->sql, $sql, qq[Name is "$sql"] );
454 is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );