Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
637ca936 |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
637ca936 |
7 | |
8 | eval "use SQL::Translator"; |
9 | plan skip_all => 'SQL::Translator required' if $@; |
10 | |
661fc8eb |
11 | my $schema = DBICTest->init_schema; |
637ca936 |
12 | |
9c1f7965 |
13 | plan tests => 130; |
637ca936 |
14 | |
661fc8eb |
15 | my $translator = SQL::Translator->new( |
16 | parser_args => { |
17 | 'DBIx::Schema' => $schema, |
18 | }, |
19 | producer_args => {}, |
637ca936 |
20 | ); |
21 | |
22 | $translator->parser('SQL::Translator::Parser::DBIx::Class'); |
23 | $translator->producer('SQLite'); |
24 | |
25 | my $output = $translator->translate(); |
26 | |
256e87b0 |
27 | |
28 | ok($output, "SQLT produced someoutput") |
29 | or diag($translator->error); |
30 | |
b1edf9f9 |
31 | # Note that the constraints listed here are the only ones that are tested -- if |
32 | # more exist in the Schema than are listed here and all listed constraints are |
c75b18e9 |
33 | # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN |
34 | # KEY constraints to DBICTest::Schema, add tests here if you think the existing |
35 | # test coverage is not sufficient |
b1edf9f9 |
36 | |
37 | my %fk_constraints = ( |
661fc8eb |
38 | |
39 | # TwoKeys |
b1edf9f9 |
40 | twokeys => [ |
41 | { |
42 | 'display' => 'twokeys->cd', |
bb0f01d0 |
43 | 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd', |
b1edf9f9 |
44 | 'selftable' => 'twokeys', 'foreigntable' => 'cd', |
45 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
9c1f7965 |
46 | 'noindex' => 1, |
13de943d |
47 | on_delete => '', on_update => '', deferrable => 0, |
b1edf9f9 |
48 | }, |
49 | { |
50 | 'display' => 'twokeys->artist', |
bb0f01d0 |
51 | 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist', |
b1edf9f9 |
52 | 'selftable' => 'twokeys', 'foreigntable' => 'artist', |
53 | 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], |
e394339b |
54 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
55 | }, |
56 | ], |
661fc8eb |
57 | |
58 | # FourKeys_to_TwoKeys |
b1edf9f9 |
59 | fourkeys_to_twokeys => [ |
60 | { |
61 | 'display' => 'fourkeys_to_twokeys->twokeys', |
f34cb1fd |
62 | 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd', |
b1edf9f9 |
63 | 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys', |
64 | 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'], |
e394339b |
65 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
66 | }, |
67 | { |
f34cb1fd |
68 | 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye', |
d1b264d3 |
69 | 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye', |
b1edf9f9 |
70 | 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys', |
71 | 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)], |
72 | 'foreigncols' => [qw(foo bar hello goodbye)], |
e394339b |
73 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
74 | }, |
75 | ], |
661fc8eb |
76 | |
77 | # CD_to_Producer |
b1edf9f9 |
78 | cd_to_producer => [ |
79 | { |
80 | 'display' => 'cd_to_producer->cd', |
bb0f01d0 |
81 | 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd', |
b1edf9f9 |
82 | 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', |
83 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
e394339b |
84 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
85 | }, |
86 | { |
87 | 'display' => 'cd_to_producer->producer', |
bb0f01d0 |
88 | 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer', |
b1edf9f9 |
89 | 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer', |
90 | 'selfcols' => ['producer'], 'foreigncols' => ['producerid'], |
e394339b |
91 | on_delete => '', on_update => '', deferrable => 1, |
b1edf9f9 |
92 | }, |
93 | ], |
661fc8eb |
94 | |
95 | # Self_ref_alias |
b1edf9f9 |
96 | self_ref_alias => [ |
97 | { |
98 | 'display' => 'self_ref_alias->self_ref for self_ref', |
bb0f01d0 |
99 | 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref', |
b1edf9f9 |
100 | 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', |
101 | 'selfcols' => ['self_ref'], 'foreigncols' => ['id'], |
e394339b |
102 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
103 | }, |
104 | { |
105 | 'display' => 'self_ref_alias->self_ref for alias', |
bb0f01d0 |
106 | 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias', |
b1edf9f9 |
107 | 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', |
108 | 'selfcols' => ['alias'], 'foreigncols' => ['id'], |
e394339b |
109 | on_delete => '', on_update => '', deferrable => 1, |
b1edf9f9 |
110 | }, |
111 | ], |
661fc8eb |
112 | |
113 | # CD |
b1edf9f9 |
114 | cd => [ |
115 | { |
116 | 'display' => 'cd->artist', |
bb0f01d0 |
117 | 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist', |
b1edf9f9 |
118 | 'selftable' => 'cd', 'foreigntable' => 'artist', |
119 | 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], |
13de943d |
120 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
121 | }, |
122 | ], |
661fc8eb |
123 | |
124 | # Artist_undirected_map |
b1edf9f9 |
125 | artist_undirected_map => [ |
126 | { |
127 | 'display' => 'artist_undirected_map->artist for id1', |
bb0f01d0 |
128 | 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1', |
b1edf9f9 |
129 | 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', |
130 | 'selfcols' => ['id1'], 'foreigncols' => ['artistid'], |
e394339b |
131 | on_delete => 'CASCADE', on_update => '', deferrable => 1, |
b1edf9f9 |
132 | }, |
133 | { |
134 | 'display' => 'artist_undirected_map->artist for id2', |
bb0f01d0 |
135 | 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2', |
b1edf9f9 |
136 | 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', |
137 | 'selfcols' => ['id2'], 'foreigncols' => ['artistid'], |
e394339b |
138 | on_delete => 'CASCADE', on_update => '', deferrable => 1, |
b1edf9f9 |
139 | }, |
140 | ], |
661fc8eb |
141 | |
142 | # Track |
b1edf9f9 |
143 | track => [ |
144 | { |
145 | 'display' => 'track->cd', |
bb0f01d0 |
146 | 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd', |
b1edf9f9 |
147 | 'selftable' => 'track', 'foreigntable' => 'cd', |
148 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
e394339b |
149 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
150 | }, |
151 | ], |
661fc8eb |
152 | |
153 | # TreeLike |
b1edf9f9 |
154 | treelike => [ |
155 | { |
156 | 'display' => 'treelike->treelike for parent', |
61177e44 |
157 | 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent', |
b1edf9f9 |
158 | 'selftable' => 'treelike', 'foreigntable' => 'treelike', |
61177e44 |
159 | 'selfcols' => ['parent'], 'foreigncols' => ['id'], |
e394339b |
160 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
161 | }, |
162 | ], |
163 | |
164 | # TwoKeyTreeLike |
165 | twokeytreelike => [ |
166 | { |
167 | 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2', |
bb0f01d0 |
168 | 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2', |
b1edf9f9 |
169 | 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike', |
170 | 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'], |
e394339b |
171 | on_delete => '', on_update => '', deferrable => 1, |
b1edf9f9 |
172 | }, |
173 | ], |
ae515736 |
174 | |
661fc8eb |
175 | # Tags |
b1edf9f9 |
176 | tags => [ |
177 | { |
178 | 'display' => 'tags->cd', |
bb0f01d0 |
179 | 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd', |
b1edf9f9 |
180 | 'selftable' => 'tags', 'foreigntable' => 'cd', |
181 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
e394339b |
182 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
183 | }, |
184 | ], |
661fc8eb |
185 | |
186 | # Bookmark |
b1edf9f9 |
187 | bookmark => [ |
188 | { |
189 | 'display' => 'bookmark->link', |
bb0f01d0 |
190 | 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link', |
b1edf9f9 |
191 | 'selftable' => 'bookmark', 'foreigntable' => 'link', |
192 | 'selfcols' => ['link'], 'foreigncols' => ['id'], |
e394339b |
193 | on_delete => '', on_update => '', deferrable => 1, |
b1edf9f9 |
194 | }, |
195 | ], |
a0024650 |
196 | # ForceForeign |
197 | forceforeign => [ |
198 | { |
199 | 'display' => 'forceforeign->artist', |
bb0f01d0 |
200 | 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist', |
a0024650 |
201 | 'selftable' => 'forceforeign', 'foreigntable' => 'artist', |
8871d4ad |
202 | 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], |
e394339b |
203 | on_delete => '', on_update => '', deferrable => 1, |
a0024650 |
204 | }, |
205 | ], |
206 | |
b1edf9f9 |
207 | ); |
208 | |
209 | my %unique_constraints = ( |
210 | # CD |
211 | cd => [ |
212 | { |
213 | 'display' => 'cd artist and title unique', |
0da8b7da |
214 | 'name' => 'cd_artist_title', |
b1edf9f9 |
215 | 'table' => 'cd', 'cols' => ['artist', 'title'], |
216 | }, |
217 | ], |
218 | |
219 | # Producer |
220 | producer => [ |
221 | { |
222 | 'display' => 'producer name unique', |
0da8b7da |
223 | 'name' => 'prod_name', # explicit name |
b1edf9f9 |
224 | 'table' => 'producer', 'cols' => ['name'], |
225 | }, |
226 | ], |
227 | |
228 | # TwoKeyTreeLike |
229 | twokeytreelike => [ |
230 | { |
231 | 'display' => 'twokeytreelike name unique', |
0da8b7da |
232 | 'name' => 'tktlnameunique', # explicit name |
b1edf9f9 |
233 | 'table' => 'twokeytreelike', 'cols' => ['name'], |
234 | }, |
235 | ], |
236 | |
237 | # Employee |
238 | # Constraint is commented out in DBICTest/Schema/Employee.pm |
239 | # employee => [ |
240 | # { |
241 | # 'display' => 'employee position and group_id unique', |
0da8b7da |
242 | # 'name' => 'position_group', |
b1edf9f9 |
243 | # 'table' => 'employee', cols => ['position', 'group_id'], |
244 | # }, |
245 | # ], |
7b90bb13 |
246 | ); |
247 | |
17cab2f0 |
248 | my %indexes = ( |
c385ecea |
249 | artist => [ |
250 | { |
251 | 'fields' => ['name'] |
252 | }, |
253 | ] |
254 | ); |
255 | |
637ca936 |
256 | my $tschema = $translator->schema(); |
d6c79cb3 |
257 | # Test that the $schema->sqlt_deploy_hook was called okay and that it removed |
458e0292 |
258 | # the 'dummy' table |
259 | ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook"); |
d6c79cb3 |
260 | |
b1edf9f9 |
261 | # Test that nonexistent constraints are not found |
262 | my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']); |
263 | ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' ); |
264 | $constraint = get_constraint('UNIQUE', 'cd', ['artist']); |
265 | ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' ); |
a0024650 |
266 | $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']); |
267 | ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' ); |
b1edf9f9 |
268 | |
269 | for my $expected_constraints (keys %fk_constraints) { |
270 | for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) { |
271 | my $desc = $expected_constraint->{display}; |
272 | my $constraint = get_constraint( |
273 | 'FOREIGN KEY', |
274 | $expected_constraint->{selftable}, $expected_constraint->{selfcols}, |
275 | $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols}, |
276 | ); |
277 | ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" ); |
278 | test_fk($expected_constraint, $constraint); |
661fc8eb |
279 | } |
637ca936 |
280 | } |
281 | |
b1edf9f9 |
282 | for my $expected_constraints (keys %unique_constraints) { |
283 | for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) { |
284 | my $desc = $expected_constraint->{display}; |
285 | my $constraint = get_constraint( |
286 | 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols}, |
287 | ); |
288 | ok( defined($constraint), "UNIQUE constraint matching `$desc' found" ); |
0da8b7da |
289 | test_unique($expected_constraint, $constraint); |
b1edf9f9 |
290 | } |
637ca936 |
291 | } |
292 | |
17cab2f0 |
293 | for my $table_index (keys %indexes) { |
294 | for my $expected_index ( @{ $indexes{$table_index} } ) { |
c385ecea |
295 | |
296 | ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table"); |
297 | } |
298 | } |
299 | |
b1edf9f9 |
300 | # Returns the Constraint object for the specified constraint type, table and |
301 | # columns from the SQL::Translator schema, or undef if no matching constraint |
302 | # is found. |
303 | # |
304 | # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last |
305 | # two parameters are not used. |
306 | sub get_constraint { |
307 | my ($type, $table_name, $cols, $f_table, $f_cols) = @_; |
308 | $f_table ||= ''; # For UNIQUE constraints, reference_table is '' |
309 | $f_cols ||= []; |
310 | |
311 | my $table = $tschema->get_table($table_name); |
312 | |
313 | my %fields = map { $_ => 1 } @$cols; |
314 | my %f_fields = map { $_ => 1 } @$f_cols; |
315 | |
a7e65bb5 |
316 | die "No $table_name" unless $table; |
b1edf9f9 |
317 | CONSTRAINT: |
318 | for my $constraint ( $table->get_constraints ) { |
319 | next unless $constraint->type eq $type; |
320 | next unless $constraint->reference_table eq $f_table; |
321 | |
322 | my %rev_fields = map { $_ => 1 } $constraint->fields; |
323 | my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields; |
324 | |
325 | # Check that the given fields are a subset of the constraint's fields |
326 | for my $field ($constraint->fields) { |
327 | next CONSTRAINT unless $fields{$field}; |
328 | } |
329 | if ($type eq 'FOREIGN KEY') { |
330 | for my $f_field ($constraint->reference_fields) { |
331 | next CONSTRAINT unless $f_fields{$f_field}; |
661fc8eb |
332 | } |
b1edf9f9 |
333 | } |
661fc8eb |
334 | |
b1edf9f9 |
335 | # Check that the constraint's fields are a subset of the given fields |
336 | for my $field (@$cols) { |
337 | next CONSTRAINT unless $rev_fields{$field}; |
338 | } |
339 | if ($type eq 'FOREIGN KEY') { |
340 | for my $f_field (@$f_cols) { |
341 | next CONSTRAINT unless $rev_f_fields{$f_field}; |
661fc8eb |
342 | } |
343 | } |
b1edf9f9 |
344 | |
345 | return $constraint; # everything passes, found the constraint |
661fc8eb |
346 | } |
b1edf9f9 |
347 | return undef; # didn't find a matching constraint |
7b90bb13 |
348 | } |
349 | |
c385ecea |
350 | sub get_index { |
351 | my ($table_name, $index) = @_; |
352 | |
353 | my $table = $tschema->get_table($table_name); |
354 | |
355 | CAND_INDEX: |
356 | for my $cand_index ( $table->get_indices ) { |
357 | |
358 | next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name} |
359 | || $index->{type} && $cand_index->type ne $index->{type}; |
360 | |
361 | my %idx_fields = map { $_ => 1 } $cand_index->fields; |
362 | |
363 | for my $field ( @{ $index->{fields} } ) { |
364 | next CAND_INDEX unless $idx_fields{$field}; |
365 | } |
366 | |
367 | %idx_fields = map { $_ => 1 } @{$index->{fields}}; |
368 | for my $field ( $cand_index->fields) { |
369 | next CAND_INDEX unless $idx_fields{$field}; |
370 | } |
371 | |
372 | return $cand_index; |
373 | } |
374 | |
375 | return undef; # No matching idx |
376 | } |
377 | |
b1edf9f9 |
378 | # Test parameters in a FOREIGN KEY constraint other than columns |
379 | sub test_fk { |
380 | my ($expected, $got) = @_; |
381 | my $desc = $expected->{display}; |
0da8b7da |
382 | is( $got->name, $expected->{name}, |
383 | "name parameter correct for `$desc'" ); |
b1edf9f9 |
384 | is( $got->on_delete, $expected->{on_delete}, |
385 | "on_delete parameter correct for `$desc'" ); |
386 | is( $got->on_update, $expected->{on_update}, |
387 | "on_update parameter correct for `$desc'" ); |
13de943d |
388 | is( $got->deferrable, $expected->{deferrable}, |
389 | "is_deferrable parameter correct for `$desc'" ); |
0da8b7da |
390 | |
391 | my $index = get_index( $got->table, { fields => $expected->{selfcols} } ); |
9c1f7965 |
392 | |
393 | if ($expected->{noindex}) { |
394 | ok( !defined $index, "index doesn't for `$desc'" ); |
395 | } else { |
396 | ok( defined $index, "index exists for `$desc'" ); |
397 | is( $index->name, $expected->{index_name}, "index has correct name for `$desc'" ); |
398 | } |
0da8b7da |
399 | } |
400 | |
401 | sub test_unique { |
402 | my ($expected, $got) = @_; |
403 | my $desc = $expected->{display}; |
404 | is( $got->name, $expected->{name}, |
405 | "name parameter correct for `$desc'" ); |
637ca936 |
406 | } |