Commit | Line | Data |
4d878d2f |
1 | #!/usr/bin/perl |
2 | |
586fba3f |
3 | $| = 1; |
4 | |
4d878d2f |
5 | use strict; |
6 | use Test::More 'no_plan'; # plans => 1; |
07680493 |
7 | use SQL::Translator::Schema::Constants; |
4d878d2f |
8 | |
9 | require_ok( 'SQL::Translator::Schema' ); |
10 | |
07680493 |
11 | { |
12 | # |
13 | # Schema |
14 | # |
15 | my $schema = SQL::Translator::Schema->new( |
16 | name => 'foo', |
17 | database => 'MySQL', |
18 | ); |
19 | isa_ok( $schema, 'SQL::Translator::Schema' ); |
4d878d2f |
20 | |
07680493 |
21 | is( $schema->name, 'foo', 'Schema name is "foo"' ); |
22 | is( $schema->name('bar'), 'bar', 'Schema name changed to "bar"' ); |
4d878d2f |
23 | |
07680493 |
24 | is( $schema->database, 'MySQL', 'Schema database is "MySQL"' ); |
25 | is( $schema->database('PostgreSQL'), 'PostgreSQL', |
26 | 'Schema database changed to "PostgreSQL"' ); |
4d878d2f |
27 | |
07680493 |
28 | is( $schema->is_valid, undef, 'Schema not valid...' ); |
29 | like( $schema->error, qr/no tables/i, '...because there are no tables' ); |
4d878d2f |
30 | |
07680493 |
31 | # |
32 | # $schema->add_* |
33 | # |
34 | my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error; |
35 | isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Table "foo"' ); |
4d878d2f |
36 | |
07680493 |
37 | my $bar_table = SQL::Translator::Schema::Table->new( name => 'bar' ) or |
38 | warn SQL::Translator::Schema::Table->error; |
39 | $bar_table = $schema->add_table( $bar_table ); |
40 | isa_ok( $bar_table, 'SQL::Translator::Schema::Table', 'Table "bar"' ); |
41 | is( $bar_table->name, 'bar', 'Add table "bar"' ); |
4d878d2f |
42 | |
07680493 |
43 | $schema = $bar_table->schema( $schema ); |
44 | isa_ok( $schema, 'SQL::Translator::Schema', 'Schema' ); |
4d878d2f |
45 | |
07680493 |
46 | is( $bar_table->name('foo'), undef, |
47 | q[Can't change name of table "bar" to "foo"...]); |
48 | like( $bar_table->error, qr/can't use table name/i, |
49 | q[...because "foo" exists] ); |
4d878d2f |
50 | |
07680493 |
51 | my $redundant_table = $schema->add_table(name => 'foo'); |
52 | is( $redundant_table, undef, qq[Can't create another "foo" table...] ); |
53 | like( $schema->error, qr/can't use table name/i, '... because "foo" exists' ); |
54 | |
55 | # |
56 | # Table default new |
57 | # |
58 | is( $foo_table->name, 'foo', 'Table name is "foo"' ); |
59 | is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' ); |
60 | |
61 | my $fields = $foo_table->get_fields; |
62 | is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' ); |
63 | like( $foo_table->error, qr/no fields/i, 'Error for no fields' ); |
64 | |
65 | # |
66 | # New table with args |
67 | # |
68 | my $person_table = $schema->add_table( name => 'person' ); |
69 | is( $person_table->name, 'person', 'Table name is "person"' ); |
70 | is( $person_table->is_valid, undef, 'Table is not yet valid' ); |
71 | |
72 | # |
73 | # Field default new |
74 | # |
75 | my $f1 = $person_table->add_field(name => 'foo') or |
76 | warn $person_table->error; |
77 | isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' ); |
78 | is( $f1->name, 'foo', 'Field name is "foo"' ); |
79 | is( $f1->data_type, '', 'Field data type is blank' ); |
80 | is( $f1->size, 0, 'Field size is "0"' ); |
81 | is( $f1->is_primary_key, '0', 'Field is_primary_key is false' ); |
82 | is( $f1->nullable, 1, 'Field can be NULL' ); |
83 | is( $f1->default_value, undef, 'Field default is undefined' ); |
84 | |
85 | my $f2 = SQL::Translator::Schema::Field->new (name => 'f2') or |
86 | warn SQL::Translator::Schema::Field->error; |
87 | $f2 = $person_table->add_field( $f2 ); |
88 | isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' ); |
89 | is( $f2->name, 'f2', 'Add field "f2"' ); |
90 | is( $f2->nullable(0), 0, 'Field cannot be NULL' ); |
91 | is( $f2->nullable(''), 0, 'Field cannot be NULL' ); |
92 | is( $f2->nullable('0'), 0, 'Field cannot be NULL' ); |
93 | is( $f1->default_value(''), '', 'Field default is empty string' ); |
94 | |
95 | $person_table = $f2->table( $person_table ); |
96 | isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' ); |
97 | |
98 | is( $f2->name('foo'), undef, q[Can't set field name of "f2" to "foo"...] ); |
99 | like( $f2->error, qr/can't use field name/i, '...because name exists' ); |
100 | |
101 | my $redundant_field = $person_table->add_field(name => 'f2'); |
102 | is( $redundant_field, undef, qq[Didn't create another "f2" field...] ); |
103 | like( $person_table->error, qr/can't create field/i, |
104 | '... because it exists' ); |
105 | |
106 | # |
107 | # Field methods |
108 | # |
109 | is( $f1->name('person_name'), 'person_name', |
110 | 'Field name is "person_name"' ); |
111 | is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' ); |
112 | is( $f1->size('30'), '30', 'Field size is "30"' ); |
113 | is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' ); |
114 | |
115 | # |
116 | # New field with args |
117 | # |
118 | my $age = $person_table->add_field( |
119 | name => 'age', |
120 | data_type => 'float', |
121 | size => '10,2', |
122 | ); |
123 | is( $age->name, 'age', 'Field name is "age"' ); |
124 | is( $age->data_type, 'float', 'Field data type is "float"' ); |
125 | is( $age->size, '10,2', 'Field size is "10,2"' ); |
126 | is( $age->size(10,2), '10,2', 'Field size still "10,2"' ); |
127 | is( $age->size([10,2]), '10,2', 'Field size still "10,2"' ); |
128 | is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' ); |
129 | |
130 | # |
131 | # Index |
132 | # |
133 | my @indices = $person_table->get_indices; |
134 | is( scalar @indices, 0, 'No indices' ); |
135 | like( $person_table->error, qr/no indices/i, 'Error for no indices' ); |
136 | my $index1 = $person_table->add_index( name => "foo" ) |
137 | or warn $person_table->error; |
138 | isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' ); |
139 | is( $index1->name, 'foo', 'Index name is "foo"' ); |
140 | |
141 | my $index2 = SQL::Translator::Schema::Index->new( name => "bar" ) |
142 | or warn SQL::Translator::Schema::Index->error; |
143 | $index2 = $person_table->add_index( $index2 ); |
144 | isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' ); |
145 | is( $index2->name, 'bar', 'Index name is "bar"' ); |
146 | |
147 | my $indices = $person_table->get_indices; |
148 | is( scalar @$indices, 2, 'Two indices' ); |
149 | is( $indices->[0]->name, 'foo', '"foo" index' ); |
150 | is( $indices->[1]->name, 'bar', '"bar" index' ); |
151 | |
152 | # |
153 | # Constraint |
154 | # |
155 | my @constraints = $person_table->get_constraints; |
156 | is( scalar @constraints, 0, 'No constraints' ); |
157 | like( $person_table->error, qr/no constraints/i, |
158 | 'Error for no constraints' ); |
159 | my $constraint1 = $person_table->add_constraint( name => 'foo' ) |
160 | or warn $person_table->error; |
161 | isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' ); |
162 | is( $constraint1->name, 'foo', 'Constraint name is "foo"' ); |
163 | |
164 | $fields = join(',', $constraint1->fields('id') ); |
165 | is( $fields, 'id', 'Constraint field = "id"' ); |
166 | |
167 | $fields = join(',', $constraint1->fields('id,id') ); |
168 | is( $fields, 'id', 'Constraint field = "id"' ); |
169 | |
170 | $fields = join(',', $constraint1->fields('id', 'name') ); |
171 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
172 | |
173 | $fields = join(',', $constraint1->fields( 'id,name,id' ) ); |
174 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
175 | |
176 | $fields = join(',', $constraint1->fields( 'id, name' ) ); |
177 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
178 | |
179 | $fields = join(',', $constraint1->fields( [ 'id', 'name' ] ) ); |
180 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
181 | |
182 | $fields = join(',', $constraint1->fields( qw[ id name ] ) ); |
183 | is( $fields, 'id,name', 'Constraint field = "id,name"' ); |
184 | |
185 | my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' ); |
186 | $constraint2 = $person_table->add_constraint( $constraint2 ); |
187 | isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' ); |
188 | is( $constraint2->name, 'bar', 'Constraint name is "bar"' ); |
189 | |
190 | my $constraints = $person_table->get_constraints; |
191 | is( scalar @$constraints, 2, 'Two constraints' ); |
192 | is( $constraints->[0]->name, 'foo', '"foo" constraint' ); |
193 | is( $constraints->[1]->name, 'bar', '"bar" constraint' ); |
194 | |
195 | # |
196 | # View |
197 | # |
198 | my $view = $schema->add_view( name => 'view1' ) or warn $schema->error; |
199 | isa_ok( $view, 'SQL::Translator::Schema::View', 'View' ); |
200 | my $view_sql = 'select * from table'; |
201 | is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' ); |
202 | |
203 | my $view2 = SQL::Translator::Schema::View->new(name => 'view2') or |
204 | warn SQL::Translator::Schema::View->error; |
205 | my $check_view = $schema->add_view( $view2 ); |
206 | is( $check_view->name, 'view2', 'Add view "view2"' ); |
207 | |
208 | my $redundant_view = $schema->add_view(name => 'view2'); |
209 | is( $redundant_view, undef, qq[Didn't create another "view2" view...] ); |
210 | like( $schema->error, qr/can't create view/i, '... because it exists' ); |
211 | |
212 | # |
213 | # $schema->get_* |
214 | # |
215 | my $bad_table = $schema->get_table; |
216 | like( $schema->error, qr/no table/i, 'Error on no arg to get_table' ); |
217 | |
218 | $bad_table = $schema->get_table('baz'); |
219 | like( $schema->error, qr/does not exist/i, |
220 | 'Error on bad arg to get_table' ); |
221 | |
222 | my $bad_view = $schema->get_view; |
223 | like( $schema->error, qr/no view/i, 'Error on no arg to get_view' ); |
224 | |
225 | $bad_view = $schema->get_view('bar'); |
226 | like( $schema->error, qr/does not exist/i, |
227 | 'Error on bad arg to get_view' ); |
228 | |
229 | my $good_table = $schema->get_table('foo'); |
230 | isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' ); |
231 | |
232 | my $good_view = $schema->get_view('view1'); |
233 | isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' ); |
234 | is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' ); |
235 | |
236 | # |
237 | # $schema->get_*s |
238 | # |
239 | my @tables = $schema->get_tables; |
240 | is( scalar @tables, 3, 'Found 2 tables' ); |
241 | |
242 | my @views = $schema->get_views; |
243 | is( scalar @views, 2, 'Found 1 view' ); |
244 | } |
4d878d2f |
245 | |
246 | # |
07680493 |
247 | # Test ability to introspect some values |
4d878d2f |
248 | # |
07680493 |
249 | { |
250 | my $s = SQL::Translator::Schema->new( |
251 | name => 'foo', |
252 | database => 'PostgreSQL', |
253 | ); |
254 | my $t = $s->add_table( name => 'person' ) or warn $s->erro; |
255 | my $f = $t->add_field( name => 'person_id' ) or warn $t->error; |
256 | $f->data_type('serial'); |
257 | |
258 | my $c = $t->add_constraint( |
259 | type => PRIMARY_KEY, |
260 | fields => 'person_id', |
261 | ) or warn $t->error; |
262 | |
263 | is( $f->is_primary_key, 1, 'Field is PK' ); |
264 | is( $f->is_auto_increment, 1, 'Field is auto inc' ); |
265 | } |
4d878d2f |
266 | |
267 | # |
07680493 |
268 | # FK constraint validity |
4d878d2f |
269 | # |
07680493 |
270 | { |
271 | my $s = SQL::Translator::Schema->new; |
272 | my $t = $s->add_table( name => 'person' ) or warn $s->error; |
273 | my $c = $t->add_constraint or warn $t->error; |
274 | |
275 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
276 | like( $c->error, qr/no type/i, '...because it has no type' ); |
277 | |
278 | is( $c->type( FOREIGN_KEY ), FOREIGN_KEY, 'Constraint type now a FK' ); |
279 | |
280 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
281 | like( $c->error, qr/no fields/i, '...because it has no fields' ); |
282 | |
283 | is( join('', $c->fields('foo')), 'foo', 'Fields now = "foo"' ); |
284 | |
285 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
286 | like( $c->error, qr/non-existent field/i, |
287 | q[...because field "foo" doesn't exist] ); |
288 | |
289 | my $fk = $t->add_field( name => 'pet_id' ); |
290 | is( $fk->name, 'pet_id', 'Added field "pet_id"' ); |
291 | is( join('', $c->fields('pet_id')), 'pet_id', 'Fields now = "pet_id"' ); |
292 | |
293 | $t->add_field( name => 'f1' ); |
294 | $t->add_field( name => 'f2' ); |
295 | is( join(',', $c->fields('f1,f2')), 'f1,f2', 'Fields now = "f1,f2"' ); |
296 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
297 | like( $c->error, qr/only one field/i, |
298 | q[...because too many fields for FK] ); |
299 | |
300 | $c->fields('f1'); |
301 | |
302 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
303 | like( $c->error, qr/no reference table/i, |
304 | q[...because there's no reference table] ); |
305 | |
306 | is( $c->reference_table('foo'), 'foo', 'Reference table now = "foo"' ); |
307 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
308 | like( $c->error, qr/no table named/i, |
309 | q[...because reference table "foo" doesn't exist] ); |
310 | |
311 | my $t2 = $s->add_table( name => 'pet' ); |
312 | is( $t2->name, 'pet', 'Added "pet" table' ); |
313 | |
314 | is( $c->reference_table('pet'), 'pet', 'Reference table now = "pet"' ); |
315 | |
316 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
317 | like( $c->error, qr/no reference fields/i, |
318 | q[...because there're no reference fields]); |
319 | |
320 | is( join('', $c->reference_fields('pet_id')), 'pet_id', |
321 | 'Reference fields = "pet_id"' ); |
322 | |
323 | is( $c->is_valid, undef, 'Constraint on "person" not valid...'); |
324 | like( $c->error, qr/non-existent field/i, |
325 | q[...because there's no "pet_id" field in "pet"]); |
326 | |
327 | my $pet_id = $t2->add_field( name => 'pet_id' ); |
328 | is( $pet_id->name, 'pet_id', 'Added field "pet_id"' ); |
329 | |
330 | is( $c->is_valid, 1, 'Constraint now valid' ); |
331 | |
332 | } |
333 | exit; |
586fba3f |
334 | |
335 | # |
07680493 |
336 | # $table->primary_key test |
586fba3f |
337 | # |
07680493 |
338 | { |
339 | my $s = SQL::Translator::Schema->new; |
340 | my $t = $s->add_table( name => 'person' ); |
341 | |
342 | is( $t->primary_key, undef, 'No primary key' ); |
343 | like( $t->error, qr/no primary key/i, 'No primary key error' ); |
586fba3f |
344 | |
07680493 |
345 | is( $t->primary_key('person_id'), undef, |
346 | q[Can't make PK on "person_id"...] ); |
347 | like( $t->error, qr/invalid field/i, "...because it doesn't exist" ); |
586fba3f |
348 | |
07680493 |
349 | $t->add_field( name => 'person_id' ); |
350 | my $c = $t->primary_key('person_id'); |
586fba3f |
351 | |
07680493 |
352 | isa_ok( $c, 'SQL::Translator::Schema::Constraint', 'Constraint' ); |
586fba3f |
353 | |
07680493 |
354 | is( join('', $c->fields), 'person_id', 'Constraint now on "person_id"' ); |
586fba3f |
355 | |
07680493 |
356 | $t->add_field( name => 'name' ); |
357 | $c = $t->primary_key('name'); |
358 | is( join(',', $c->fields), 'person_id,name', |
359 | 'Constraint now on "person_id" and "name"' ); |
360 | |
361 | is( scalar @{ $t->get_constraints }, 1, 'Found 1 constraint' ); |
362 | } |
586fba3f |
363 | |
364 | # |
07680493 |
365 | # FK finds PK |
586fba3f |
366 | # |
07680493 |
367 | { |
368 | my $s = SQL::Translator::Schema->new; |
369 | my $t1 = $s->add_table( name => 'person' ); |
370 | my $t2 = $s->add_table( name => 'pet' ); |
371 | $t1->add_field( name => 'id' ); |
372 | my $c1 = $t1->primary_key( 'id' ) or warn $t1->error; |
373 | is( $c1->type, PRIMARY_KEY, 'Made "person_id" PK on "person"' ); |
374 | |
375 | $t2->add_field( name => 'person_id' ); |
376 | my $c2 = $t2->add_constraint( |
377 | type => PRIMARY_KEY, |
378 | fields => 'person_id', |
379 | reference_table => 'person', |
380 | ); |
586fba3f |
381 | |
07680493 |
382 | is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' ); |
383 | } |