7 use Test::More 'no_plan'; # plans => 1;
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, '... because "foo" exists' );
59 is( $foo_table->name, 'foo', 'Table name is "foo"' );
60 is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
62 my $fields = $foo_table->get_fields;
63 is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
64 like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
69 my $person_table = $schema->add_table( name => 'person' );
70 is( $person_table->name, 'person', 'Table name is "person"' );
71 is( $person_table->is_valid, undef, 'Table is not yet valid' );
76 my $f1 = $person_table->add_field(name => 'foo') or
77 warn $person_table->error;
78 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' );
79 is( $f1->name, 'foo', 'Field name is "foo"' );
80 is( $f1->data_type, '', 'Field data type is blank' );
81 is( $f1->size, 0, 'Field size is "0"' );
82 is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
83 is( $f1->nullable, 1, 'Field can be NULL' );
84 is( $f1->default_value, undef, 'Field default is undefined' );
86 my $f2 = SQL::Translator::Schema::Field->new (name => 'f2') or
87 warn SQL::Translator::Schema::Field->error;
88 $f2 = $person_table->add_field( $f2 );
89 isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
90 is( $f2->name, 'f2', 'Add field "f2"' );
91 is( $f2->nullable(0), 0, 'Field cannot be NULL' );
92 is( $f2->nullable(''), 0, 'Field cannot be NULL' );
93 is( $f2->nullable('0'), 0, 'Field cannot be NULL' );
94 is( $f1->default_value(''), '', 'Field default is empty string' );
96 $person_table = $f2->table( $person_table );
97 isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' );
99 is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] );
100 like( $f2->error, qr/can't use field name/i, '...because name exists' );
102 my $redundant_field = $person_table->add_field(name => 'f2');
103 is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
104 like( $person_table->error, qr/can't create field/i,
105 '... because it exists' );
110 is( $f1->name('person_name'), 'person_name',
111 'Field name is "person_name"' );
112 is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
113 is( $f1->size('30'), '30', 'Field size is "30"' );
114 is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
117 # New field with args
119 my $age = $person_table->add_field(
121 data_type => 'float',
124 is( $age->name, 'age', 'Field name is "age"' );
125 is( $age->data_type, 'float', 'Field data type is "float"' );
126 is( $age->size, '10,2', 'Field size is "10,2"' );
127 is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
128 is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
129 is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
134 my @indices = $person_table->get_indices;
135 is( scalar @indices, 0, 'No indices' );
136 like( $person_table->error, qr/no indices/i, 'Error for no indices' );
137 my $index1 = $person_table->add_index( name => "foo" )
138 or warn $person_table->error;
139 isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' );
140 is( $index1->name, 'foo', 'Index name is "foo"' );
142 my $index2 = SQL::Translator::Schema::Index->new( name => "bar" )
143 or warn SQL::Translator::Schema::Index->error;
144 $index2 = $person_table->add_index( $index2 );
145 isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' );
146 is( $index2->name, 'bar', 'Index name is "bar"' );
148 my $indices = $person_table->get_indices;
149 is( scalar @$indices, 2, 'Two indices' );
150 is( $indices->[0]->name, 'foo', '"foo" index' );
151 is( $indices->[1]->name, 'bar', '"bar" index' );
156 my @constraints = $person_table->get_constraints;
157 is( scalar @constraints, 0, 'No constraints' );
158 like( $person_table->error, qr/no constraints/i,
159 'Error for no constraints' );
160 my $constraint1 = $person_table->add_constraint( name => 'foo' )
161 or warn $person_table->error;
162 isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' );
163 is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
165 $fields = join(',', $constraint1->fields('id') );
166 is( $fields, 'id', 'Constraint field = "id"' );
168 $fields = join(',', $constraint1->fields('id,id') );
169 is( $fields, 'id', 'Constraint field = "id"' );
171 $fields = join(',', $constraint1->fields('id', 'name') );
172 is( $fields, 'id,name', 'Constraint field = "id,name"' );
174 $fields = join(',', $constraint1->fields( 'id,name,id' ) );
175 is( $fields, 'id,name', 'Constraint field = "id,name"' );
177 $fields = join(',', $constraint1->fields( 'id, name' ) );
178 is( $fields, 'id,name', 'Constraint field = "id,name"' );
180 $fields = join(',', $constraint1->fields( [ 'id', 'name' ] ) );
181 is( $fields, 'id,name', 'Constraint field = "id,name"' );
183 $fields = join(',', $constraint1->fields( qw[ id name ] ) );
184 is( $fields, 'id,name', 'Constraint field = "id,name"' );
186 my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' );
187 $constraint2 = $person_table->add_constraint( $constraint2 );
188 isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' );
189 is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
191 my $constraints = $person_table->get_constraints;
192 is( scalar @$constraints, 2, 'Two constraints' );
193 is( $constraints->[0]->name, 'foo', '"foo" constraint' );
194 is( $constraints->[1]->name, 'bar', '"bar" constraint' );
199 my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
200 isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
201 my $view_sql = 'select * from table';
202 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
204 my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or
205 warn SQL::Translator::Schema::View->error;
206 my $check_view = $schema->add_view( $view2 );
207 is( $check_view->name, 'view2', 'Add view "view2"' );
209 my $redundant_view = $schema->add_view(name => 'view2');
210 is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
211 like( $schema->error, qr/can't create view/i, '... because it exists' );
216 my $bad_table = $schema->get_table;
217 like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
219 $bad_table = $schema->get_table('baz');
220 like( $schema->error, qr/does not exist/i,
221 'Error on bad arg to get_table' );
223 my $bad_view = $schema->get_view;
224 like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
226 $bad_view = $schema->get_view('bar');
227 like( $schema->error, qr/does not exist/i,
228 'Error on bad arg to get_view' );
230 my $good_table = $schema->get_table('foo');
231 isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
233 my $good_view = $schema->get_view('view1');
234 isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
235 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
240 my @tables = $schema->get_tables;
241 is( scalar @tables, 3, 'Found 2 tables' );
243 my @views = $schema->get_views;
244 is( scalar @views, 2, 'Found 1 view' );
248 # Test ability to introspect some values
251 my $s = SQL::Translator::Schema->new(
253 database => 'PostgreSQL',
255 my $t = $s->add_table( name => 'person' ) or warn $s->erro;
256 my $f = $t->add_field( name => 'person_id' ) or warn $t->error;
257 $f->data_type('serial');
259 my $c = $t->add_constraint(
261 fields => 'person_id',
264 is( $f->is_primary_key, 1, 'Field is PK' );
265 is( $f->is_auto_increment, 1, 'Field is auto inc' );
269 # FK constraint validity
272 my $s = SQL::Translator::Schema->new;
273 my $t = $s->add_table( name => 'person' ) or warn $s->error;
274 my $c = $t->add_constraint or warn $t->error;
276 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
277 like( $c->error, qr/no type/i, '...because it has no type' );
279 is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
281 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
282 like( $c->error, qr/no fields/i, '...because it has no fields' );
284 is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
286 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
287 like( $c->error, qr/non-existent field/i,
288 q[...because field "foo" doesn't exist] );
290 my $fk = $t->add_field( name => 'pet_id' );
291 is( $fk->name, 'pet_id', 'Added field "pet_id"' );
292 is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
294 $t->add_field( name => 'f1' );
295 $t->add_field( name => 'f2' );
296 is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
297 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
298 like( $c->error, qr/only one field/i,
299 q[...because too many fields for FK] );
303 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
304 like( $c->error, qr/no reference table/i,
305 q[...because there's no reference table] );
307 is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
308 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
309 like( $c->error, qr/no table named/i,
310 q[...because reference table "foo" doesn't exist] );
312 my $t2 = $s->add_table( name => 'pet' );
313 is( $t2->name, 'pet', 'Added "pet" table' );
315 is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
317 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
318 like( $c->error, qr/no reference fields/i,
319 q[...because there're no reference fields]);
321 is( join('', $c->reference_fields('pet_id')), 'pet_id',
322 'Reference fields = "pet_id"' );
324 is( $c->is_valid, undef, 'Constraint on "person" not valid...');
325 like( $c->error, qr/non-existent field/i,
326 q[...because there's no "pet_id" field in "pet"]);
328 my $pet_id = $t2->add_field( name => 'pet_id' );
329 is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
331 is( $c->is_valid, 1, 'Constraint now valid' );
337 # $table->primary_key test
340 my $s = SQL::Translator::Schema->new;
341 my $t = $s->add_table( name => 'person' );
343 is( $t->primary_key, undef, 'No primary key' );
344 like( $t->error, qr/no primary key/i, 'No primary key error' );
346 is( $t->primary_key('person_id'), undef,
347 q[Can't make PK on "person_id"...] );
348 like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
350 $t->add_field( name => 'person_id' );
351 my $c = $t->primary_key('person_id');
353 isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' );
355 is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
357 $t->add_field( name => 'name' );
358 $c = $t->primary_key('name');
359 is( join(',', $c->fields), 'person_id,name',
360 'Constraint now on "person_id" and "name"' );
362 is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
369 my $s = SQL::Translator::Schema->new;
370 my $t1 = $s->add_table( name => 'person' );
371 my $t2 = $s->add_table( name => 'pet' );
372 $t1->add_field( name => 'id' );
373 my $c1 = $t1->primary_key( 'id' ) or warn $t1->error;
374 is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
376 $t2->add_field( name => 'person_id' );
377 my $c2 = $t2->add_constraint(
379 fields => 'person_id',
380 reference_table => 'person',
383 is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );