PATH may be unset entirely
[dbsrgits/DBIx-Class.git] / t / prefetch / manual.t
CommitLineData
69ab63d4 1use strict;
2use warnings;
3
4use Test::More;
52864fbd 5use Test::Deep;
69e99ee6 6use Test::Warn;
69ab63d4 7use Test::Exception;
8use lib qw(t/lib);
9use DBICTest;
10
6dd43920 11delete $ENV{DBIC_COLUMNS_INCLUDE_FILTER_RELS};
12
908aa1bb 13my $schema = DBICTest->init_schema(no_populate => 1);
14
742280c7 15$schema->resultset('Artist')->create({ name => 'JMJ', cds => [{
16 title => 'Magnetic Fields',
17 year => 1981,
18 genre => { name => 'electro' },
19 tracks => [
20 { title => 'm1' },
21 { title => 'm2' },
22 { title => 'm3' },
23 { title => 'm4' },
24 ],
25} ] });
26
908aa1bb 27$schema->resultset('CD')->create({
28 title => 'Equinoxe',
29 year => 1978,
30 artist => { name => 'JMJ' },
31 genre => { name => 'electro' },
32 tracks => [
33 { title => 'e1' },
34 { title => 'e2' },
35 { title => 'e3' },
36 ],
37 single_track => {
38 title => 'o1',
39 cd => {
40 title => 'Oxygene',
41 year => 1976,
742280c7 42 artist => { name => 'JMJ' },
908aa1bb 43 tracks => [
44 { title => 'o2', position => 2}, # the position should not be here, bug in MC
45 ],
46 },
47 },
48});
69ab63d4 49
50my $rs = $schema->resultset ('CD')->search ({}, {
51 join => [ 'tracks', { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
52 collapse => 1,
53 columns => [
54 { 'year' => 'me.year' }, # non-unique
55 { 'genreid' => 'me.genreid' }, # nullable
56 { 'tracks.title' => 'tracks.title' }, # non-unique (no me.id)
57 { 'single_track.cd.artist.cds.cdid' => 'cds.cdid' }, # to give uniquiness to ...tracks.title below
3904d3c3 58 { 'single_track.cd.artist.artistid' => 'artist.artistid' }, # uniqufies entire parental chain
69ab63d4 59 { 'single_track.cd.artist.cds.year' => 'cds.year' }, # non-unique
60 { 'single_track.cd.artist.cds.genreid' => 'cds.genreid' }, # nullable
61 { 'single_track.cd.artist.cds.tracks.title' => 'tracks_2.title' }, # unique when combined with ...cds.cdid above
908aa1bb 62 { 'latest_cd' => \ "(SELECT MAX(year) FROM cd)" }, # random function
3904d3c3 63 { 'title' => 'me.title' }, # uniquiness for me
64 { 'artist' => 'me.artist' }, # uniquiness for me
69ab63d4 65 ],
fb88ca2c 66 order_by => [{ -desc => 'cds.year' }, { -desc => 'me.title'}, 'tracks.title', 'tracks_2.title' ],
69ab63d4 67});
68
908aa1bb 69my $hri_rs = $rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
70
52864fbd 71cmp_deeply (
908aa1bb 72 [$hri_rs->all],
73 [
1e4f9fb3 74 { artist => 1, genreid => 1, latest_cd => 1981, title => "Equinoxe", year => 1978,
908aa1bb 75 single_track => {
76 cd => {
1e4f9fb3 77 artist => { artistid => 1, cds => [
78 { cdid => 1, genreid => 1, year => 1981, tracks => [
79 { title => "m1" },
80 { title => "m2" },
81 { title => "m3" },
82 { title => "m4" },
83 ]},
84 { cdid => 3, genreid => 1, year => 1978, tracks => [
85 { title => "e1" },
86 { title => "e2" },
87 { title => "e3" },
88 ]},
89 { cdid => 2, genreid => undef, year => 1976, tracks => [
90 { title => "o1" },
91 { title => "o2" },
92 ]},
93 ]},
94 },
908aa1bb 95 },
908aa1bb 96 tracks => [
1e4f9fb3 97 { title => "e1" },
98 { title => "e2" },
99 { title => "e3" },
908aa1bb 100 ],
908aa1bb 101 },
102 {
1e4f9fb3 103 artist => 1, genreid => undef, latest_cd => 1981, title => "Oxygene", year => 1976, single_track => undef,
908aa1bb 104 tracks => [
1e4f9fb3 105 { title => "o1" },
106 { title => "o2" },
908aa1bb 107 ],
908aa1bb 108 },
109 {
1e4f9fb3 110 artist => 1, genreid => 1, latest_cd => 1981, title => "Magnetic Fields", year => 1981, single_track => undef,
908aa1bb 111 tracks => [
1e4f9fb3 112 { title => "m1" },
113 { title => "m2" },
114 { title => "m3" },
115 { title => "m4" },
908aa1bb 116 ],
908aa1bb 117 },
118 ],
119 'W00T, manual prefetch with collapse works'
120);
121
6dd43920 122lives_ok { my $dummy = $rs; warnings_exist {
123
124##############
125### This is a bunch of workarounds for deprecated behavior - delete entire block when fixed
126 my $cd_obj = ($rs->all)[0]->single_track->cd;
127 my $art_obj = $cd_obj->artist;
128
129 my $empty_single_columns = {
130 cd => undef
131 };
132 my $empty_single_inflated_columns = {
133 cd => $cd_obj
134 };
135 my $empty_cd_columns = {
136 artist => $art_obj->artistid
137 };
138 my $empty_cd_inflated_columns = {
139 artist => $art_obj
140 };
141
142 {
143 local $TODO = "Returning prefetched 'filter' rels as part of get_columns/get_inflated_columns is deprecated";
144 is_deeply($_, {}) for (
145 $empty_single_columns, $empty_single_inflated_columns, $empty_cd_columns, $empty_cd_inflated_columns
146 );
147 }
148##############
149
150
151### this tests the standard root -> single -> filter ->filter
152 my ($row) = $rs->all; # don't trigger order warnings
153
154 is_deeply(
155 { $row->single_track->get_columns },
156 $empty_single_columns,
157 "No unexpected columns available on intermediate 'single' rel with a chained 'filter' prefetch",
158 );
159
160 is_deeply(
161 { $row->single_track->get_inflated_columns },
162 $empty_single_inflated_columns,
163 "No unexpected inflated columns available on intermediate 'single' rel with a chained 'filter' prefetch",
164 );
165
166 is_deeply(
167 { $row->single_track->cd->get_columns },
168 $empty_cd_columns,
169 "No unexpected columns available on intermediate 'single' rel with 2x chained 'filter' prefetch",
170 );
171
172 is_deeply(
173 { $row->single_track->cd->get_inflated_columns },
174 $empty_cd_inflated_columns,
175 "No unexpected inflated columns available on intermediate 'single' rel with 2x chained 'filter' prefetch",
176 );
177
178### also try a different arangement root -> single -> single ->filter
179 ($row) = $rs->result_source->resultset->search({ 'artist.artistid' => 1 }, {
180 join => { single_track => { disc => { artist => 'cds' } } },
181 '+columns' => {
182 'single_track.disc.artist.artistid' => 'artist.artistid',
183 'single_track.disc.artist.cds.cdid' => 'cds.cdid',
184 },
185 collapse => 1,
186 })->all;
187
188 is_deeply(
189 { $row->single_track->get_columns },
190 {},
191 "No unexpected columns available on intermediate 'single' rel with a chained 'single' prefetch",
192 );
193
194 is_deeply(
195 { $row->single_track->get_inflated_columns },
196 {},
197 "No unexpected inflated columns available on intermediate 'single' rel with a chained 'single' prefetch",
198 );
199
200 is_deeply(
201 { $row->single_track->disc->get_columns },
202 $empty_cd_columns,
203 "No unexpected columns available on intermediate 'single' rel with chained 'single' and chained 'filter' prefetch",
204 );
205
206 is_deeply(
207 { $row->single_track->disc->get_inflated_columns },
208 $empty_cd_inflated_columns,
209 "No unexpected inflated columns available on intermediate 'single' rel with chained 'single' and chained 'filter' prefetch",
210 );
211
212} [
213 qr/\QReturning primary keys of prefetched 'filter' rels as part of get_columns()/,
214 qr/\QUnable to deflate 'filter'-type relationship 'cd' (related object primary key not retrieved)/,
215 qr/\QReturning prefetched 'filter' rels as part of get_inflated_columns()/,
216 qr/\QReturning primary keys of prefetched 'filter' rels as part of get_columns()/,
217 qr/\QReturning prefetched 'filter' rels as part of get_inflated_columns()/,
218 qr/\QReturning primary keys of prefetched 'filter' rels as part of get_columns()/,
219 qr/\QReturning prefetched 'filter' rels as part of get_inflated_columns()/,
220], 'expected_warnings'
221} 'traversing prefetch chain with empty intermediates works';
69ab63d4 222
fcf32d04 223# multi-has_many with underdefined root, with rather random order
224$rs = $schema->resultset ('CD')->search ({}, {
225 join => [ 'tracks', { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
226 collapse => 1,
227 columns => [
228 { 'single_track.trackid' => 'single_track.trackid' }, # definitive link to root from 1:1:1:1:M:M chain
229 { 'year' => 'me.year' }, # non-unique
230 { 'tracks.cd' => 'tracks.cd' }, # \ together both uniqueness for second multirel
231 { 'tracks.title' => 'tracks.title' }, # / and definitive link back to root
232 { 'single_track.cd.artist.cds.cdid' => 'cds.cdid' }, # to give uniquiness to ...tracks.title below
233 { 'single_track.cd.artist.cds.year' => 'cds.year' }, # non-unique
234 { 'single_track.cd.artist.artistid' => 'artist.artistid' }, # uniqufies entire parental chain
235 { 'single_track.cd.artist.cds.genreid' => 'cds.genreid' }, # nullable
236 { 'single_track.cd.artist.cds.tracks.title' => 'tracks_2.title' }, # unique when combined with ...cds.cdid above
237 ],
238});
239
240for (1..3) {
241 $rs->create({ artist => 1, year => 1977, title => "fuzzy_$_" });
242}
243
244my $rs_random = $rs->search({}, { order_by => \ 'RANDOM()' });
245is ($rs_random->count, 6, 'row count matches');
246
247if ($ENV{TEST_VERBOSE}) {
248 my @lines = (
249 [ "What are we actually trying to collapse (Select/As, tests below will see a *DIFFERENT* random order):" ],
250 [ map { my $s = $_; $s =~ s/single_track\./sngl_tr./; $s } @{$rs_random->{_attrs}{select} } ],
251 $rs_random->{_attrs}{as},
252 [ "-" x 159 ],
253 $rs_random->cursor->all,
254 );
255
256 diag join ' # ', map { sprintf '% 15s', (defined $_ ? $_ : 'NULL') } @$_
257 for @lines;
258}
259
1e4f9fb3 260{
261 my $queries = 0;
262 $schema->storage->debugcb(sub { $queries++ });
263 my $orig_debug = $schema->storage->debug;
264 $schema->storage->debug (1);
fcf32d04 265
1e4f9fb3 266 for my $use_next (0, 1) {
267 my @random_cds;
268 if ($use_next) {
269 warnings_exist {
270 while (my $o = $rs_random->next) {
271 push @random_cds, $o;
272 }
273 } qr/performed an eager cursor slurp underneath/,
274 'Warned on auto-eager cursor';
fcf32d04 275 }
1e4f9fb3 276 else {
277 @random_cds = $rs_random->all;
fcf32d04 278 }
1e4f9fb3 279
280 is (@random_cds, 6, 'object count matches');
281
282 for my $cd (@random_cds) {
283 if ($cd->year == 1977) {
284 is( scalar $cd->tracks, 0, 'no tracks on 1977 cd' );
285 is( $cd->single_track, undef, 'no single_track on 1977 cd' );
286 }
287 elsif ($cd->year == 1976) {
288 is( scalar $cd->tracks, 2, 'Two tracks on 1976 cd' );
289 like( $_->title, qr/^o\d/, "correct title" )
290 for $cd->tracks;
291 is( $cd->single_track, undef, 'no single_track on 1976 cd' );
292 }
293 elsif ($cd->year == 1981) {
294 is( scalar $cd->tracks, 4, 'Four tracks on 1981 cd' );
295 like( $_->title, qr/^m\d/, "correct title" )
296 for $cd->tracks;
297 is( $cd->single_track, undef, 'no single_track on 1981 cd' );
298 }
299 elsif ($cd->year == 1978) {
300 is( scalar $cd->tracks, 3, 'Three tracks on 1978 cd' );
301 like( $_->title, qr/^e\d/, "correct title" )
302 for $cd->tracks;
303 ok( defined $cd->single_track, 'single track prefetched on 1987 cd' );
304 is( $cd->single_track->cd->artist->id, 1, 'Single_track->cd->artist prefetched on 1978 cd' );
305 is( scalar $cd->single_track->cd->artist->cds, 6, '6 cds prefetched on artist' );
306 }
fcf32d04 307 }
308 }
fcf32d04 309
1e4f9fb3 310 $schema->storage->debugcb(undef);
311 $schema->storage->debug($orig_debug);
312 is ($queries, 2, "Only two queries for two prefetch calls total");
313}
fcf32d04 314
52864fbd 315# can't cmp_deeply a random set - need *some* order
aa1d8a87 316my $ord_rs = $rs->search({}, {
fcf32d04 317 order_by => [ 'tracks_2.title', 'tracks.title', 'cds.cdid', \ 'RANDOM()' ],
aa1d8a87 318 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
319});
320my @hris_all = sort { $a->{year} cmp $b->{year} } $ord_rs->all;
321is (@hris_all, 6, 'hri count matches' );
322
323my $iter_rs = $rs->search({}, {
324 order_by => [ 'me.year', 'me.cdid', 'tracks_2.title', 'tracks.title', 'cds.cdid', \ 'RANDOM()' ],
325 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
326});
327my @hris_iter;
328while (my $r = $iter_rs->next) {
329 push @hris_iter, $r;
330}
331
332cmp_deeply(
333 \@hris_iter,
334 \@hris_all,
335 'Iteration works correctly',
336);
fcf32d04 337
1e4f9fb3 338my @hri_contents = (
339 { year => 1976, single_track => undef, tracks => [
340 { cd => 2, title => "o1" },
341 { cd => 2, title => "o2" },
342 ]},
343 { year => 1977, single_track => undef, tracks => [] },
344 { year => 1977, single_track => undef, tracks => [] },
345 { year => 1977, single_track => undef, tracks => [] },
fcf32d04 346 {
1e4f9fb3 347 year => 1978,
fcf32d04 348 single_track => {
1e4f9fb3 349 trackid => 6,
fcf32d04 350 cd => {
351 artist => {
1e4f9fb3 352 artistid => 1, cds => [
353 { cdid => 4, genreid => undef, year => 1977, tracks => [] },
354 { cdid => 5, genreid => undef, year => 1977, tracks => [] },
355 { cdid => 6, genreid => undef, year => 1977, tracks => [] },
356 { cdid => 3, genreid => 1, year => 1978, tracks => [
357 { title => "e1" },
358 { title => "e2" },
359 { title => "e3" },
360 ]},
361 { cdid => 1, genreid => 1, year => 1981, tracks => [
362 { title => "m1" },
363 { title => "m2" },
364 { title => "m3" },
365 { title => "m4" },
366 ]},
367 { cdid => 2, genreid => undef, year => 1976, tracks => [
368 { title => "o1" },
369 { title => "o2" },
370 ]},
fcf32d04 371 ]
1e4f9fb3 372 },
fcf32d04 373 },
fcf32d04 374 },
375 tracks => [
1e4f9fb3 376 { cd => 3, title => "e1" },
377 { cd => 3, title => "e2" },
378 { cd => 3, title => "e3" },
fcf32d04 379 ],
fcf32d04 380 },
1e4f9fb3 381 { year => 1981, single_track => undef, tracks => [
382 { cd => 1, title => "m1" },
383 { cd => 1, title => "m2" },
384 { cd => 1, title => "m3" },
385 { cd => 1, title => "m4" },
386 ]},
387);
388
389cmp_deeply (\@hris_all, \@hri_contents, 'W00T, multi-has_many manual underdefined root prefetch with collapse works');
390
391cmp_deeply(
392 $rs->search({}, {
393 order_by => [ 'me.year', 'tracks_2.title', 'tracks.title', 'cds.cdid', { -desc => 'name' } ],
394 rows => 4,
395 offset => 2,
396 })->all_hri,
397 [ @hri_contents[2..5] ],
398 'multi-has_many prefetch with limit works too',
399);
fcf32d04 400
1e4f9fb3 401# left-ordered real iterator
aa1d8a87 402$rs = $rs->search({}, { order_by => [ 'me.year', 'me.cdid', \ 'RANDOM()' ] });
403my @objs_iter;
404while (my $r = $rs->next) {
405 push @objs_iter, $r;
406}
407
408for my $i (0 .. $#objs_iter) {
409 is ($objs_iter[$i]->year, $hris_all[$i]{year}, "Expected year on object $i" );
410 is (
411 (defined $objs_iter[$i]->single_track),
412 (defined $hris_all[$i]{single_track}),
413 "Expected single relation on object $i"
414 );
415}
416
417$rs = $schema->resultset('Artist')->search({}, {
418 join => 'cds',
419 columns => ['cds.title', 'cds.artist' ],
420 collapse => 1,
421 order_by => [qw( me.name cds.title )],
422});
423
424$rs->create({ name => "${_}_cdless" })
425 for (qw( Z A ));
426
427cmp_deeply (
428 $rs->all_hri,
429 [
430 { cds => [] },
431 { cds => [
432 { artist => 1, title => "Equinoxe" },
433 { artist => 1, title => "Magnetic Fields" },
434 { artist => 1, title => "Oxygene" },
435 { artist => 1, title => "fuzzy_1" },
436 { artist => 1, title => "fuzzy_2" },
437 { artist => 1, title => "fuzzy_3" },
438 ] },
439 { cds => [] },
440 ],
441 'Expected HRI of 1:M with empty root selection',
442);
443
908aa1bb 444done_testing;