tweaked search_rs to not be destructive to passed
[dbsrgits/DBIx-Class.git] / t / 76joins.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
a47e1233 8my $schema = DBICTest->init_schema();
0567538f 9
4d91ad3f 10my $orig_debug = $schema->storage->debug;
11
0567538f 12use IO::File;
13
14BEGIN {
15 eval "use DBD::SQLite";
16 plan $@
17 ? ( skip_all => 'needs DBD::SQLite for testing' )
10ed6c25 18 : ( tests => 47 );
0567538f 19}
20
58ff4acf 21# figure out if we've got a version of sqlite that is older than 3.2.6, in
22# which case COUNT(DISTINCT()) doesn't work
23my $is_broken_sqlite = 0;
24my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
25 split /\./, $schema->storage->dbh->get_info(18);
26if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
27 ( ($sqlite_major_ver < 3) ||
28 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
29 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
30 $is_broken_sqlite = 1;
31}
485f6e10 32
0567538f 33# test the abstract join => SQL generator
34my $sa = new DBIC::SQL::Abstract;
35
36my @j = (
37 { child => 'person' },
38 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
39 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
40);
41my $match = 'person child JOIN person father ON ( father.person_id = '
42 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
43 . '= child.mother_id )'
44 ;
45is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
46
47my @j2 = (
48 { mother => 'person' },
49 [ [ { child => 'person' },
50 [ { father => 'person' },
51 { 'father.person_id' => 'child.father_id' }
52 ]
53 ],
54 { 'mother.person_id' => 'child.mother_id' }
55 ],
56);
57$match = 'person mother JOIN (person child JOIN person father ON ('
58 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
59 . 'child.mother_id )'
60 ;
61is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
62
63my @j3 = (
64 { child => 'person' },
65 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
66 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
67);
68$match = 'person child INNER JOIN person father ON ( father.person_id = '
69 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
70 . '= child.mother_id )'
71 ;
72
73is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
74
ca7b9fdf 75my @j4 = (
76 { mother => 'person' },
77 [ [ { child => 'person', -join_type => 'left' },
78 [ { father => 'person', -join_type => 'right' },
79 { 'father.person_id' => 'child.father_id' }
80 ]
81 ],
82 { 'mother.person_id' => 'child.mother_id' }
83 ],
84);
85$match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
86 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
87 . 'child.mother_id )'
88 ;
89is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');
90
f9db5527 91my $rs = $schema->resultset("CD")->search(
0567538f 92 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
93 { from => [ { 'me' => 'cd' },
94 [
95 { artist => 'artist' },
96 { 'me.artist' => 'artist.artistid' }
97 ] ] }
98 );
99
ebaefbc2 100cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 101
102is($rs->first->title, 'Forkful of bees', 'Correct record returned');
103
f9db5527 104$rs = $schema->resultset("CD")->search(
0567538f 105 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
106 { join => 'artist' });
107
ebaefbc2 108cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 109
110is($rs->first->title, 'Forkful of bees', 'Correct record returned');
111
f9db5527 112$rs = $schema->resultset("CD")->search(
0567538f 113 { 'artist.name' => 'We Are Goth',
114 'liner_notes.notes' => 'Kill Yourself!' },
115 { join => [ qw/artist liner_notes/ ] });
116
ebaefbc2 117cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 118
119is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
120
8fe164b9 121# when using join attribute, make sure slice()ing all objects has same count as all()
f9db5527 122$rs = $schema->resultset("CD")->search(
8fe164b9 123 { 'artist' => 1 },
124 { join => [qw/artist/], order_by => 'artist.name' }
125);
126cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
127
f9db5527 128$rs = $schema->resultset("Artist")->search(
0567538f 129 { 'liner_notes.notes' => 'Kill Yourself!' },
130 { join => { 'cds' => 'liner_notes' } });
131
132cmp_ok( $rs->count, '==', 1, "Single record in resultset");
133
134is($rs->first->name, 'We Are Goth', 'Correct record returned');
135
f9db5527 136$rs = $schema->resultset("CD")->search(
0567538f 137 { 'artist.name' => 'Caterwauler McCrae' },
138 { prefetch => [ qw/artist liner_notes/ ],
139 order_by => 'me.cdid' });
140
ebaefbc2 141cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
0567538f 142
d52170d4 143my $queries = 0;
144$schema->storage->debugcb(sub { $queries++ });
d52170d4 145$schema->storage->debug(1);
0567538f 146
147my @cd = $rs->all;
148
149is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
150
7cd300ea 151ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
0567538f 152
153is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
154
dd417d06 155is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
156
0567538f 157is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
158
d52170d4 159is($queries, 1, 'prefetch ran only 1 select statement');
160
4d91ad3f 161$schema->storage->debug($orig_debug);
162$schema->storage->debugobj->callback(undef);
0567538f 163
9b465d00 164# test for partial prefetch via columns attr
d20bb28e 165my $cd = $schema->resultset('CD')->find(1,
166 {
9b465d00 167 columns => [qw/title artist.name/],
0e35aa05 168 join => { 'artist' => {} }
d20bb28e 169 }
170);
171ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
172
b3e8ac9b 173# start test for nested prefetch SELECT count
d52170d4 174$queries = 0;
db0e65d2 175$schema->storage->debugcb(sub { $queries++ });
d52170d4 176$schema->storage->debug(1);
b3e8ac9b 177
178$rs = $schema->resultset('Tag')->search(
179 {},
180 {
181 prefetch => { cd => 'artist' }
182 }
183);
184
185my $tag = $rs->first;
186
187is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
188
189is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
190
191# count the SELECTs
d52170d4 192#$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
193is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
b3e8ac9b 194
d52170d4 195$queries = 0;
c5b7d799 196
d20bb28e 197$cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
c5b7d799 198
199is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
200
d52170d4 201is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
202
4d91ad3f 203$schema->storage->debug($orig_debug);
204$schema->storage->debugobj->callback(undef);
c5b7d799 205
b3e8ac9b 206$rs = $schema->resultset('Tag')->search(
207 {},
208 {
209 join => { cd => 'artist' },
210 prefetch => { cd => 'artist' }
211 }
212);
213
214cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
215
f9db5527 216my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
0567538f 217 { order_by => 'artistid DESC', join => 'cds' });
218
219is($artist->name, 'Random Boy Band', "Join search by object ok");
220
f9db5527 221my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
0567538f 222 { join => 'liner_notes' });
223
224cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
225
226is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
227
f9db5527 228my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
0567538f 229 { join => { 'cds' => 'tags' } });
230
231cmp_ok( @artists, '==', 2, "two-join search ok" );
232
15c382be 233$rs = $schema->resultset("CD")->search(
234 {},
235 { group_by => [qw/ title me.cdid /] }
236);
237
485f6e10 238SKIP: {
239 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
240 if $is_broken_sqlite;
241 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
242}
15c382be 243
244cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
245
246$rs = $schema->resultset("CD")->search(
247 {},
248 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
249);
250
485f6e10 251SKIP: {
252 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
253 if $is_broken_sqlite;
254 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
255}
15c382be 256
8839560b 257$rs = $schema->resultset("Artist")->search(
6991aba3 258 {},
01c73d7b 259 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
6991aba3 260);
261
8839560b 262cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
6991aba3 263
01c73d7b 264$rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
6991aba3 265
8839560b 266cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
489709af 267
268$rs = $schema->resultset("Artist")->search(
269 { 'cds.title' => 'Spoonful of bees',
270 'cds_2.title' => 'Forkful of bees' },
271 { join => [ 'cds', 'cds' ] });
272
485f6e10 273SKIP: {
274 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
275 if $is_broken_sqlite;
276 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
277}
278
489709af 279is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
280
d52170d4 281$queries = 0;
db0e65d2 282$schema->storage->debugcb(sub { $queries++ });
887ce227 283$schema->storage->debug(1);
284
285my $tree_like =
286 $schema->resultset('TreeLike')->find(4,
287 { join => { parent => { parent => 'parent' } },
288 prefetch => { parent => { parent => 'parent' } } });
289
290is($tree_like->name, 'quux', 'Bottom of tree ok');
291$tree_like = $tree_like->parent;
292is($tree_like->name, 'baz', 'First level up ok');
293$tree_like = $tree_like->parent;
294is($tree_like->name, 'bar', 'Second level up ok');
295$tree_like = $tree_like->parent;
296is($tree_like->name, 'foo', 'Third level up ok');
297
4d91ad3f 298$schema->storage->debug($orig_debug);
299$schema->storage->debugobj->callback(undef);
887ce227 300
301cmp_ok($queries, '==', 1, 'Only one query run');
302
10ed6c25 303$tree_like = $schema->resultset('TreeLike')->search({'me.id' => 1});
af4cd3e5 304$tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
7e5b5f6e 305is($tree_like->name, 'quux', 'Tree search_related ok');
306
48d2be64 307$tree_like = $schema->resultset('TreeLike')->search_related('children',
308 { 'children.id' => 2, 'children_2.id' => 3 },
309 { prefetch => { children => 'children' } }
310 )->first;
b377c7e1 311is(eval { $tree_like->children->first->children->first->name }, 'quux',
312 'Tree search_related with prefetch ok');
10ed6c25 313
b377c7e1 314$tree_like = eval { $schema->resultset('TreeLike')->search(
10ed6c25 315 { 'children.id' => 2, 'children_2.id' => 5 },
3583d755 316 { join => [qw/children children/] }
9755b3df 317 )->search_related('children', { 'children_4.id' => 6 }, { prefetch => 'children' }
b377c7e1 318 )->first->children->first; };
319is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
10ed6c25 320
7e5b5f6e 321# test that collapsed joins don't get a _2 appended to the alias
322
323my $sql = '';
324$schema->storage->debugcb(sub { $sql = $_[1] });
325$schema->storage->debug(1);
326
327eval {
328 my $row = $schema->resultset('Artist')->search_related('cds', undef, {
329 join => 'tracks',
330 prefetch => 'tracks',
331 })->search_related('tracks')->first;
332};
333
9755b3df 334like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
7e5b5f6e 335
4d91ad3f 336$schema->storage->debug($orig_debug);
337$schema->storage->debugobj->callback(undef);