Warn in case of iterative collapse being upgraded to an eager cursor slurp
[dbsrgits/DBIx-Class.git] / t / prefetch / manual.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Deep;
6 use Test::Warn;
7 use Test::Exception;
8 use lib qw(t/lib);
9 use DBICTest;
10
11 my $schema = DBICTest->init_schema(no_populate => 1);
12
13 $schema->resultset('Artist')->create({ name => 'JMJ', cds => [{
14   title => 'Magnetic Fields',
15   year => 1981,
16   genre => { name => 'electro' },
17   tracks => [
18     { title => 'm1' },
19     { title => 'm2' },
20     { title => 'm3' },
21     { title => 'm4' },
22   ],
23 } ] });
24
25 $schema->resultset('CD')->create({
26   title => 'Equinoxe',
27   year => 1978,
28   artist => { name => 'JMJ' },
29   genre => { name => 'electro' },
30   tracks => [
31     { title => 'e1' },
32     { title => 'e2' },
33     { title => 'e3' },
34   ],
35   single_track => {
36     title => 'o1',
37     cd => {
38       title => 'Oxygene',
39       year => 1976,
40       artist => { name => 'JMJ' },
41       tracks => [
42         { title => 'o2', position => 2},  # the position should not be here, bug in MC
43       ],
44     },
45   },
46 });
47
48 my $rs = $schema->resultset ('CD')->search ({}, {
49   join => [ 'tracks', { single_track => { cd => { artist => { cds => 'tracks' } } } }  ],
50   collapse => 1,
51   columns => [
52     { 'year'                                    => 'me.year' },               # non-unique
53     { 'genreid'                                 => 'me.genreid' },            # nullable
54     { 'tracks.title'                            => 'tracks.title' },          # non-unique (no me.id)
55     { 'single_track.cd.artist.cds.cdid'         => 'cds.cdid' },              # to give uniquiness to ...tracks.title below
56     { 'single_track.cd.artist.artistid'         => 'artist.artistid' },       # uniqufies entire parental chain
57     { 'single_track.cd.artist.cds.year'         => 'cds.year' },              # non-unique
58     { 'single_track.cd.artist.cds.genreid'      => 'cds.genreid' },           # nullable
59     { 'single_track.cd.artist.cds.tracks.title' => 'tracks_2.title' },        # unique when combined with ...cds.cdid above
60     { 'latest_cd'                     => \ "(SELECT MAX(year) FROM cd)" },    # random function
61     { 'title'                                   => 'me.title' },              # uniquiness for me
62     { 'artist'                                  => 'me.artist' },             # uniquiness for me
63   ],
64   order_by => [{ -desc => 'cds.year' }, { -desc => 'me.title'} ],
65 });
66
67 my $hri_rs = $rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
68
69 cmp_deeply (
70   [$hri_rs->all],
71   [
72     {
73       artist => 1,
74       genreid => 1,
75       latest_cd => 1981,
76       single_track => {
77         cd => {
78           artist => {
79             artistid => 1,
80             cds => [
81               {
82                 cdid => 1,
83                 genreid => 1,
84                 tracks => [
85                   {
86                     title => "m1"
87                   },
88                   {
89                     title => "m2"
90                   },
91                   {
92                     title => "m3"
93                   },
94                   {
95                     title => "m4"
96                   }
97                 ],
98                 year => 1981
99               },
100               {
101                 cdid => 3,
102                 genreid => 1,
103                 tracks => [
104                   {
105                     title => "e1"
106                   },
107                   {
108                     title => "e2"
109                   },
110                   {
111                     title => "e3"
112                   }
113                 ],
114                 year => 1978
115               },
116               {
117                 cdid => 2,
118                 genreid => undef,
119                 tracks => [
120                   {
121                     title => "o1"
122                   },
123                   {
124                     title => "o2"
125                   }
126                 ],
127                 year => 1976
128               }
129             ]
130           }
131         }
132       },
133       title => "Equinoxe",
134       tracks => [
135         {
136           title => "e1"
137         },
138         {
139           title => "e2"
140         },
141         {
142           title => "e3"
143         }
144       ],
145       year => 1978
146     },
147     {
148       artist => 1,
149       genreid => undef,
150       latest_cd => 1981,
151       single_track => undef,
152       title => "Oxygene",
153       tracks => [
154         {
155           title => "o1"
156         },
157         {
158           title => "o2"
159         }
160       ],
161       year => 1976
162     },
163     {
164       artist => 1,
165       genreid => 1,
166       latest_cd => 1981,
167       single_track => undef,
168       title => "Magnetic Fields",
169       tracks => [
170         {
171           title => "m1"
172         },
173         {
174           title => "m2"
175         },
176         {
177           title => "m3"
178         },
179         {
180           title => "m4"
181         }
182       ],
183       year => 1981
184     },
185   ],
186   'W00T, manual prefetch with collapse works'
187 );
188
189 TODO: {
190   my ($row) = $rs->all;
191   local $TODO = 'Something is wrong with filter type rels, they throw on incomplete objects >.<';
192
193   lives_ok {
194     cmp_deeply (
195       { $row->single_track->get_columns },
196       {},
197       'empty intermediate object ok',
198     )
199   } 'no exception';
200 }
201
202 TODO: {
203 local $TODO = 'this does not work at all, need to promote rsattrs to an object on its own';
204 # make sure has_many column redirection does not do weird stuff when collapse is requested
205 for my $pref_args (
206   { prefetch => 'cds'},
207   { collapse => 1 }
208 ) {
209   for my $col_and_join_args (
210     { '+columns' => { 'cd_title' => 'cds_2.title' }, join => [ 'cds', 'cds' ] },
211     { '+columns' => { 'cd_title' => 'cds.title' }, join => 'cds', }
212   ) {
213
214     my $weird_rs = $schema->resultset('Artist')->search({}, {
215       %$col_and_join_args, %$pref_args,
216     });
217
218     for (qw/next all first/) {
219       throws_ok { $weird_rs->$_ } qr/not yet determined exception text/;
220     }
221   }
222 }
223 }
224
225 # multi-has_many with underdefined root, with rather random order
226 $rs = $schema->resultset ('CD')->search ({}, {
227   join => [ 'tracks', { single_track => { cd => { artist => { cds => 'tracks' } } } }  ],
228   collapse => 1,
229   columns => [
230     { 'single_track.trackid'                    => 'single_track.trackid' },  # definitive link to root from 1:1:1:1:M:M chain
231     { 'year'                                    => 'me.year' },               # non-unique
232     { 'tracks.cd'                               => 'tracks.cd' },             # \ together both uniqueness for second multirel
233     { 'tracks.title'                            => 'tracks.title' },          # / and definitive link back to root
234     { 'single_track.cd.artist.cds.cdid'         => 'cds.cdid' },              # to give uniquiness to ...tracks.title below
235     { 'single_track.cd.artist.cds.year'         => 'cds.year' },              # non-unique
236     { 'single_track.cd.artist.artistid'         => 'artist.artistid' },       # uniqufies entire parental chain
237     { 'single_track.cd.artist.cds.genreid'      => 'cds.genreid' },           # nullable
238     { 'single_track.cd.artist.cds.tracks.title' => 'tracks_2.title' },        # unique when combined with ...cds.cdid above
239   ],
240 });
241
242 for (1..3) {
243   $rs->create({ artist => 1, year => 1977, title => "fuzzy_$_" });
244 }
245
246 my $rs_random = $rs->search({}, { order_by => \ 'RANDOM()' });
247 is ($rs_random->count, 6, 'row count matches');
248
249 if ($ENV{TEST_VERBOSE}) {
250  my @lines = (
251     [ "What are we actually trying to collapse (Select/As, tests below will see a *DIFFERENT* random order):" ],
252     [ map { my $s = $_; $s =~ s/single_track\./sngl_tr./; $s } @{$rs_random->{_attrs}{select} } ],
253     $rs_random->{_attrs}{as},
254     [ "-" x 159 ],
255     $rs_random->cursor->all,
256   );
257
258   diag join ' # ', map { sprintf '% 15s', (defined $_ ? $_ : 'NULL') } @$_
259     for @lines;
260 }
261
262 my $queries = 0;
263 $schema->storage->debugcb(sub { $queries++ });
264 my $orig_debug = $schema->storage->debug;
265 $schema->storage->debug (1);
266
267 for my $use_next (0, 1) {
268   my @random_cds;
269   if ($use_next) {
270     warnings_exist {
271       while (my $o = $rs_random->next) {
272         push @random_cds, $o;
273       }
274     } qr/performed an eager cursor slurp underneath/,
275     'Warned on auto-eager cursor';
276   }
277   else {
278     @random_cds = $rs_random->all;
279   }
280
281   is (@random_cds, 6, 'object count matches');
282
283   for my $cd (@random_cds) {
284     if ($cd->year == 1977) {
285       is( scalar $cd->tracks, 0, 'no tracks on 1977 cd' );
286       is( $cd->single_track, undef, 'no single_track on 1977 cd' );
287     }
288     elsif ($cd->year == 1976) {
289       is( scalar $cd->tracks, 2, 'Two tracks on 1976 cd' );
290       like( $_->title, qr/^o\d/, "correct title" )
291         for $cd->tracks;
292       is( $cd->single_track, undef, 'no single_track on 1976 cd' );
293     }
294     elsif ($cd->year == 1981) {
295       is( scalar $cd->tracks, 4, 'Four tracks on 1981 cd' );
296       like( $_->title, qr/^m\d/, "correct title" )
297         for $cd->tracks;
298       is( $cd->single_track, undef, 'no single_track on 1981 cd' );
299     }
300     elsif ($cd->year == 1978) {
301       is( scalar $cd->tracks, 3, 'Three tracks on 1978 cd' );
302       like( $_->title, qr/^e\d/, "correct title" )
303         for $cd->tracks;
304       ok( defined $cd->single_track, 'single track prefetched on 1987 cd' );
305       is( $cd->single_track->cd->artist->id, 1, 'Single_track->cd->artist prefetched on 1978 cd' );
306       is( scalar $cd->single_track->cd->artist->cds, 6, '6 cds prefetched on artist' );
307     }
308   }
309 }
310
311 $schema->storage->debugcb(undef);
312 $schema->storage->debug($orig_debug);
313 is ($queries, 2, "Only two queries for rwo prefetch calls total");
314
315 # can't cmp_deeply a random set - need *some* order
316 my $ord_rs = $rs->search({}, {
317   order_by => [ 'tracks_2.title', 'tracks.title', 'cds.cdid', \ 'RANDOM()' ],
318   result_class => 'DBIx::Class::ResultClass::HashRefInflator',
319 });
320 my @hris_all = sort { $a->{year} cmp $b->{year} } $ord_rs->all;
321 is (@hris_all, 6, 'hri count matches' );
322
323 my $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 });
327 my @hris_iter;
328 while (my $r = $iter_rs->next) {
329   push @hris_iter, $r;
330 }
331
332 cmp_deeply(
333   \@hris_iter,
334   \@hris_all,
335   'Iteration works correctly',
336 );
337
338 cmp_deeply (\@hris_all, [
339   {
340     single_track => undef,
341     tracks => [
342       {
343         cd => 2,
344         title => "o1"
345       },
346       {
347         cd => 2,
348         title => "o2"
349       }
350     ],
351     year => 1976
352   },
353   {
354     single_track => undef,
355     tracks => [],
356     year => 1977
357   },
358   {
359     single_track => undef,
360     tracks => [],
361     year => 1977
362   },
363   {
364     single_track => undef,
365     tracks => [],
366     year => 1977
367   },
368   {
369     single_track => {
370       cd => {
371         artist => {
372           artistid => 1,
373           cds => [
374             {
375               cdid => 4,
376               genreid => undef,
377               tracks => [],
378               year => 1977
379             },
380             {
381               cdid => 5,
382               genreid => undef,
383               tracks => [],
384               year => 1977
385             },
386             {
387               cdid => 6,
388               genreid => undef,
389               tracks => [],
390               year => 1977
391             },
392             {
393               cdid => 3,
394               genreid => 1,
395               tracks => [
396                 {
397                   title => "e1"
398                 },
399                 {
400                   title => "e2"
401                 },
402                 {
403                   title => "e3"
404                 }
405               ],
406               year => 1978
407             },
408             {
409               cdid => 1,
410               genreid => 1,
411               tracks => [
412                 {
413                   title => "m1"
414                 },
415                 {
416                   title => "m2"
417                 },
418                 {
419                   title => "m3"
420                 },
421                 {
422                   title => "m4"
423                 }
424               ],
425               year => 1981
426             },
427             {
428               cdid => 2,
429               genreid => undef,
430               tracks => [
431                 {
432                   title => "o1"
433                 },
434                 {
435                   title => "o2"
436                 }
437               ],
438               year => 1976
439             }
440           ]
441         }
442       },
443       trackid => 6
444     },
445     tracks => [
446       {
447         cd => 3,
448         title => "e1"
449       },
450       {
451         cd => 3,
452         title => "e2"
453       },
454       {
455         cd => 3,
456         title => "e3"
457       },
458     ],
459     year => 1978
460   },
461   {
462     single_track => undef,
463     tracks => [
464       {
465         cd => 1,
466         title => "m1"
467       },
468       {
469         cd => 1,
470         title => "m2"
471       },
472       {
473         cd => 1,
474         title => "m3"
475       },
476       {
477         cd => 1,
478         title => "m4"
479       },
480     ],
481     year => 1981
482   },
483 ], 'W00T, multi-has_many manual underdefined root prefetch with collapse works');
484
485 $rs = $rs->search({}, { order_by => [ 'me.year', 'me.cdid', \ 'RANDOM()' ] });
486 my @objs_iter;
487 while (my $r = $rs->next) {
488   push @objs_iter, $r;
489 }
490
491 for my $i (0 .. $#objs_iter) {
492   is ($objs_iter[$i]->year, $hris_all[$i]{year}, "Expected year on object $i" );
493   is (
494     (defined $objs_iter[$i]->single_track),
495     (defined $hris_all[$i]{single_track}),
496     "Expected single relation on object $i"
497   );
498 }
499
500 $rs = $schema->resultset('Artist')->search({}, {
501   join => 'cds',
502   columns => ['cds.title', 'cds.artist' ],
503   collapse => 1,
504   order_by => [qw( me.name cds.title )],
505 });
506
507 $rs->create({ name => "${_}_cdless" })
508   for (qw( Z A ));
509
510 cmp_deeply (
511   $rs->all_hri,
512   [
513     { cds => [] },
514     { cds => [
515       { artist => 1, title => "Equinoxe" },
516       { artist => 1, title => "Magnetic Fields" },
517       { artist => 1, title => "Oxygene" },
518       { artist => 1, title => "fuzzy_1" },
519       { artist => 1, title => "fuzzy_2" },
520       { artist => 1, title => "fuzzy_3" },
521     ] },
522     { cds => [] },
523   ],
524   'Expected HRI of 1:M with empty root selection',
525 );
526
527 done_testing;