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