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