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