current state of this test
[dbsrgits/SQL-Translator-2.0-ish.git] / t / 13schema.t
CommitLineData
f0e701d3 1$| = 1;
2use warnings;
3use strict;
4use Test::More tests => 238;
5use Test::Exception;
6use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants);
7
8require_ok( 'SQL::Translator' );
a36eae8b 9require_ok( 'SQL::Translator::Object::Column' );
10require_ok( 'SQL::Translator::Object::Constraint' );
11require_ok( 'SQL::Translator::Object::Index' );
f0e701d3 12require_ok( 'SQL::Translator::Object::Schema' );
13require_ok( 'SQL::Translator::Object::Table' );
a36eae8b 14require_ok( 'SQL::Translator::Object::Trigger' );
15require_ok( 'SQL::Translator::Object::View' );
f0e701d3 16
17{
18 #
19 # Schema
20 #
21 my $schema = SQL::Translator::Object::Schema->new(
22 name => 'foo',
23 database => 'MySQL',
24 );
25 isa_ok( $schema, 'SQL::Translator::Object::Schema' );
26
27 is( $schema->name, 'foo', 'Schema name is "foo"' );
28 is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' );
29
30 is( $schema->database, 'MySQL', 'Schema database is "MySQL"' );
31 is( $schema->database('PostgreSQL'), 'PostgreSQL',
32 'Schema database changed to "PostgreSQL"' );
33
34 is( $schema->is_valid, undef, 'Schema not valid...' );
35# like( $schema->error, qr/no tables/i, '...because there are no tables' );
36
37 #
38 # $schema->add_*
39 #
a36eae8b 40 my $foo_table = SQL::Translator::Object::Table->new({ name => 'foo' });
f0e701d3 41 $schema->add_table($foo_table) or warn $schema->error;
42 isa_ok( $foo_table, 'SQL::Translator::Object::Table', 'Table "foo"' );
43
a36eae8b 44 my $bar_table = SQL::Translator::Object::Table->new({ name => 'bar' }) or
f0e701d3 45 warn SQL::Translator::Object::Table->error;
46 $schema->add_table( $bar_table );
47 isa_ok( $bar_table, 'SQL::Translator::Object::Table', 'Table "bar"' );
48 is( $bar_table->name, 'bar', 'Add table "bar"' );
49
50 $schema = $bar_table->schema( $schema );
51 isa_ok( $schema, 'SQL::Translator::Object::Schema', 'Schema' );
52
53 dies_ok(sub { $bar_table->name('foo') }, '...because "foo" exists');
54
55# is( $bar_table->name('foo'), undef,
56# q[Can't change name of table "bar" to "foo"...]);
57# like( $bar_table->error, qr/can't use table name/i,
58# q[...because "foo" exists] );
59
a36eae8b 60 my $redundant_table = new SQL::Translator::Object::Table({ name => 'foo' });
f0e701d3 61 dies_ok(sub { $schema->add_table($redundant_table) }, '...because "foo" exists"');
62 #is( $redundant_table, undef, qq[Can't create another "foo" table...] );
63 #like( $schema->error, qr/can't use table name/i,
64 # '... because "foo" exists' );
65
a36eae8b 66 $redundant_table = new SQL::Translator::Object::Table({ name => '' });
f0e701d3 67# is( $redundant_table, undef, qq[Can't add an anonymous table...] );
68# like( $schema->error, qr/No table name/i,
69# '... because it has no name ' );
70 dies_ok(sub { $schema->add_table($redundant_table) }, '...because it has no name');
71
a36eae8b 72 $redundant_table = SQL::Translator::Object::Table->new({ name => '' });
f0e701d3 73 #is( $redundant_table, undef, qq[Can't create an anonymous table] );
74 #like( SQL::Translator::Object::Table->error, qr/No table name/i,
75 # '... because it has no name ' );
76 dies_ok(sub { $schema->add_table($redundant_table) }, '...because it has no name');
77
78 #
79 # $schema-> drop_table
80 #
81 my $dropped_table = $schema->drop_table($foo_table->name, cascade => 1);
82 isa_ok($dropped_table, 'SQL::Translator::Object::Table', 'Dropped table "foo"' );
83 $schema->add_table($foo_table);
84 my $dropped_table2 = $schema->drop_table($foo_table, cascade => 1);
85 isa_ok($dropped_table2, 'SQL::Translator::Object::Table', 'Dropped table "foo" by object' );
86 dies_ok(sub { my $dropped_table3 = $schema->drop_table($foo_table->name, cascade => 1) }, qq[Can't drop non-existant table "foo"]);
87# my $dropped_table3 = $schema->drop_table($foo_table->name, cascade => 1);
88# like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant table "foo"] );
89
90 $schema->add_table($foo_table);
91 #
92 # Table default new
93 #
94 is( $foo_table->name, 'foo', 'Table name is "foo"' );
95 is( "$foo_table", 'foo', 'Table stringifies to "foo"' );
96 is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
97
98 my $fields = $foo_table->get_fields;
99 is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
100 #like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
101
102 is( $foo_table->comments, '', 'No comments' );
103
104 #
105 # New table with args
106 #
107 my $person_table = SQL::Translator::Object::Table->new({
108 name => 'person',
109 schema => $schema,
110 });
111 $person_table->comments('foo');
112 $schema->add_table($person_table);
113 is( $person_table->name, 'person', 'Table name is "person"' );
114 is( $person_table->is_valid, undef, 'Table is not yet valid' );
115 is( $person_table->comments, 'foo', 'Comments = "foo"' );
116 is( join(',', $person_table->comments('bar')), 'foo,bar', 'Table comments = "foo,bar"' );
117 is( $person_table->comments, "foo\nbar", 'Table comments = "foo,bar"' );
118
119 #
120 # Field default new
121 #
a36eae8b 122 my $person_table_column = SQL::Translator::Object::Column->new( name => 'foo' );
f0e701d3 123 my $f1 = $person_table->add_column($person_table_column);
124 isa_ok( $f1, 'SQL::Translator::Object::Column', 'Column' );
125 is( $f1->name, 'foo', 'Field name is "foo"' );
126 is( $f1->full_name, 'person.foo', 'Field full_name is "person.foo"' );
127 is( "$f1", 'foo', 'Field stringifies to "foo"' );
128 is( $f1->data_type, '', 'Field data type is blank' );
129 is( $f1->size, 0, 'Field size is "0"' );
130 is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
131 is( $f1->is_nullable, 1, 'Field can be NULL' );
132 is( $f1->default_value, undef, 'Field default is undefined' );
133 is( $f1->comments, '', 'No comments' );
134 is( $f1->table, 'person', 'Field table is person' );
135 is( $f1->schema->database, 'PostgreSQL', 'Field schema shortcut works' );
136
137 my $f2 = SQL::Translator::Object::Column->new ({ name => 'f2', });
138 $f2->comments('foo');
139 $f2 = $person_table->add_column( $f2 );
140 isa_ok( $f1, 'SQL::Translator::Object::Column', 'f2' );
141 is( $f2->name, 'f2', 'Add field "f2"' );
142 is( $f2->is_nullable(0), 0, 'Field cannot be NULL' );
143# is( $f2->is_nullable(''), 0, 'Field cannot be NULL' );
144 is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' );
145 is( $f2->default_value(''), '', 'Field default is empty string' );
146 is( $f2->comments, 'foo', 'Field comment = "foo"' );
147 is( join(',', $f2->comments('bar')), 'foo,bar', 'Field comment = "foo,bar"' );
148 is( $f2->comments, "foo\nbar", 'Field comment = "foo,bar"' );
149
150 $person_table = $f2->table( $person_table );
151 isa_ok( $person_table, 'SQL::Translator::Object::Table', 'person_table' );
152
153 #is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] );
154 dies_ok(sub { $f2->name('foo') }, q[Can't set field name of "f2" to "foo"...] . '...because name exists');
155 #like( $f2->error, qr/can't use field name/i, '...because name exists' );
156
157# my $redundant_field = $person_table->add_field(name => 'f2');
158# is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
159# like( $person_table->error, qr/can't use field/i,
160# '... because it exists' );
161 dies_ok(sub { my $redundant_field = $person_table->add_column({ name => 'f2' }); }, qq[Didn't create another "f2" field...] . '... because it exists' );
162
163# my $redundant_field = $person_table->add_column({ name => '' });
164# is( $redundant_field, undef, qq[Didn't add a "" field...] );
165# like( $person_table->error, qr/No field name/i,
166# '... because it has no name' );
167 dies_ok(sub { my $redundant_field = $person_table->add_column({ name => '' }) }, qq[Didn't add a "" field...] . '... because it has no name' );
168
169# my $redundant_field = SQL::Translator::Object::Column->new(name => '');
170# is( $redundant_field, undef, qq[Didn't create a "" field...] );
171# like( SQL::Translator::Object::Column->error, qr/No field name/i,
172# '... because it has no name' );
173 dies_ok(sub { my $redundant_field = SQL::Translator::Object::Column->new({ name => '' }) }, qq[Didn't create a "" field...] . '... because it has no name' );
174
175 my @fields = $person_table->get_fields;
176 is( scalar @fields, 2, 'Table "foo" has 2 fields' );
177
178 is( $fields[0]->name, 'foo', 'First field is "foo"' );
179 is( $fields[1]->name, 'f2', 'Second field is "f2"' );
180 is( join(",",$person_table->fields), 'foo,f2', 'field_names is "foo,f2"' );
181
182 #
183 # $table-> drop_column
184 #
185 my $dropped_field = $person_table->drop_column($f2->name, cascade => 1);
186 isa_ok($dropped_field, 'SQL::Translator::Object::Column', 'Dropped field "f2"' );
187 $person_table->add_column($f2);
188 my $dropped_field2 = $person_table->drop_column($f2, cascade => 1);
189 isa_ok($dropped_field2, 'SQL::Translator::Object::Column', 'Dropped field "f2" by object' );
190# my $dropped_field3 = $person_table->drop_column($f2->name, cascade => 1);
191# like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant field "f2"] );
192 dies_ok(sub { $person_table->drop_column($f2->name, cascade => 1); }, qq[Can't drop non-existant field "f2"]);
193
194 $person_table->add_field($f2);
195
196 #
197 # Field methods
198 #
a36eae8b 199 is( $f1->name('person_name'), 'person_name', 'Field name is "person_name"' );
f0e701d3 200 is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
201 is( $f1->size('30'), '30', 'Field size is "30"' );
202 is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
203
204 $f1->extra({ foo => 'bar' });
a36eae8b 205 $f1->extra({ baz => 'quux' });
f0e701d3 206 my %extra = $f1->extra;
207 is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' );
208 is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' );
209
210 #
211 # New field with args
212 #
213 my $age = $person_table->add_field({
214 name => 'age',
215 data_type => 'float',
216 size => '10,2',
217 });
a36eae8b 218
f0e701d3 219 is( $age->name, 'age', 'Field name is "age"' );
220 is( $age->data_type, 'float', 'Field data type is "float"' );
221 is( $age->size, '10,2', 'Field size is "10,2"' );
222 is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
223 is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
224 is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
225 is( join(':', $age->size), '10:2', 'Field size returns array' );
226
227 #
228 # Index
229 #
230 my @indices = $person_table->get_indices;
231 is( scalar @indices, 0, 'No indices' );
a36eae8b 232 #like( $person_table->error, qr/no indices/i, 'Error for no indices' );
233# my $index1 = $person_table->add_index(SQL::Translator::Object::Index->new( name => "foo" )) ;
234 my $index1 = $person_table->add_index({ name => "foo" });
235 isa_ok( $index1, 'SQL::Translator::Object::Index', 'Index' );
f0e701d3 236 is( $index1->name, 'foo', 'Index name is "foo"' );
237
238 is( $index1->is_valid, undef, 'Index name is not valid...' );
a36eae8b 239# like( $index1->error, qr/no fields/i, '...because it has no fields' );
f0e701d3 240
a36eae8b 241# is( join(':', $index1->fields('foo,bar')), 'foo:bar', 'Index accepts fields');
f0e701d3 242
a36eae8b 243# is( $index1->is_valid, undef, 'Index name is not valid...' );
244# like( $index1->error, qr/does not exist in table/i, '...because it used fields not in the table' );
245 dies_ok(sub { $index1->fields('foo,bar') }, 'because it used fields not in the table');
f0e701d3 246
a36eae8b 247 is( join(':', $index1->fields('person_name,age')), 'person_name:age', 'Index accepts fields');
248 #is( join(':', $index1->fields(qw/foo age/)), 'foo:age', 'Index accepts fields');
f0e701d3 249 is( $index1->is_valid, 1, 'Index name is now valid' );
250
251 is( $index1->type, NORMAL, 'Index type is "normal"' );
252
a36eae8b 253 my $index2 = SQL::Translator::Object::Index->new( name => "bar" );
254
255 $index2 = $person_table->add_index( $index2 );
256 isa_ok( $index2, 'SQL::Translator::Object::Index', 'Index' );
f0e701d3 257 is( $index2->name, 'bar', 'Index name is "bar"' );
258
a36eae8b 259 @indices = $person_table->get_indices;
260 is( scalar @indices, 2, 'Two indices' );
261 is( $indices[0]->name, 'foo', '"foo" index' );
262 is( $indices[1]->name, 'bar', '"bar" index' );
f0e701d3 263
264 #
265 # $table-> drop_index
266 #
267 my $dropped_index = $person_table->drop_index($index1->name);
a36eae8b 268 isa_ok($dropped_index, 'SQL::Translator::Object::Index', 'Dropped index "foo"' );
f0e701d3 269 $person_table->add_index($index1);
270 my $dropped_index2 = $person_table->drop_index($index1);
a36eae8b 271 isa_ok($dropped_index2, 'SQL::Translator::Object::Index', 'Dropped index "foo" by object' );
f0e701d3 272 is($dropped_index2->name, $index1->name, 'Dropped correct index "foo"');
a36eae8b 273# my $dropped_index3 = $person_table->drop_index($index1->name);
274# like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant index "foo"] );
275 dies_ok(sub { $person_table->drop_index($index1->name) }, qq[Can't drop non-existant index "foo"] );
f0e701d3 276
277 $person_table->add_index($index1);
278
279 #
280 # Constraint
281 #
282 my @constraints = $person_table->get_constraints;
283 is( scalar @constraints, 0, 'No constraints' );
a36eae8b 284 #like( $person_table->error, qr/no constraints/i, 'Error for no constraints' );
285 my $constraint1 = $person_table->add_constraint({ name => 'foo', type => 'bar' });
286
287 isa_ok( $constraint1, 'SQL::Translator::Object::Constraint', 'Constraint' );
f0e701d3 288 is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
289
290 $fields = join(',', $constraint1->fields('age') );
291 is( $fields, 'age', 'Constraint field = "age"' );
292
293 $fields = $constraint1->fields;
294 ok( ref $fields[0] && $fields[0]->isa("SQL::Translator::Object::Column"),
295 'Constraint fields returns a SQL::Translator::Object::Column' );
296
297 $fields = join(',', $constraint1->fields('age,age') );
298 is( $fields, 'age', 'Constraint field = "age"' );
299
a36eae8b 300# $fields = join(',', $constraint1->fields('age', 'name') );
301# is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
f0e701d3 302
a36eae8b 303 $fields = join(',', $constraint1->fields( 'age,person_name,age' ) );
304 is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
f0e701d3 305
a36eae8b 306 $fields = join(',', $constraint1->fields( 'age, person_name' ) );
307 is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
f0e701d3 308
a36eae8b 309 $fields = join(',', $constraint1->fields( [ 'age', 'person_name' ] ) );
310 is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
f0e701d3 311
a36eae8b 312# $fields = join(',', $constraint1->fields( qw[ age person_name ] ) );
313# is( $fields, 'age,person_name', 'Constraint field = "age,name"' );
f0e701d3 314
315 $fields = join(',', $constraint1->field_names );
a36eae8b 316 is( $fields, 'age,person_name', 'Constraint field_names = "age,name"' );
f0e701d3 317
318 is( $constraint1->match_type, '', 'Constraint match type is empty' );
a36eae8b 319# is( $constraint1->match_type('foo'), undef,
320# 'Constraint match type rejects bad arg...' );
321# like( $constraint1->error, qr/invalid match type/i,
322# '...because it is invalid');
323 dies_ok(sub { $constraint1->match_type('foo') }, '...because it is invalid');
f0e701d3 324 is( $constraint1->match_type('FULL'), 'full',
325 'Constraint match type = "full"' );
326
a36eae8b 327 my $constraint2 = SQL::Translator::Object::Constraint->new( name => 'bar', type => 'baz' );
f0e701d3 328 $constraint2 = $person_table->add_constraint( $constraint2 );
a36eae8b 329 isa_ok( $constraint2, 'SQL::Translator::Object::Constraint', 'Constraint' );
f0e701d3 330 is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
331
a36eae8b 332 my $constraint3 = $person_table->add_constraint({
f0e701d3 333 type => 'check',
334 expression => 'foo bar',
a36eae8b 335 }); # or die $person_table->error;
336 isa_ok( $constraint3, 'SQL::Translator::Object::Constraint', 'Constraint' );
337 is( $constraint3->type, 'check', 'Constraint type is "CHECK"' ); ## CHECK_C
338 is( $constraint3->expression, 'foo bar', 'Constraint expression is "foo bar"' );
f0e701d3 339
a36eae8b 340 @constraints = $person_table->get_constraints;
341 is( scalar @constraints, 3, 'Three constraints' );
342 is( $constraints[0]->name, 'foo', '"foo" constraint' );
343 is( $constraints[1]->name, 'bar', '"bar" constraint' );
f0e701d3 344
345 #
346 # $table-> drop_constraint
347 #
348 my $dropped_con = $person_table->drop_constraint($constraint1->name);
a36eae8b 349 isa_ok($dropped_con, 'SQL::Translator::Object::Constraint', 'Dropped constraint "foo"' );
f0e701d3 350 $person_table->add_constraint($constraint1);
351 my $dropped_con2 = $person_table->drop_constraint($constraint1);
a36eae8b 352 isa_ok($dropped_con2, 'SQL::Translator::Object::Constraint', 'Dropped constraint "foo" by object' );
f0e701d3 353 is($dropped_con2->name, $constraint1->name, 'Dropped correct constraint "foo"');
a36eae8b 354# my $dropped_con3 = $person_table->drop_constraint($constraint1->name);
355# like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant constraint "foo"] );
356 dies_ok(sub { $person_table->drop_constraint($constraint1->name) }, qq[Can't drop non-existant constraint "foo"] );
f0e701d3 357
358 $person_table->add_constraint($constraint1);
359
360 #
361 # View
362 #
a36eae8b 363 my $view = $schema->add_view({ name => 'view1' }) or warn $schema->error;
364 isa_ok( $view, 'SQL::Translator::Object::View', 'View' );
f0e701d3 365 my $view_sql = 'select * from table';
366 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
367
a36eae8b 368 my $view2 = SQL::Translator::Object::View->new(name => 'view2') or
369 warn SQL::Translator::Object::View->error;
f0e701d3 370 my $check_view = $schema->add_view( $view2 );
371 is( $check_view->name, 'view2', 'Add view "view2"' );
372
a36eae8b 373 dies_ok(sub { my $redundant_view = $schema->add_view({ name => 'view2' }) }, '... because it exists');
374# my $redundant_view = $schema->add_view({ name => 'view2' });
375# is( $redundant_view, undef, qq[Didn't create another "view2" view...] );
376# like( $schema->error, qr/can't create view/i, '... because it exists' );
f0e701d3 377
378 #
379 # $schema-> drop_view
380 #
381 my $dropped_view = $schema->drop_view($view->name);
a36eae8b 382 isa_ok($dropped_view, 'SQL::Translator::Object::View', 'Dropped view "view1"' );
f0e701d3 383 $schema->add_view($view);
384 my $dropped_view2 = $schema->drop_view($view);
a36eae8b 385 isa_ok($dropped_view2, 'SQL::Translator::Object::View', 'Dropped view "view1" by object' );
f0e701d3 386 is($dropped_view2->name, $view->name, 'Dropped correct view "view1"');
a36eae8b 387# my $dropped_view3 = $schema->drop_view($view->name);
388# like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant view "view1"] );
389 dies_ok(sub { my $dropped_view3 = $schema->drop_view($view->name) }, qq[Can't drop non-existant view "view1"] );
f0e701d3 390
391 $schema->add_view($view);
392
393 #
394 # $schema->get_*
395 #
a36eae8b 396
397 dies_ok( sub { my $bad_table = $schema->get_table }, 'Error on no arg to get_table' );
398 is($schema->get_table('baz'), undef, 'Nonexistant table returns undef');
f0e701d3 399
a36eae8b 400 dies_ok( sub { my $bad_view = $schema->get_view }, 'Error on no arg to get_view' );
401 is($schema->get_view('baz'), undef, 'Nonexistant view returns undef');
f0e701d3 402
403 my $good_table = $schema->get_table('foo');
404 isa_ok( $good_table, 'SQL::Translator::Object::Table', 'Table "foo"' );
405
406 my $good_view = $schema->get_view('view1');
a36eae8b 407 isa_ok( $good_view, 'SQL::Translator::Object::View', 'View "view1"' );
f0e701d3 408 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
409
410 #
411 # $schema->get_*s
412 #
413 my @tables = $schema->get_tables;
414 is( scalar @tables, 3, 'Found 2 tables' );
415
416 my @views = $schema->get_views;
417 is( scalar @views, 2, 'Found 1 view' );
418
419}
420
421#
422# Test ability to introspect some values
423#
424{
425 my $s = SQL::Translator::Object::Schema->new(
426 name => 'foo',
427 database => 'PostgreSQL',
428 );
a36eae8b 429 my $t = $s->add_table({ name => 'person' }) or warn $s->error;
430 my $f = $t->add_field({ name => 'person_id' }) or warn $t->error;
f0e701d3 431 $f->data_type('serial');
432
a36eae8b 433 my $c = $t->add_constraint({
f0e701d3 434 type => PRIMARY_KEY,
a36eae8b 435# fields => 'person_id',
436 }) or warn $t->error;
437 $c->add_column($f);
f0e701d3 438
439 is( $f->is_primary_key, 1, 'Field is PK' );
440 is( $f->is_auto_increment, 1, 'Field is auto inc' );
441}
442
443#
444# FK constraint validity
445#
446{
447 my $s = SQL::Translator::Object::Schema->new;
a36eae8b 448 my $t = $s->add_table({ name => 'person' });
449# my $c = $t->add_constraint({ });
f0e701d3 450
a36eae8b 451 dies_ok( sub { my $c = $t->add_constraint({ }) }, '...because it has no type' );
f0e701d3 452
a36eae8b 453# is( $c->is_valid, undef, 'Constraint on "person" not valid...');
454# like( $c->error, qr/no type/i, '...because it has no type' );
f0e701d3 455
a36eae8b 456# is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' );
f0e701d3 457
a36eae8b 458# is( $c->is_valid, undef, 'Constraint on "person" not valid...');
459# like( $c->error, qr/no fields/i, '...because it has no fields' );
8977e30a 460# dies_ok( sub { my $c = $t->add_constraint({ type => FOREIGN_KEY }) }, '...because it has no fields' );
f0e701d3 461
a36eae8b 462# is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' );
463 dies_ok( sub { my $c = $t->add_constraint({ type => FOREIGN_KEY, fields => 'foo' }) }, q[...because field "foo" doesn't exist] );
f0e701d3 464
a36eae8b 465# is( $c->is_valid, undef, 'Constraint on "person" not valid...');
466# like( $c->error, qr/non-existent field/i, q[...because field "foo" doesn't exist] );
467
468 my $fk = $t->add_field({ name => 'pet_id' });
f0e701d3 469 is( $fk->name, 'pet_id', 'Added field "pet_id"' );
a36eae8b 470# is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' );
471
472 $t->add_field({ name => 'f1' });
473 $t->add_field({ name => 'f2' });
474# is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' );
475# is( $c->is_valid, undef, 'Constraint on "person" not valid...');
476# like( $c->error, qr/only one field/i,
477# q[...because too many fields for FK] );
f0e701d3 478
a36eae8b 479# $c->fields('f1');
f0e701d3 480
a36eae8b 481# is( $c->is_valid, undef, 'Constraint on "person" not valid...');
482# like( $c->error, qr/no reference table/i,
483# q[...because there's no reference table] );
f0e701d3 484
a36eae8b 485# is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' );
486# is( $c->is_valid, undef, 'Constraint on "person" not valid...');
487# like( $c->error, qr/no table named/i,
488# q[...because reference table "foo" doesn't exist] );
f0e701d3 489
a36eae8b 490 dies_ok( sub { my $c = $t->add_constraint({ type => FOREIGN_KEY, fields => [ 'f1', 'f2' ], reference_table => 'foo'}) }, "...because reference table foo doesn't exist" );
f0e701d3 491
a36eae8b 492 my $t2 = $s->add_table({ name => 'pet' });
f0e701d3 493 is( $t2->name, 'pet', 'Added "pet" table' );
494
a36eae8b 495 dies_ok( sub { my $c = $t->add_constraint({ type => FOREIGN_KEY, reference_table => 'pet' }) }, '...because there are no reference fields' );
f0e701d3 496
a36eae8b 497# is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' );
f0e701d3 498
a36eae8b 499# is( $c->is_valid, undef, 'Constraint on "person" not valid...');
500 #like( $c->error, qr/no reference fields/i,
501 # q[...because there're no reference fields]);
f0e701d3 502
a36eae8b 503## is( join('', $c->reference_fields('pet_id')), 'pet_id',
504## 'Reference fields = "pet_id"' );
f0e701d3 505
a36eae8b 506## is( $c->is_valid, undef, 'Constraint on "person" not valid...');
507 #like( $c->error, qr/non-existent field/i,
508 # q[...because there's no "pet_id" field in "pet"]);
509
510 my $pet_id = $t2->add_field({ name => 'pet_id' });
f0e701d3 511 is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
512
a36eae8b 513## is( $c->is_valid, 1, 'Constraint now valid' );
f0e701d3 514}
515
516#
517# $table->primary_key test
518#
519{
520 my $s = SQL::Translator::Object::Schema->new;
a36eae8b 521 my $t = $s->add_table({ name => 'person' });
f0e701d3 522
523 is( $t->primary_key, undef, 'No primary key' );
524
a36eae8b 525# is( $t->primary_key('person_id'), undef,
526# q[Can't make PK on "person_id"...] );
527# like( $t->error, qr/invalid field/i, "...because it doesn't exist" );
528 dies_ok( sub { $t->primary_key }, "...because it doesn't exist" );
f0e701d3 529
a36eae8b 530 $t->add_field({ name => 'person_id' });
f0e701d3 531 my $c = $t->primary_key('person_id');
532
a36eae8b 533 isa_ok( $c, 'SQL::Translator::Object::Constraint', 'Constraint' );
f0e701d3 534
535 is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' );
536
a36eae8b 537 $t->add_field({ name => 'name' });
f0e701d3 538 $c = $t->primary_key('name');
539 is( join(',', $c->fields), 'person_id,name',
540 'Constraint now on "person_id" and "name"' );
541
a36eae8b 542 #is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' );
543 is( scalar $t->get_constraints, 1, 'Found 1 constraint' );
f0e701d3 544}
545
546#
547# FK finds PK
548#
549{
550 my $s = SQL::Translator::Object::Schema->new;
a36eae8b 551 my $t1 = $s->add_table({ name => 'person' });
552 my $t2 = $s->add_table({ name => 'pet' });
553 $t1->add_field({ name => 'id' });
8977e30a 554 my $c1 = $t1->primary_key( 'id' );
f0e701d3 555 is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' );
556
a36eae8b 557 $t2->add_field({ name => 'person_id' });
558 my $c2 = $t2->add_constraint({
f0e701d3 559 type => PRIMARY_KEY,
560 fields => 'person_id',
561 reference_table => 'person',
a36eae8b 562 table => $t1,
8977e30a 563 reference_fields => 'id',
a36eae8b 564 });
f0e701d3 565
566 is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
567}
568
569#
570# View
571#
572{
a36eae8b 573 my $s = SQL::Translator::Object::Schema->new({ name => 'ViewTest' });
f0e701d3 574 my $name = 'foo_view';
575 my $sql = 'select name, age from person';
576 my $fields = 'name, age';
a36eae8b 577 my $v = $s->add_view({
f0e701d3 578 name => $name,
579 sql => $sql,
8977e30a 580# fields => $fields,
581# columns => $fields,
f0e701d3 582 schema => $s,
a36eae8b 583 });
f0e701d3 584
8977e30a 585# $v->add_field({ name => 'name' });
586 $v->add_field({ name => 'age' });
587
a36eae8b 588 isa_ok( $v, 'SQL::Translator::Object::View', 'View' );
f0e701d3 589 isa_ok( $v->schema, 'SQL::Translator::Object::Schema', 'Schema' );
590 is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
591 is( $v->name, $name, qq[Name is "$name"] );
592 is( $v->sql, $sql, qq[Name is "$sql"] );
593 is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );
594
595 my @views = $s->get_views;
596 is( scalar @views, 1, 'Number of views is 1' );
597
598 my $v1 = $s->get_view( $name );
a36eae8b 599 isa_ok( $v1, 'SQL::Translator::Object::View', 'View' );
f0e701d3 600 is( $v1->name, $name, qq[Name is "$name"] );
601}
602
603#
604# Trigger
605#
606{
607 my $s = SQL::Translator::Object::Schema->new(name => 'TrigTest');
a36eae8b 608 $s->add_table({ name => 'foo' }) or die "Couldn't create table: ", $s->error;
f0e701d3 609 my $name = 'foo_trigger';
610 my $perform_action_when = 'after';
611 my $database_events = 'insert';
612 my $on_table = 'foo';
613 my $action = 'update modified=timestamp();';
a36eae8b 614 my $t = $s->add_trigger({
f0e701d3 615 name => $name,
616 perform_action_when => $perform_action_when,
617 database_events => $database_events,
618 on_table => $on_table,
619 action => $action,
a36eae8b 620 }) or die $s->error;
f0e701d3 621
a36eae8b 622 isa_ok( $t, 'SQL::Translator::Object::Trigger', 'Trigger' );
f0e701d3 623 isa_ok( $t->schema, 'SQL::Translator::Object::Schema', 'Schema' );
624 is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
625 is( $t->name, $name, qq[Name is "$name"] );
626 is( $t->perform_action_when, $perform_action_when,
627 qq[Perform action when is "$perform_action_when"] );
628 is( join(',', $t->database_events), $database_events,
629 qq[Database event is "$database_events"] );
630 isa_ok( $t->table, 'SQL::Translator::Object::Table', qq[table is a Table"] );
631 is( $t->action, $action, qq[Action is "$action"] );
632
633 my @triggs = $s->get_triggers;
634 is( scalar @triggs, 1, 'Number of triggers is 1' );
635
636 my $t1 = $s->get_trigger( $name );
637 isa_ok( $t1, 'SQL::Translator::Object::Trigger', 'Trigger' );
638 is( $t1->name, $name, qq[Name is "$name"] );
639
640
641
642 my $s2 = SQL::Translator::Object::Schema->new(name => 'TrigTest2');
8977e30a 643 $s2->add_table({ name => 'foo' }) or die "Couldn't create table: ", $s2->error;
644 my $t2 = $s2->add_trigger({
f0e701d3 645 name => 'foo_trigger',
646 perform_action_when => 'after',
647 database_events => [qw/insert update/],
648 on_table => 'foo',
649 action => 'update modified=timestamp();',
8977e30a 650 }) or die $s2->error;
a36eae8b 651 isa_ok( $t2, 'SQL::Translator::Object::Trigger', 'Trigger' );
f0e701d3 652 isa_ok( $t2->schema, 'SQL::Translator::Object::Schema', 'Schema' );
653 is( $t2->schema->name, 'TrigTest2', qq[Schema name is "'TrigTest2'"] );
654 is( $t2->name, 'foo_trigger', qq[Name is "foo_trigger"] );
655 is_deeply(
656 [$t2->database_events],
657 [qw/insert update/],
658 "Database events are [qw/insert update/] "
659 );
660 isa_ok($t2->database_events,'ARRAY','Database events');
661
662 #
663 # Trigger equal tests
664 #
8977e30a 665# isnt(
666# $t1->equals($t2),
667# 1,
668# 'Compare two Triggers with database_event and database_events'
669# );
f0e701d3 670
671 $t1->database_events($database_events);
672 $t2->database_events($database_events);
8977e30a 673# is($t1->equals($t2),1,'Compare two Triggers with database_event');
f0e701d3 674
675 $t2->database_events('');
676 $t1->database_events([qw/update insert/]);
677 $t2->database_events([qw/insert update/]);
8977e30a 678# is($t1->equals($t2),1,'Compare two Triggers with database_events');
f0e701d3 679
680 #
681 # $schema-> drop_trigger
682 #
683 my $dropped_trig = $s->drop_trigger($t->name);
a36eae8b 684 isa_ok($dropped_trig, 'SQL::Translator::Object::Trigger', 'Dropped trigger "foo_trigger"' );
f0e701d3 685 $s->add_trigger($t);
686 my $dropped_trig2 = $s->drop_trigger($t);
a36eae8b 687 isa_ok($dropped_trig2, 'SQL::Translator::Object::Trigger', 'Dropped trigger "foo_trigger" by object' );
f0e701d3 688 is($dropped_trig2->name, $t->name, 'Dropped correct trigger "foo_trigger"');
689 my $dropped_trig3 = $s->drop_trigger($t->name);
690 like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant trigger "foo_trigger"] );
691
692 $s->add_trigger($t);
693}
694
695#
696# Procedure
697#
698{
699 my $s = SQL::Translator::Object::Schema->new( name => 'ProcTest' );
700 my $name = 'foo_proc';
701 my $sql = 'select foo from bar';
702 my $parameters = 'foo, bar';
703 my $owner = 'Nomar';
704 my $comments = 'Go Sox!';
705 my $p = $s->add_procedure(
706 name => $name,
707 sql => $sql,
708 parameters => $parameters,
709 owner => $owner,
710 comments => $comments,
711 ) or die $s->error;
712
a36eae8b 713 isa_ok( $p, 'SQL::Translator::Object::Procedure', 'Procedure' );
f0e701d3 714 isa_ok( $p->schema, 'SQL::Translator::Object::Schema', 'Schema' );
715 is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
716 is( $p->name, $name, qq[Name is "$name"] );
717 is( $p->sql, $sql, qq[SQL is "$sql"] );
718 is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
719 is( $p->comments, $comments, qq[Comments = "$comments"] );
720
721 my @procs = $s->get_procedures;
722 is( scalar @procs, 1, 'Number of procedures is 1' );
723
724 my $p1 = $s->get_procedure( $name );
a36eae8b 725 isa_ok( $p1, 'SQL::Translator::Object::Procedure', 'Procedure' );
f0e701d3 726 is( $p1->name, $name, qq[Name is "$name"] );
727
728 #
729 # $schema-> drop_procedure
730 #
731 my $dropped_proc = $s->drop_procedure($p->name);
a36eae8b 732 isa_ok($dropped_proc, 'SQL::Translator::Object::Procedure', 'Dropped procedure "foo_proc"' );
f0e701d3 733 $s->add_procedure($p);
734 my $dropped_proc2 = $s->drop_procedure($p);
a36eae8b 735 isa_ok($dropped_proc2, 'SQL::Translator::Object::Procedure', 'Dropped procedure "foo_proc" by object' );
f0e701d3 736 is($dropped_proc2->name, $p->name, 'Dropped correct procedure "foo_proc"');
737 my $dropped_proc3 = $s->drop_procedure($p->name);
738 like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant procedure "foo_proc"] );
739
740 $s->add_procedure($p);
741}