Make sure we actually run all tests, given we're using done_testing.
[dbsrgits/DBIx-Class.git] / t / 86sqlt.t
CommitLineData
70350518 1use strict;
2use warnings;
3
637ca936 4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
637ca936 7
7f6f5b69 8BEGIN {
2527233b 9 require DBIx::Class;
7f6f5b69 10 plan skip_all =>
2527233b 11 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
7f6f5b69 13}
637ca936 14
0fd7e9a3 15my $schema = DBICTest->init_schema (no_deploy => 1);
16
30ae562b 17
18# Check deployment statements ctx sensitivity
19{
20 my $not_first_table_creation_re = qr/CREATE TABLE fourkeys_to_twokeys/;
21
22
23 my $statements = $schema->deployment_statements;
24 like (
25 $statements,
26 $not_first_table_creation_re,
27 'All create statements returned in 1 string in scalar ctx'
28 );
29
30 my @statements = $schema->deployment_statements;
31 cmp_ok (scalar @statements, '>', 1, 'Multiple statement lines in array ctx');
32
33 my $i = 0;
34 while ($i <= $#statements) {
35 last if $statements[$i] =~ $not_first_table_creation_re;
36 $i++;
37 }
38
39 ok (
40 ($i > 0) && ($i <= $#statements),
41 "Creation statement was found somewherere within array ($i)"
42 );
43}
44
45
46
427c4089 47{
48 my $deploy_hook_called = 0;
0fd7e9a3 49
427c4089 50 # replace the sqlt calback with a custom version ading an index
51 $schema->source('Track')->sqlt_deploy_callback(sub {
52 my ($self, $sqlt_table) = @_;
0fd7e9a3 53
427c4089 54 $deploy_hook_called = 1;
0fd7e9a3 55
427c4089 56 is (
57 $sqlt_table->schema->translator->producer_type,
58 join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
59 'Production type passed to translator object',
60 );
0fd7e9a3 61
427c4089 62 if ($schema->storage->sqlt_type eq 'SQLite' ) {
63 $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
64 or die $sqlt_table->error;
65 }
66
67 $self->default_sqlt_deploy_hook($sqlt_table);
68 });
69
70 $schema->deploy; # do not remove, this fires the is() test in the callback above
71 ok($deploy_hook_called, 'deploy hook got called');
72}
637ca936 73
637ca936 74
661fc8eb 75my $translator = SQL::Translator->new(
76 parser_args => {
77 'DBIx::Schema' => $schema,
78 },
79 producer_args => {},
637ca936 80);
81
e377d723 82{
83 my $warn = '';
84 local $SIG{__WARN__} = sub { $warn = shift };
637ca936 85
e377d723 86 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
87 local $relinfo->{attrs}{on_delete} = 'restrict';
637ca936 88
f89bb832 89
e377d723 90 $translator->parser('SQL::Translator::Parser::DBIx::Class');
91 $translator->producer('SQLite');
256e87b0 92
e377d723 93 my $output = $translator->translate();
94
95 ok($output, "SQLT produced someoutput")
96 or diag($translator->error);
97
0fd7e9a3 98
48850f9a 99 like (
100 $warn,
101 qr/SQLT attribute .+? was supplied for relationship .+? which does not appear to be a foreign constraint/,
102 'Warn about dubious on_delete/on_update attributes',
103 );
e377d723 104}
256e87b0 105
b1edf9f9 106# Note that the constraints listed here are the only ones that are tested -- if
107# more exist in the Schema than are listed here and all listed constraints are
c75b18e9 108# correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
109# KEY constraints to DBICTest::Schema, add tests here if you think the existing
110# test coverage is not sufficient
b1edf9f9 111
112my %fk_constraints = (
661fc8eb 113
114 # TwoKeys
b1edf9f9 115 twokeys => [
116 {
117 'display' => 'twokeys->cd',
bb0f01d0 118 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
b1edf9f9 119 'selftable' => 'twokeys', 'foreigntable' => 'cd',
120 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
9c1f7965 121 'noindex' => 1,
13de943d 122 on_delete => '', on_update => '', deferrable => 0,
b1edf9f9 123 },
124 {
125 'display' => 'twokeys->artist',
bb0f01d0 126 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
b1edf9f9 127 'selftable' => 'twokeys', 'foreigntable' => 'artist',
128 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
e394339b 129 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 130 },
131 ],
661fc8eb 132
133 # FourKeys_to_TwoKeys
b1edf9f9 134 fourkeys_to_twokeys => [
135 {
136 'display' => 'fourkeys_to_twokeys->twokeys',
f34cb1fd 137 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
b1edf9f9 138 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
139 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
e394339b 140 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 141 },
142 {
f34cb1fd 143 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
d1b264d3 144 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
b1edf9f9 145 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
146 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
147 'foreigncols' => [qw(foo bar hello goodbye)],
e394339b 148 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 149 },
150 ],
661fc8eb 151
152 # CD_to_Producer
b1edf9f9 153 cd_to_producer => [
154 {
155 'display' => 'cd_to_producer->cd',
bb0f01d0 156 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
b1edf9f9 157 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
158 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 159 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 160 },
161 {
162 'display' => 'cd_to_producer->producer',
bb0f01d0 163 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
b1edf9f9 164 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
165 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
e394339b 166 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 167 },
168 ],
661fc8eb 169
170 # Self_ref_alias
b1edf9f9 171 self_ref_alias => [
172 {
173 'display' => 'self_ref_alias->self_ref for self_ref',
bb0f01d0 174 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
b1edf9f9 175 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
176 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
e394339b 177 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 178 },
179 {
180 'display' => 'self_ref_alias->self_ref for alias',
bb0f01d0 181 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
b1edf9f9 182 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
183 'selfcols' => ['alias'], 'foreigncols' => ['id'],
e394339b 184 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 185 },
186 ],
661fc8eb 187
188 # CD
b1edf9f9 189 cd => [
190 {
191 'display' => 'cd->artist',
bb0f01d0 192 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
b1edf9f9 193 'selftable' => 'cd', 'foreigntable' => 'artist',
194 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
a0dd8679 195 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 196 },
197 ],
661fc8eb 198
199 # Artist_undirected_map
b1edf9f9 200 artist_undirected_map => [
201 {
202 'display' => 'artist_undirected_map->artist for id1',
bb0f01d0 203 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
b1edf9f9 204 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
205 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
e377d723 206 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 207 },
208 {
209 'display' => 'artist_undirected_map->artist for id2',
bb0f01d0 210 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
b1edf9f9 211 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
212 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
b230b4be 213 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 214 },
215 ],
661fc8eb 216
217 # Track
b1edf9f9 218 track => [
219 {
220 'display' => 'track->cd',
bb0f01d0 221 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
b1edf9f9 222 'selftable' => 'track', 'foreigntable' => 'cd',
223 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 224 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 225 },
226 ],
661fc8eb 227
228 # TreeLike
b1edf9f9 229 treelike => [
230 {
231 'display' => 'treelike->treelike for parent',
61177e44 232 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
b1edf9f9 233 'selftable' => 'treelike', 'foreigntable' => 'treelike',
61177e44 234 'selfcols' => ['parent'], 'foreigncols' => ['id'],
e394339b 235 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 236 },
237 ],
238
239 # TwoKeyTreeLike
240 twokeytreelike => [
241 {
242 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
bb0f01d0 243 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
b1edf9f9 244 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
245 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
e394339b 246 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 247 },
248 ],
ae515736 249
661fc8eb 250 # Tags
b1edf9f9 251 tags => [
252 {
253 'display' => 'tags->cd',
bb0f01d0 254 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
b1edf9f9 255 'selftable' => 'tags', 'foreigntable' => 'cd',
256 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 257 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 258 },
259 ],
661fc8eb 260
261 # Bookmark
b1edf9f9 262 bookmark => [
263 {
264 'display' => 'bookmark->link',
bb0f01d0 265 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
b1edf9f9 266 'selftable' => 'bookmark', 'foreigntable' => 'link',
267 'selfcols' => ['link'], 'foreigncols' => ['id'],
def17c59 268 on_delete => 'SET NULL', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 269 },
270 ],
a0024650 271 # ForceForeign
272 forceforeign => [
273 {
274 'display' => 'forceforeign->artist',
bb0f01d0 275 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
a0024650 276 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
8871d4ad 277 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
827a808f 278 'noindex' => 1,
e394339b 279 on_delete => '', on_update => '', deferrable => 1,
a0024650 280 },
281 ],
b1edf9f9 282);
283
284my %unique_constraints = (
285 # CD
286 cd => [
287 {
288 'display' => 'cd artist and title unique',
0da8b7da 289 'name' => 'cd_artist_title',
b1edf9f9 290 'table' => 'cd', 'cols' => ['artist', 'title'],
291 },
292 ],
293
294 # Producer
295 producer => [
296 {
297 'display' => 'producer name unique',
0da8b7da 298 'name' => 'prod_name', # explicit name
b1edf9f9 299 'table' => 'producer', 'cols' => ['name'],
300 },
301 ],
302
303 # TwoKeyTreeLike
304 twokeytreelike => [
305 {
306 'display' => 'twokeytreelike name unique',
0da8b7da 307 'name' => 'tktlnameunique', # explicit name
b1edf9f9 308 'table' => 'twokeytreelike', 'cols' => ['name'],
309 },
310 ],
311
312 # Employee
313# Constraint is commented out in DBICTest/Schema/Employee.pm
314# employee => [
315# {
316# 'display' => 'employee position and group_id unique',
0da8b7da 317# 'name' => 'position_group',
b1edf9f9 318# 'table' => 'employee', cols => ['position', 'group_id'],
319# },
320# ],
7b90bb13 321);
322
17cab2f0 323my %indexes = (
c385ecea 324 artist => [
325 {
326 'fields' => ['name']
327 },
f89bb832 328 ],
329 track => [
330 {
331 'fields' => ['title']
332 }
333 ],
c385ecea 334);
335
637ca936 336my $tschema = $translator->schema();
d6c79cb3 337# Test that the $schema->sqlt_deploy_hook was called okay and that it removed
458e0292 338# the 'dummy' table
339ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
d6c79cb3 340
1f5bf324 341# Test that the Artist resultsource sqlt_deploy_hook was called okay and added
342# an index
343SKIP: {
344 skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1)
345 if $schema->storage->sqlt_type ne 'SQLite';
346
347 ok( ( grep
348 { $_->name eq 'artist_name_hookidx' }
349 $tschema->get_table('artist')->get_indices
350 ), 'sqlt_deploy_hook fired within a resultsource');
351}
352
b1edf9f9 353# Test that nonexistent constraints are not found
354my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
355ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
356$constraint = get_constraint('UNIQUE', 'cd', ['artist']);
357ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
a0024650 358$constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
359ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
b1edf9f9 360
361for my $expected_constraints (keys %fk_constraints) {
362 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
363 my $desc = $expected_constraint->{display};
364 my $constraint = get_constraint(
365 'FOREIGN KEY',
366 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
367 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
368 );
369 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
370 test_fk($expected_constraint, $constraint);
661fc8eb 371 }
637ca936 372}
373
b1edf9f9 374for my $expected_constraints (keys %unique_constraints) {
375 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
376 my $desc = $expected_constraint->{display};
377 my $constraint = get_constraint(
378 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
379 );
380 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
0da8b7da 381 test_unique($expected_constraint, $constraint);
b1edf9f9 382 }
637ca936 383}
384
17cab2f0 385for my $table_index (keys %indexes) {
386 for my $expected_index ( @{ $indexes{$table_index} } ) {
c385ecea 387 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
388 }
389}
390
b1edf9f9 391# Returns the Constraint object for the specified constraint type, table and
392# columns from the SQL::Translator schema, or undef if no matching constraint
393# is found.
394#
395# NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
396# two parameters are not used.
397sub get_constraint {
398 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
399 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
400 $f_cols ||= [];
401
402 my $table = $tschema->get_table($table_name);
403
404 my %fields = map { $_ => 1 } @$cols;
405 my %f_fields = map { $_ => 1 } @$f_cols;
406
a7e65bb5 407 die "No $table_name" unless $table;
b1edf9f9 408 CONSTRAINT:
409 for my $constraint ( $table->get_constraints ) {
410 next unless $constraint->type eq $type;
411 next unless $constraint->reference_table eq $f_table;
412
413 my %rev_fields = map { $_ => 1 } $constraint->fields;
414 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
415
416 # Check that the given fields are a subset of the constraint's fields
417 for my $field ($constraint->fields) {
418 next CONSTRAINT unless $fields{$field};
419 }
420 if ($type eq 'FOREIGN KEY') {
421 for my $f_field ($constraint->reference_fields) {
422 next CONSTRAINT unless $f_fields{$f_field};
661fc8eb 423 }
b1edf9f9 424 }
661fc8eb 425
b1edf9f9 426 # Check that the constraint's fields are a subset of the given fields
427 for my $field (@$cols) {
428 next CONSTRAINT unless $rev_fields{$field};
429 }
430 if ($type eq 'FOREIGN KEY') {
431 for my $f_field (@$f_cols) {
432 next CONSTRAINT unless $rev_f_fields{$f_field};
661fc8eb 433 }
434 }
b1edf9f9 435
436 return $constraint; # everything passes, found the constraint
661fc8eb 437 }
b1edf9f9 438 return undef; # didn't find a matching constraint
7b90bb13 439}
440
c385ecea 441sub get_index {
442 my ($table_name, $index) = @_;
443
444 my $table = $tschema->get_table($table_name);
445
446 CAND_INDEX:
447 for my $cand_index ( $table->get_indices ) {
448
449 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
450 || $index->{type} && $cand_index->type ne $index->{type};
451
452 my %idx_fields = map { $_ => 1 } $cand_index->fields;
453
454 for my $field ( @{ $index->{fields} } ) {
455 next CAND_INDEX unless $idx_fields{$field};
456 }
457
458 %idx_fields = map { $_ => 1 } @{$index->{fields}};
459 for my $field ( $cand_index->fields) {
460 next CAND_INDEX unless $idx_fields{$field};
461 }
462
463 return $cand_index;
464 }
465
466 return undef; # No matching idx
467}
468
b1edf9f9 469# Test parameters in a FOREIGN KEY constraint other than columns
470sub test_fk {
471 my ($expected, $got) = @_;
472 my $desc = $expected->{display};
0da8b7da 473 is( $got->name, $expected->{name},
827a808f 474 "name parameter correct for '$desc'" );
b1edf9f9 475 is( $got->on_delete, $expected->{on_delete},
827a808f 476 "on_delete parameter correct for '$desc'" );
b1edf9f9 477 is( $got->on_update, $expected->{on_update},
827a808f 478 "on_update parameter correct for '$desc'" );
13de943d 479 is( $got->deferrable, $expected->{deferrable},
827a808f 480 "is_deferrable parameter correct for '$desc'" );
0da8b7da 481
482 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
9c1f7965 483
484 if ($expected->{noindex}) {
827a808f 485 ok( !defined $index, "index doesn't for '$desc'" );
9c1f7965 486 } else {
827a808f 487 ok( defined $index, "index exists for '$desc'" );
488 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
9c1f7965 489 }
0da8b7da 490}
491
492sub test_unique {
493 my ($expected, $got) = @_;
494 my $desc = $expected->{display};
495 is( $got->name, $expected->{name},
827a808f 496 "name parameter correct for '$desc'" );
637ca936 497}
0fd7e9a3 498
499done_testing;