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