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