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