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