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