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