added check to skip tests that break on SQLite < 3.2.6 due to SQLite not understandin...
[dbsrgits/DBIx-Class.git] / t / run / 16joins.tl
CommitLineData
0567538f 1sub run_tests {
1edaf6fe 2my $schema = shift;
0567538f 3
4use IO::File;
485f6e10 5use version;
0567538f 6
7BEGIN {
8 eval "use DBD::SQLite";
9 plan $@
10 ? ( skip_all => 'needs DBD::SQLite for testing' )
d20bb28e 11 : ( tests => 41 );
0567538f 12}
13
485f6e10 14my $is_broken_sqlite = version->new($schema->storage->dbh->get_info(18)) lt '3.2.6' &&
15 $schema->storage->dbh->get_info(17) eq 'SQLite';
16
0567538f 17# test the abstract join => SQL generator
18my $sa = new DBIC::SQL::Abstract;
19
20my @j = (
21 { child => 'person' },
22 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
23 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
24);
25my $match = 'person child JOIN person father ON ( father.person_id = '
26 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
27 . '= child.mother_id )'
28 ;
29is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
30
31my @j2 = (
32 { mother => 'person' },
33 [ [ { child => 'person' },
34 [ { father => 'person' },
35 { 'father.person_id' => 'child.father_id' }
36 ]
37 ],
38 { 'mother.person_id' => 'child.mother_id' }
39 ],
40);
41$match = 'person mother JOIN (person child JOIN person father ON ('
42 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
43 . 'child.mother_id )'
44 ;
45is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
46
47my @j3 = (
48 { child => 'person' },
49 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
50 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
51);
52$match = 'person child INNER JOIN person father ON ( father.person_id = '
53 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
54 . '= child.mother_id )'
55 ;
56
57is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
58
f9db5527 59my $rs = $schema->resultset("CD")->search(
0567538f 60 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
61 { from => [ { 'me' => 'cd' },
62 [
63 { artist => 'artist' },
64 { 'me.artist' => 'artist.artistid' }
65 ] ] }
66 );
67
68cmp_ok( $rs->count, '==', 1, "Single record in resultset");
69
70is($rs->first->title, 'Forkful of bees', 'Correct record returned');
71
f9db5527 72$rs = $schema->resultset("CD")->search(
0567538f 73 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
74 { join => 'artist' });
75
76cmp_ok( $rs->count, '==', 1, "Single record in resultset");
77
78is($rs->first->title, 'Forkful of bees', 'Correct record returned');
79
f9db5527 80$rs = $schema->resultset("CD")->search(
0567538f 81 { 'artist.name' => 'We Are Goth',
82 'liner_notes.notes' => 'Kill Yourself!' },
83 { join => [ qw/artist liner_notes/ ] });
84
85cmp_ok( $rs->count, '==', 1, "Single record in resultset");
86
87is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
88
8fe164b9 89# when using join attribute, make sure slice()ing all objects has same count as all()
f9db5527 90$rs = $schema->resultset("CD")->search(
8fe164b9 91 { 'artist' => 1 },
92 { join => [qw/artist/], order_by => 'artist.name' }
93);
94cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
95
f9db5527 96$rs = $schema->resultset("Artist")->search(
0567538f 97 { 'liner_notes.notes' => 'Kill Yourself!' },
98 { join => { 'cds' => 'liner_notes' } });
99
100cmp_ok( $rs->count, '==', 1, "Single record in resultset");
101
102is($rs->first->name, 'We Are Goth', 'Correct record returned');
103
f9db5527 104$rs = $schema->resultset("CD")->search(
0567538f 105 { 'artist.name' => 'Caterwauler McCrae' },
106 { prefetch => [ qw/artist liner_notes/ ],
107 order_by => 'me.cdid' });
108
109cmp_ok($rs->count, '==', 3, 'Correct number of records returned');
110
111# start test for prefetch SELECT count
112unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
113DBI->trace(1, 't/var/dbic.trace');
114
115my @cd = $rs->all;
116
117is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
118
7cd300ea 119ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
0567538f 120
121is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
122
dd417d06 123is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
124
0567538f 125is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
126
127# count the SELECTs
b3e8ac9b 128DBI->trace(0, undef);
0567538f 129my $selects = 0;
130my $trace = IO::File->new('t/var/dbic.trace', '<')
131 or die "Unable to read trace file";
132while (<$trace>) {
133 $selects++ if /SELECT/;
134}
135$trace->close;
136unlink 't/var/dbic.trace';
137is($selects, 1, 'prefetch ran only 1 select statement');
138
d20bb28e 139# test for partial prefetch via cols attr
140my $cd = $schema->resultset('CD')->find(1,
141 {
142 cols => [qw/title artist.name/],
143 join => 'artist'
144 }
145);
146ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
147
b3e8ac9b 148# start test for nested prefetch SELECT count
149unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
150DBI->trace(1, 't/var/dbic.trace');
151
152$rs = $schema->resultset('Tag')->search(
153 {},
154 {
155 prefetch => { cd => 'artist' }
156 }
157);
158
159my $tag = $rs->first;
160
161is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
162
163is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
164
165# count the SELECTs
166DBI->trace(0, undef);
0009fa49 167$selects = 0;
168$trace = IO::File->new('t/var/dbic.trace', '<')
b3e8ac9b 169 or die "Unable to read trace file";
170while (<$trace>) {
171 $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
172}
173$trace->close;
174unlink 't/var/dbic.trace';
175is($selects, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
176
c5b7d799 177# start test for prefetch on find SELECT count
178unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
179DBI->trace(1, 't/var/dbic.trace');
180
d20bb28e 181$cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
c5b7d799 182
183is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
184
185# count the SELECTs
186DBI->trace(0, undef);
187$selects = 0;
188$trace = IO::File->new('t/var/dbic.trace', '<')
189 or die "Unable to read trace file";
190while (<$trace>) {
191 $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
192}
193$trace->close;
194unlink 't/var/dbic.trace';
195is($selects, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
196
b3e8ac9b 197$rs = $schema->resultset('Tag')->search(
198 {},
199 {
200 join => { cd => 'artist' },
201 prefetch => { cd => 'artist' }
202 }
203);
204
205cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
206
f9db5527 207my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
0567538f 208 { order_by => 'artistid DESC', join => 'cds' });
209
210is($artist->name, 'Random Boy Band', "Join search by object ok");
211
f9db5527 212my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
0567538f 213 { join => 'liner_notes' });
214
215cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
216
217is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
218
f9db5527 219my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
0567538f 220 { join => { 'cds' => 'tags' } });
221
222cmp_ok( @artists, '==', 2, "two-join search ok" );
223
15c382be 224$rs = $schema->resultset("CD")->search(
225 {},
226 { group_by => [qw/ title me.cdid /] }
227);
228
485f6e10 229SKIP: {
230 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
231 if $is_broken_sqlite;
232 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
233}
15c382be 234
235cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
236
237$rs = $schema->resultset("CD")->search(
238 {},
239 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
240);
241
485f6e10 242SKIP: {
243 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
244 if $is_broken_sqlite;
245 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
246}
15c382be 247
248cmp_ok( scalar $rs->all, '==', 3, "all() returns same count as count() after group_by on related column" );
489709af 249
250$rs = $schema->resultset("Artist")->search(
251 { 'cds.title' => 'Spoonful of bees',
252 'cds_2.title' => 'Forkful of bees' },
253 { join => [ 'cds', 'cds' ] });
254
485f6e10 255SKIP: {
256 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
257 if $is_broken_sqlite;
258 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
259}
260
489709af 261is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
262
887ce227 263my $queries;
264$schema->storage->debugcb(sub { $queries++ });
265$schema->storage->debug(1);
266
267my $tree_like =
268 $schema->resultset('TreeLike')->find(4,
269 { join => { parent => { parent => 'parent' } },
270 prefetch => { parent => { parent => 'parent' } } });
271
272is($tree_like->name, 'quux', 'Bottom of tree ok');
273$tree_like = $tree_like->parent;
274is($tree_like->name, 'baz', 'First level up ok');
275$tree_like = $tree_like->parent;
276is($tree_like->name, 'bar', 'Second level up ok');
277$tree_like = $tree_like->parent;
278is($tree_like->name, 'foo', 'Third level up ok');
279
280$schema->storage->debug(0);
281
282cmp_ok($queries, '==', 1, 'Only one query run');
283
489709af 284} # end run_tests
0567538f 285
2861;