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