Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
cb3e35d2 |
7 | use Data::Dumper; |
70350518 |
8 | |
a47e1233 |
9 | my $schema = DBICTest->init_schema(); |
0567538f |
10 | |
4d91ad3f |
11 | my $orig_debug = $schema->storage->debug; |
12 | |
0567538f |
13 | use IO::File; |
14 | |
15 | BEGIN { |
16 | eval "use DBD::SQLite"; |
17 | plan $@ |
18 | ? ( skip_all => 'needs DBD::SQLite for testing' ) |
2bd9c7c0 |
19 | : ( tests => 64 ); |
0567538f |
20 | } |
21 | |
58ff4acf |
22 | # figure out if we've got a version of sqlite that is older than 3.2.6, in |
23 | # which case COUNT(DISTINCT()) doesn't work |
24 | my $is_broken_sqlite = 0; |
25 | my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) = |
26 | split /\./, $schema->storage->dbh->get_info(18); |
27 | if( $schema->storage->dbh->get_info(17) eq 'SQLite' && |
28 | ( ($sqlite_major_ver < 3) || |
29 | ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) || |
30 | ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) { |
31 | $is_broken_sqlite = 1; |
32 | } |
485f6e10 |
33 | |
0567538f |
34 | # test the abstract join => SQL generator |
35 | my $sa = new DBIC::SQL::Abstract; |
36 | |
37 | my @j = ( |
38 | { child => 'person' }, |
39 | [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ], |
40 | [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], |
41 | ); |
42 | my $match = 'person child JOIN person father ON ( father.person_id = ' |
43 | . 'child.father_id ) JOIN person mother ON ( mother.person_id ' |
44 | . '= child.mother_id )' |
45 | ; |
46 | is( $sa->_recurse_from(@j), $match, 'join 1 ok' ); |
47 | |
48 | my @j2 = ( |
49 | { mother => 'person' }, |
50 | [ [ { child => 'person' }, |
51 | [ { father => 'person' }, |
52 | { 'father.person_id' => 'child.father_id' } |
53 | ] |
54 | ], |
55 | { 'mother.person_id' => 'child.mother_id' } |
56 | ], |
57 | ); |
58 | $match = 'person mother JOIN (person child JOIN person father ON (' |
59 | . ' father.person_id = child.father_id )) ON ( mother.person_id = ' |
60 | . 'child.mother_id )' |
61 | ; |
62 | is( $sa->_recurse_from(@j2), $match, 'join 2 ok' ); |
63 | |
64 | my @j3 = ( |
65 | { child => 'person' }, |
66 | [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ], |
67 | [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ], |
68 | ); |
69 | $match = 'person child INNER JOIN person father ON ( father.person_id = ' |
70 | . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id ' |
71 | . '= child.mother_id )' |
72 | ; |
73 | |
74 | is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok'); |
75 | |
ca7b9fdf |
76 | my @j4 = ( |
77 | { mother => 'person' }, |
78 | [ [ { child => 'person', -join_type => 'left' }, |
79 | [ { father => 'person', -join_type => 'right' }, |
80 | { 'father.person_id' => 'child.father_id' } |
81 | ] |
82 | ], |
83 | { 'mother.person_id' => 'child.mother_id' } |
84 | ], |
85 | ); |
86 | $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON (' |
87 | . ' father.person_id = child.father_id )) ON ( mother.person_id = ' |
88 | . 'child.mother_id )' |
89 | ; |
90 | is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok'); |
91 | |
635b9634 |
92 | my @j5 = ( |
93 | { child => 'person' }, |
94 | [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ], |
95 | [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], |
96 | ); |
97 | $match = 'person child JOIN person father ON ( father.person_id != ' |
98 | . 'child.father_id ) JOIN person mother ON ( mother.person_id ' |
99 | . '= child.mother_id )' |
100 | ; |
101 | is( $sa->_recurse_from(@j5), $match, 'join 5 (SCALAR reference for ON statement) ok' ); |
102 | |
103 | my @j6 = ( |
104 | { child => 'person' }, |
105 | [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ], |
106 | [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], |
107 | ); |
11aada86 |
108 | $match = qr/^HASH reference arguments are not supported in JOINS - try using "\.\.\." instead/; |
635b9634 |
109 | eval { $sa->_recurse_from(@j6) }; |
110 | like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' ); |
111 | |
f9db5527 |
112 | my $rs = $schema->resultset("CD")->search( |
0567538f |
113 | { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, |
114 | { from => [ { 'me' => 'cd' }, |
115 | [ |
116 | { artist => 'artist' }, |
117 | { 'me.artist' => 'artist.artistid' } |
118 | ] ] } |
119 | ); |
120 | |
ebaefbc2 |
121 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
122 | |
123 | is($rs->first->title, 'Forkful of bees', 'Correct record returned'); |
124 | |
f9db5527 |
125 | $rs = $schema->resultset("CD")->search( |
0567538f |
126 | { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, |
127 | { join => 'artist' }); |
128 | |
ebaefbc2 |
129 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
130 | |
131 | is($rs->first->title, 'Forkful of bees', 'Correct record returned'); |
132 | |
f9db5527 |
133 | $rs = $schema->resultset("CD")->search( |
0567538f |
134 | { 'artist.name' => 'We Are Goth', |
135 | 'liner_notes.notes' => 'Kill Yourself!' }, |
136 | { join => [ qw/artist liner_notes/ ] }); |
137 | |
ebaefbc2 |
138 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
139 | |
140 | is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned'); |
141 | |
8fe164b9 |
142 | # when using join attribute, make sure slice()ing all objects has same count as all() |
f9db5527 |
143 | $rs = $schema->resultset("CD")->search( |
8fe164b9 |
144 | { 'artist' => 1 }, |
145 | { join => [qw/artist/], order_by => 'artist.name' } |
146 | ); |
147 | cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' ); |
148 | |
2bd9c7c0 |
149 | ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count, |
150 | 'Slicing beyond end of rs returns a zero count'); |
151 | |
f9db5527 |
152 | $rs = $schema->resultset("Artist")->search( |
0567538f |
153 | { 'liner_notes.notes' => 'Kill Yourself!' }, |
154 | { join => { 'cds' => 'liner_notes' } }); |
155 | |
156 | cmp_ok( $rs->count, '==', 1, "Single record in resultset"); |
157 | |
158 | is($rs->first->name, 'We Are Goth', 'Correct record returned'); |
159 | |
cb3e35d2 |
160 | # bug in 0.07000 caused attr (join/prefetch) to be modifed by search |
161 | # so we check the search & attr arrays are not modified |
162 | my $search = { 'artist.name' => 'Caterwauler McCrae' }; |
163 | my $attr = { prefetch => [ qw/artist liner_notes/ ], |
164 | order_by => 'me.cdid' }; |
165 | my $search_str = Dumper($search); |
166 | my $attr_str = Dumper($attr); |
167 | |
168 | $rs = $schema->resultset("CD")->search($search, $attr); |
0567538f |
169 | |
cb3e35d2 |
170 | is(Dumper($search), $search_str, 'Search hash untouched after search()'); |
171 | is(Dumper($attr), $attr_str, 'Attribute hash untouched after search()'); |
ebaefbc2 |
172 | cmp_ok($rs + 0, '==', 3, 'Correct number of records returned'); |
0567538f |
173 | |
d52170d4 |
174 | my $queries = 0; |
9809a6df |
175 | $schema->storage->debugcb(sub { $queries++; }); |
d52170d4 |
176 | $schema->storage->debug(1); |
0567538f |
177 | |
178 | my @cd = $rs->all; |
179 | |
180 | is($cd[0]->title, 'Spoonful of bees', 'First record returned ok'); |
181 | |
7cd300ea |
182 | ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join'); |
0567538f |
183 | |
184 | is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN'); |
185 | |
dd417d06 |
186 | is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class'); |
187 | |
0567538f |
188 | is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok'); |
189 | |
d52170d4 |
190 | is($queries, 1, 'prefetch ran only 1 select statement'); |
191 | |
4d91ad3f |
192 | $schema->storage->debug($orig_debug); |
193 | $schema->storage->debugobj->callback(undef); |
0567538f |
194 | |
9b465d00 |
195 | # test for partial prefetch via columns attr |
d20bb28e |
196 | my $cd = $schema->resultset('CD')->find(1, |
197 | { |
9b465d00 |
198 | columns => [qw/title artist.name/], |
0e35aa05 |
199 | join => { 'artist' => {} } |
d20bb28e |
200 | } |
201 | ); |
202 | ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched'); |
203 | |
b3e8ac9b |
204 | # start test for nested prefetch SELECT count |
d52170d4 |
205 | $queries = 0; |
db0e65d2 |
206 | $schema->storage->debugcb(sub { $queries++ }); |
d52170d4 |
207 | $schema->storage->debug(1); |
b3e8ac9b |
208 | |
209 | $rs = $schema->resultset('Tag')->search( |
210 | {}, |
211 | { |
212 | prefetch => { cd => 'artist' } |
213 | } |
214 | ); |
215 | |
216 | my $tag = $rs->first; |
217 | |
218 | is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' ); |
219 | |
220 | is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch'); |
221 | |
222 | # count the SELECTs |
d52170d4 |
223 | #$selects++ if /SELECT(?!.*WHERE 1=0.*)/; |
224 | is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)'); |
b3e8ac9b |
225 | |
d52170d4 |
226 | $queries = 0; |
c5b7d799 |
227 | |
d54c409f |
228 | is($tag->search_related('cd')->search_related('artist')->first->name, |
229 | 'Caterwauler McCrae', |
230 | 'chained belongs_to->belongs_to search_related ok'); |
231 | |
9809a6df |
232 | is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries'); |
d54c409f |
233 | |
234 | $queries = 0; |
235 | |
d20bb28e |
236 | $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' }); |
c5b7d799 |
237 | |
238 | is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find'); |
239 | |
d52170d4 |
240 | is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)'); |
241 | |
d54c409f |
242 | $queries = 0; |
243 | |
95303b0a |
244 | $schema->storage->debugcb(sub { $queries++; }); |
9809a6df |
245 | |
d54c409f |
246 | $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } }); |
247 | |
248 | is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok'); |
249 | |
250 | TODO: { |
251 | local $TODO = 'use prefetched values for many_to_many accessor'; |
252 | |
253 | is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query'); |
254 | } |
255 | |
256 | $queries = 0; |
257 | |
258 | my $producers = $cd->search_related('cd_to_producer')->search_related('producer'); |
259 | |
260 | is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok'); |
261 | |
9809a6df |
262 | is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries'); |
d54c409f |
263 | |
4d91ad3f |
264 | $schema->storage->debug($orig_debug); |
265 | $schema->storage->debugobj->callback(undef); |
c5b7d799 |
266 | |
b3e8ac9b |
267 | $rs = $schema->resultset('Tag')->search( |
268 | {}, |
269 | { |
270 | join => { cd => 'artist' }, |
271 | prefetch => { cd => 'artist' } |
272 | } |
273 | ); |
274 | |
275 | cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' ); |
276 | |
f9db5527 |
277 | my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 }, |
0567538f |
278 | { order_by => 'artistid DESC', join => 'cds' }); |
279 | |
280 | is($artist->name, 'Random Boy Band', "Join search by object ok"); |
281 | |
f9db5527 |
282 | my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' }, |
0567538f |
283 | { join => 'liner_notes' }); |
284 | |
285 | cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have"); |
286 | |
287 | is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved"); |
288 | |
f9db5527 |
289 | my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' }, |
0567538f |
290 | { join => { 'cds' => 'tags' } }); |
291 | |
292 | cmp_ok( @artists, '==', 2, "two-join search ok" ); |
293 | |
15c382be |
294 | $rs = $schema->resultset("CD")->search( |
295 | {}, |
296 | { group_by => [qw/ title me.cdid /] } |
297 | ); |
298 | |
485f6e10 |
299 | SKIP: { |
300 | skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1 |
301 | if $is_broken_sqlite; |
302 | cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" ); |
303 | } |
15c382be |
304 | |
305 | cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" ); |
306 | |
307 | $rs = $schema->resultset("CD")->search( |
308 | {}, |
309 | { join => [qw/ artist /], group_by => [qw/ artist.name /] } |
310 | ); |
311 | |
485f6e10 |
312 | SKIP: { |
313 | skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1 |
314 | if $is_broken_sqlite; |
315 | cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" ); |
316 | } |
15c382be |
317 | |
8839560b |
318 | $rs = $schema->resultset("Artist")->search( |
6991aba3 |
319 | {}, |
01c73d7b |
320 | { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } } |
6991aba3 |
321 | ); |
322 | |
8839560b |
323 | cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" ); |
6991aba3 |
324 | |
01c73d7b |
325 | $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }}); |
6991aba3 |
326 | |
8839560b |
327 | cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" ); |
489709af |
328 | |
329 | $rs = $schema->resultset("Artist")->search( |
330 | { 'cds.title' => 'Spoonful of bees', |
331 | 'cds_2.title' => 'Forkful of bees' }, |
332 | { join => [ 'cds', 'cds' ] }); |
333 | |
485f6e10 |
334 | SKIP: { |
335 | skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1 |
336 | if $is_broken_sqlite; |
337 | cmp_ok($rs->count, '==', 1, "single artist returned from multi-join"); |
338 | } |
339 | |
489709af |
340 | is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned"); |
341 | |
d96034a1 |
342 | $cd = $schema->resultset('Artist')->first->create_related('cds', |
24010dd8 |
343 | { |
344 | title => 'Unproduced Single', |
345 | year => 2007 |
346 | }); |
347 | |
348 | my $left_join = $schema->resultset('CD')->search( |
349 | { 'me.cdid' => $cd->cdid }, |
350 | { prefetch => { cd_to_producer => 'producer' } } |
351 | ); |
352 | |
353 | cmp_ok($left_join, '==', 1, 'prefetch with no join record present'); |
8206c013 |
354 | |
d52170d4 |
355 | $queries = 0; |
db0e65d2 |
356 | $schema->storage->debugcb(sub { $queries++ }); |
887ce227 |
357 | $schema->storage->debug(1); |
358 | |
359 | my $tree_like = |
360 | $schema->resultset('TreeLike')->find(4, |
361 | { join => { parent => { parent => 'parent' } }, |
362 | prefetch => { parent => { parent => 'parent' } } }); |
363 | |
364 | is($tree_like->name, 'quux', 'Bottom of tree ok'); |
365 | $tree_like = $tree_like->parent; |
366 | is($tree_like->name, 'baz', 'First level up ok'); |
367 | $tree_like = $tree_like->parent; |
368 | is($tree_like->name, 'bar', 'Second level up ok'); |
369 | $tree_like = $tree_like->parent; |
370 | is($tree_like->name, 'foo', 'Third level up ok'); |
371 | |
4d91ad3f |
372 | $schema->storage->debug($orig_debug); |
373 | $schema->storage->debugobj->callback(undef); |
887ce227 |
374 | |
375 | cmp_ok($queries, '==', 1, 'Only one query run'); |
376 | |
10ed6c25 |
377 | $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 1}); |
af4cd3e5 |
378 | $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first; |
7e5b5f6e |
379 | is($tree_like->name, 'quux', 'Tree search_related ok'); |
380 | |
48d2be64 |
381 | $tree_like = $schema->resultset('TreeLike')->search_related('children', |
382 | { 'children.id' => 2, 'children_2.id' => 3 }, |
383 | { prefetch => { children => 'children' } } |
384 | )->first; |
b377c7e1 |
385 | is(eval { $tree_like->children->first->children->first->name }, 'quux', |
386 | 'Tree search_related with prefetch ok'); |
10ed6c25 |
387 | |
b377c7e1 |
388 | $tree_like = eval { $schema->resultset('TreeLike')->search( |
10ed6c25 |
389 | { 'children.id' => 2, 'children_2.id' => 5 }, |
3583d755 |
390 | { join => [qw/children children/] } |
9755b3df |
391 | )->search_related('children', { 'children_4.id' => 6 }, { prefetch => 'children' } |
b377c7e1 |
392 | )->first->children->first; }; |
393 | is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok'); |
10ed6c25 |
394 | |
7e5b5f6e |
395 | # test that collapsed joins don't get a _2 appended to the alias |
396 | |
397 | my $sql = ''; |
398 | $schema->storage->debugcb(sub { $sql = $_[1] }); |
399 | $schema->storage->debug(1); |
400 | |
401 | eval { |
402 | my $row = $schema->resultset('Artist')->search_related('cds', undef, { |
403 | join => 'tracks', |
404 | prefetch => 'tracks', |
405 | })->search_related('tracks')->first; |
406 | }; |
407 | |
9755b3df |
408 | like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" ); |
7e5b5f6e |
409 | |
4d91ad3f |
410 | $schema->storage->debug($orig_debug); |
411 | $schema->storage->debugobj->callback(undef); |
907d3b9b |
412 | |
a08c907f |
413 | $rs = $schema->resultset('Artist'); |
414 | $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' }); |
415 | $rs->create({ artistid => 5, name => 'Emo 4ever' }); |
416 | @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' }); |
417 | is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok'); |
418 | |
907d3b9b |
419 | # ------------- |
420 | # |
421 | # Tests for multilevel has_many prefetch |
422 | |
423 | # artist resultsets - with and without prefetch |
424 | my $art_rs = $schema->resultset('Artist'); |
425 | my $art_rs_pr = $art_rs->search( |
426 | {}, |
427 | { |
428 | join => [ { cds => ['tracks'] } ], |
9809a6df |
429 | prefetch => [ { cds => ['tracks'] } ], |
430 | cache => 1 # last test needs this |
907d3b9b |
431 | } |
432 | ); |
433 | |
434 | # This test does the same operation twice - once on a |
435 | # set of items fetched from the db with no prefetch of has_many rels |
436 | # The second prefetches 2 levels of has_many |
437 | # We check things are the same by comparing the name or title |
438 | # we build everything into a hash structure and compare the one |
439 | # from each rs to see what differs |
440 | |
441 | sub make_hash_struc { |
442 | my $rs = shift; |
443 | |
444 | my $struc = {}; |
445 | foreach my $art ( $rs->all ) { |
446 | foreach my $cd ( $art->cds ) { |
447 | foreach my $track ( $cd->tracks ) { |
448 | $struc->{ $art->name }{ $cd->title }{ $track->title }++; |
449 | } |
450 | } |
451 | } |
452 | return $struc; |
453 | } |
454 | |
d54c409f |
455 | $queries = 0; |
456 | $schema->storage->debugcb(sub { $queries++ }); |
457 | $schema->storage->debug(1); |
458 | |
907d3b9b |
459 | my $prefetch_result = make_hash_struc($art_rs_pr); |
d54c409f |
460 | |
461 | is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query'); |
462 | |
907d3b9b |
463 | my $nonpre_result = make_hash_struc($art_rs); |
464 | |
465 | is_deeply( $prefetch_result, $nonpre_result, |
466 | 'Compare 2 level prefetch result to non-prefetch result' ); |
d54c409f |
467 | |
468 | $queries = 0; |
469 | |
470 | is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title, |
471 | 'Fowlin', |
472 | 'chained has_many->has_many search_related ok' |
473 | ); |
474 | |
9809a6df |
475 | is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries'); |