fix SQLite pragma in test suite
[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
10use IO::File;
11
12BEGIN {
13 eval "use DBD::SQLite";
14 plan $@
15 ? ( skip_all => 'needs DBD::SQLite for testing' )
ca7b9fdf 16 : ( tests => 43 );
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
ca7b9fdf 73my @j4 = (
74 { mother => 'person' },
75 [ [ { child => 'person', -join_type => 'left' },
76 [ { father => 'person', -join_type => 'right' },
77 { 'father.person_id' => 'child.father_id' }
78 ]
79 ],
80 { 'mother.person_id' => 'child.mother_id' }
81 ],
82);
83$match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
84 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
85 . 'child.mother_id )'
86 ;
87is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');
88
f9db5527 89my $rs = $schema->resultset("CD")->search(
0567538f 90 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
91 { from => [ { 'me' => 'cd' },
92 [
93 { artist => 'artist' },
94 { 'me.artist' => 'artist.artistid' }
95 ] ] }
96 );
97
ebaefbc2 98cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 99
100is($rs->first->title, 'Forkful of bees', 'Correct record returned');
101
f9db5527 102$rs = $schema->resultset("CD")->search(
0567538f 103 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
104 { join => 'artist' });
105
ebaefbc2 106cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 107
108is($rs->first->title, 'Forkful of bees', 'Correct record returned');
109
f9db5527 110$rs = $schema->resultset("CD")->search(
0567538f 111 { 'artist.name' => 'We Are Goth',
112 'liner_notes.notes' => 'Kill Yourself!' },
113 { join => [ qw/artist liner_notes/ ] });
114
ebaefbc2 115cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 116
117is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
118
8fe164b9 119# when using join attribute, make sure slice()ing all objects has same count as all()
f9db5527 120$rs = $schema->resultset("CD")->search(
8fe164b9 121 { 'artist' => 1 },
122 { join => [qw/artist/], order_by => 'artist.name' }
123);
124cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
125
f9db5527 126$rs = $schema->resultset("Artist")->search(
0567538f 127 { 'liner_notes.notes' => 'Kill Yourself!' },
128 { join => { 'cds' => 'liner_notes' } });
129
130cmp_ok( $rs->count, '==', 1, "Single record in resultset");
131
132is($rs->first->name, 'We Are Goth', 'Correct record returned');
133
f9db5527 134$rs = $schema->resultset("CD")->search(
0567538f 135 { 'artist.name' => 'Caterwauler McCrae' },
136 { prefetch => [ qw/artist liner_notes/ ],
137 order_by => 'me.cdid' });
138
ebaefbc2 139cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
0567538f 140
d52170d4 141my $queries = 0;
142$schema->storage->debugcb(sub { $queries++ });
143
144$queries = 0;
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
161$schema->storage->debug(0);
0567538f 162
9b465d00 163# test for partial prefetch via columns attr
d20bb28e 164my $cd = $schema->resultset('CD')->find(1,
165 {
9b465d00 166 columns => [qw/title artist.name/],
0e35aa05 167 join => { 'artist' => {} }
d20bb28e 168 }
169);
170ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
171
b3e8ac9b 172# start test for nested prefetch SELECT count
d52170d4 173$queries = 0;
174$schema->storage->debug(1);
b3e8ac9b 175
176$rs = $schema->resultset('Tag')->search(
177 {},
178 {
179 prefetch => { cd => 'artist' }
180 }
181);
182
183my $tag = $rs->first;
184
185is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
186
187is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
188
189# count the SELECTs
d52170d4 190#$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
191is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
b3e8ac9b 192
d52170d4 193$queries = 0;
c5b7d799 194
d20bb28e 195$cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
c5b7d799 196
197is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
198
d52170d4 199is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
200
201$schema->storage->debug(0);
c5b7d799 202
b3e8ac9b 203$rs = $schema->resultset('Tag')->search(
204 {},
205 {
206 join => { cd => 'artist' },
207 prefetch => { cd => 'artist' }
208 }
209);
210
211cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
212
f9db5527 213my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
0567538f 214 { order_by => 'artistid DESC', join => 'cds' });
215
216is($artist->name, 'Random Boy Band', "Join search by object ok");
217
f9db5527 218my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
0567538f 219 { join => 'liner_notes' });
220
221cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
222
223is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
224
f9db5527 225my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
0567538f 226 { join => { 'cds' => 'tags' } });
227
228cmp_ok( @artists, '==', 2, "two-join search ok" );
229
15c382be 230$rs = $schema->resultset("CD")->search(
231 {},
232 { group_by => [qw/ title me.cdid /] }
233);
234
485f6e10 235SKIP: {
236 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
237 if $is_broken_sqlite;
238 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
239}
15c382be 240
241cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
242
243$rs = $schema->resultset("CD")->search(
244 {},
245 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
246);
247
485f6e10 248SKIP: {
249 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
250 if $is_broken_sqlite;
251 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
252}
15c382be 253
8839560b 254$rs = $schema->resultset("Artist")->search(
6991aba3 255 {},
01c73d7b 256 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
6991aba3 257);
258
8839560b 259cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
6991aba3 260
01c73d7b 261$rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
6991aba3 262
8839560b 263cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
489709af 264
265$rs = $schema->resultset("Artist")->search(
266 { 'cds.title' => 'Spoonful of bees',
267 'cds_2.title' => 'Forkful of bees' },
268 { join => [ 'cds', 'cds' ] });
269
485f6e10 270SKIP: {
271 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
272 if $is_broken_sqlite;
273 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
274}
275
489709af 276is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
277
d52170d4 278$queries = 0;
887ce227 279$schema->storage->debug(1);
280
281my $tree_like =
282 $schema->resultset('TreeLike')->find(4,
283 { join => { parent => { parent => 'parent' } },
284 prefetch => { parent => { parent => 'parent' } } });
285
286is($tree_like->name, 'quux', 'Bottom of tree ok');
287$tree_like = $tree_like->parent;
288is($tree_like->name, 'baz', 'First level up ok');
289$tree_like = $tree_like->parent;
290is($tree_like->name, 'bar', 'Second level up ok');
291$tree_like = $tree_like->parent;
292is($tree_like->name, 'foo', 'Third level up ok');
293
294$schema->storage->debug(0);
295
296cmp_ok($queries, '==', 1, 'Only one query run');
297