9 my $new_collapser_version = DBIx::Class::ResultSet->can('_construct_objects');
11 my $schema = DBICTest->init_schema(no_populate => 1);
13 $schema->resultset('Artist')->create({ name => 'JMJ', cds => [{
14 title => 'Magnetic Fields',
16 genre => { name => 'electro' },
25 $schema->resultset('CD')->create({
28 artist => { name => 'JMJ' },
29 genre => { name => 'electro' },
40 artist => { name => 'JMJ' },
42 { title => 'o2', position => 2}, # the position should not be needed here, bug in MC
48 $schema->resultset('CD')->create({ artist => 1, year => 1977, title => "fuzzy_1" });
51 package DBICTest::_IRCapture;
52 sub inflate_result { [@_[2,3]] };
56 package DBICTest::_IRCaptureAround;
57 use base 'DBIx::Class::Row';
58 sub inflate_result { [@_[2,3]] };
62 { $schema->resultset ('CD')->search ({}, { result_class => 'DBICTest::_IRCapture', prefetch => 'tracks' } )->all }
63 qr/\QResultClass DBICTest::_IRCapture does not inherit from DBIx::Class::Row and therefore its inflate_result() will receive the full prefetched data tree, without any branch definedness checks/,
64 'Legacy inflate_result() API warned',
65 if $new_collapser_version;
68 ([$schema->resultset ('CD')->search ({}, {
69 result_class => 'DBICTest::_IRCapture',
70 prefetch => { single_track => { cd => 'artist' } },
71 order_by => 'me.cdid',
75 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
77 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
79 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
82 { artistid => undef, name => undef, charfield => undef, rank => undef }
89 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
91 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
93 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
96 { artistid => undef, name => undef, charfield => undef, rank => undef }
103 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
105 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
107 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
110 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 }
117 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
119 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
121 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
124 { artistid => undef, name => undef, charfield => undef, rank => undef }
131 'Simple 1:1 descend with classic prefetch legacy'
135 ([$schema->resultset ('CD')->search ({}, {
136 result_class => 'DBICTest::_IRCaptureAround',
137 prefetch => { single_track => { cd => 'artist' } },
138 order_by => 'me.cdid',
142 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
143 { single_track => [] }
146 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
147 { single_track => [] }
150 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
152 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
154 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
157 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 }
164 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
165 { single_track => [ ] }
168 'Simple 1:1 descend with classic prefetch pruning'
173 [$schema->resultset ('CD')->search ({}, {
174 result_class => 'DBICTest::_IRCapture',
175 join => { single_track => { cd => 'artist' } },
177 { 'year' => 'me.year' },
178 { 'genreid' => 'me.genreid' },
179 { 'single_track.cd.artist.artistid' => 'artist.artistid' },
180 { 'title' => 'me.title' },
181 { 'artist' => 'me.artist' },
183 order_by => 'me.cdid',
187 { artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
194 { artistid => undef }
201 { artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
208 { artistid => undef }
215 { artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
229 { artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
236 { artistid => undef }
243 'Simple 1:1 descend with missing selectors legacy'
247 [$schema->resultset ('CD')->search ({}, {
248 result_class => 'DBICTest::_IRCaptureAround',
249 join => { single_track => { cd => 'artist' } },
251 { 'year' => 'me.year' },
252 { 'genreid' => 'me.genreid' },
253 { 'single_track.cd.artist.artistid' => 'artist.artistid' },
254 { 'title' => 'me.title' },
255 { 'artist' => 'me.artist' },
257 order_by => 'me.cdid',
261 { artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
262 { single_track => [] }
265 { artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
266 { single_track => [ ] }
269 { artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
283 { artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
284 { single_track => [] }
287 'Simple 1:1 descend with missing selectors pruning'
291 ([$schema->resultset ('CD')->search ({}, {
292 result_class => 'DBICTest::_IRCapture',
293 prefetch => [ { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
294 order_by => [qw/me.cdid tracks.trackid/],
298 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
300 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
302 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
305 { artistid => undef, name => undef, charfield => undef, rank => undef },
307 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
309 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
318 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
320 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
322 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
325 { artistid => undef, name => undef, charfield => undef, rank => undef },
327 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
329 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
338 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
340 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
342 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
345 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
348 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
350 [ { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef } ],
354 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
356 [ { trackid => 1, title => 'm1', position => 1, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
357 [ { trackid => 2, title => 'm2', position => 2, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
358 [ { trackid => 3, title => 'm3', position => 3, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
359 [ { trackid => 4, title => 'm4', position => 4, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
363 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
365 [ { trackid => 5, title => 'o2', position => 2, cd => 2, last_updated_at => undef, last_updated_on => undef } ],
366 [ { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef } ],
370 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
372 [ { trackid => 7, title => 'e1', position => 1, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
373 [ { trackid => 8, title => 'e2', position => 2, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
374 [ { trackid => 9, title => 'e3', position => 3, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
384 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
386 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
388 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
391 { artistid => undef, name => undef, charfield => undef, rank => undef },
393 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
395 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
404 'Collapsing 1:1 ending in chained has_many with classic prefetch legacy'
408 ([$schema->resultset ('CD')->search ({}, {
409 result_class => 'DBICTest::_IRCaptureAround',
410 prefetch => [ { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
411 order_by => [qw/me.cdid tracks.trackid/],
415 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
416 { single_track => [ ] },
419 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
420 { single_track => [ ] },
423 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
425 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
427 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
430 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
433 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
437 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
439 [ { trackid => 1, title => 'm1', position => 1, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
440 [ { trackid => 2, title => 'm2', position => 2, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
441 [ { trackid => 3, title => 'm3', position => 3, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
442 [ { trackid => 4, title => 'm4', position => 4, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
446 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
448 [ { trackid => 5, title => 'o2', position => 2, cd => 2, last_updated_at => undef, last_updated_on => undef } ],
449 [ { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef } ],
453 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
455 [ { trackid => 7, title => 'e1', position => 1, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
456 [ { trackid => 8, title => 'e2', position => 2, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
457 [ { trackid => 9, title => 'e3', position => 3, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
467 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
468 { single_track => [ ] },
471 'Collapsing 1:1 ending in chained has_many with classic prefetch pruning'
475 ([$schema->resultset ('Artist')->search ({}, {
476 result_class => 'DBICTest::_IRCapture',
477 join => { cds => 'tracks' },
479 (map { "cds.$_" } $schema->source('CD')->columns),
480 (map { +{ "cds.tracks.$_" => "tracks.$_" } } $schema->source('Track')->columns),
482 order_by => [qw/cds.cdid tracks.trackid/],
486 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
488 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
490 { trackid => 1, title => 'm1', position => 1, cd => 1, last_updated_at => undef, last_updated_on => undef },
495 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
497 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
499 { trackid => 2, title => 'm2', position => 2, cd => 1, last_updated_at => undef, last_updated_on => undef },
504 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
506 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
508 { trackid => 3, title => 'm3', position => 3, cd => 1, last_updated_at => undef, last_updated_on => undef },
513 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
515 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
517 { trackid => 4, title => 'm4', position => 4, cd => 1, last_updated_at => undef, last_updated_on => undef },
522 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
524 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
526 { trackid => 5, title => 'o2', position => 2, cd => 2, last_updated_at => undef, last_updated_on => undef },
531 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
533 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
535 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
540 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
542 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
544 { trackid => 7, title => 'e1', position => 1, cd => 3, last_updated_at => undef, last_updated_on => undef },
549 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
551 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
553 { trackid => 8, title => 'e2', position => 2, cd => 3, last_updated_at => undef, last_updated_on => undef },
558 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
560 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
562 { trackid => 9, title => 'e3', position => 3, cd => 3, last_updated_at => undef, last_updated_on => undef },
567 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
569 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
571 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
576 'Non-Collapsing chained has_many legacy'
580 ([$schema->resultset ('Artist')->search ({}, {
581 result_class => 'DBICTest::_IRCaptureAround',
582 join => { cds => 'tracks' },
584 (map { "cds.$_" } $schema->source('CD')->columns),
585 (map { +{ "cds.tracks.$_" => "tracks.$_" } } $schema->source('Track')->columns),
587 order_by => [qw/cds.cdid tracks.trackid/],
591 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
593 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
595 { trackid => 1, title => 'm1', position => 1, cd => 1, last_updated_at => undef, last_updated_on => undef },
600 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
602 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
604 { trackid => 2, title => 'm2', position => 2, cd => 1, last_updated_at => undef, last_updated_on => undef },
609 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
611 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
613 { trackid => 3, title => 'm3', position => 3, cd => 1, last_updated_at => undef, last_updated_on => undef },
618 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
620 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
622 { trackid => 4, title => 'm4', position => 4, cd => 1, last_updated_at => undef, last_updated_on => undef },
627 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
629 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
631 { trackid => 5, title => 'o2', position => 2, cd => 2, last_updated_at => undef, last_updated_on => undef },
636 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
638 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
640 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
645 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
647 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
649 { trackid => 7, title => 'e1', position => 1, cd => 3, last_updated_at => undef, last_updated_on => undef },
654 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
656 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
658 { trackid => 8, title => 'e2', position => 2, cd => 3, last_updated_at => undef, last_updated_on => undef },
663 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
665 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
667 { trackid => 9, title => 'e3', position => 3, cd => 3, last_updated_at => undef, last_updated_on => undef },
672 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
674 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
679 'Non-Collapsing chained has_many pruning'
683 my ($left, $right, $msg) = @_;
685 local $TODO = "Pruning test won't work on pre-rewrite DBIC"
686 if ($msg||'') =~ /pruning$/ and ! $new_collapser_version;
688 local $Test::Builder::Level = $Test::Builder::Level + 1;
689 is_deeply($left, $right, $msg||());