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