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