9 my $schema = DBICTest->init_schema(no_populate => 1);
11 $schema->resultset('Artist')->create({ name => 'JMJ', cds => [{
12 title => 'Magnetic Fields',
14 genre => { name => 'electro' },
23 $schema->resultset('CD')->create({
26 artist => { name => 'JMJ' },
27 genre => { name => 'electro' },
38 artist => { name => 'JMJ' },
40 { title => 'o2', position => 2}, # the position should not be needed here, bug in MC
46 $schema->resultset('CD')->create({ artist => 1, year => 1977, title => "fuzzy_1" });
49 package DBICTest::_IRCapture;
50 sub inflate_result { [@_[2,3]] };
54 ([$schema->resultset ('CD')->search ({}, {
55 result_class => 'DBICTest::_IRCapture',
56 prefetch => { single_track => { cd => 'artist' } },
57 order_by => 'me.cdid',
61 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
62 { single_track => bless( [
63 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
65 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
68 { artistid => undef, name => undef, charfield => undef, rank => undef }
69 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class )
71 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
72 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
75 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
76 { single_track => bless( [
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 }
83 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class )
85 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
86 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
89 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
91 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
93 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
96 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 }
103 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
104 { single_track => bless( [
105 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
107 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
110 { artistid => undef, name => undef, charfield => undef, rank => undef }
111 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class )
113 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
114 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
117 'Simple 1:1 descend with classic prefetch'
121 [$schema->resultset ('CD')->search ({}, {
122 result_class => 'DBICTest::_IRCapture',
123 join => { single_track => { cd => 'artist' } },
125 { 'year' => 'me.year' },
126 { 'genreid' => 'me.genreid' },
127 { 'single_track.cd.artist.artistid' => 'artist.artistid' },
128 { 'title' => 'me.title' },
129 { 'artist' => 'me.artist' },
131 order_by => 'me.cdid',
135 { artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
136 { single_track => bless( [
142 { artistid => undef }
146 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
149 { artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
150 { single_track => bless( [
156 { artistid => undef }
160 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
163 { artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
177 { artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
178 { single_track => bless( [
184 { artistid => undef }
188 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) }
191 'Simple 1:1 descend with missing selectors'
195 ([$schema->resultset ('CD')->search ({}, {
196 result_class => 'DBICTest::_IRCapture',
197 prefetch => [ { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
198 order_by => [qw/me.cdid tracks.trackid/],
202 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
203 { single_track => bless( [
204 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
206 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
209 { artistid => undef, name => undef, charfield => undef, rank => undef },
211 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
212 { tracks => bless( [ [
213 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
214 ] ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
215 ] ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
219 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
222 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
223 { single_track => bless( [
224 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
226 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
229 { artistid => undef, name => undef, charfield => undef, rank => undef },
231 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
232 { tracks => bless( [ [
233 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
234 ] ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
235 ] ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
239 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
242 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
244 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
246 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
249 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
252 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
254 [ { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef } ],
255 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
258 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
260 [ { trackid => 1, title => 'm1', position => 1, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
261 [ { trackid => 2, title => 'm2', position => 2, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
262 [ { trackid => 3, title => 'm3', position => 3, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
263 [ { trackid => 4, title => 'm4', position => 4, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
267 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
269 [ { trackid => 5, title => 'o2', position => 2, cd => 2, last_updated_at => undef, last_updated_on => undef } ],
270 [ { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef } ],
274 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
276 [ { trackid => 7, title => 'e1', position => 1, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
277 [ { trackid => 8, title => 'e2', position => 2, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
278 [ { trackid => 9, title => 'e3', position => 3, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
288 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
289 { single_track => bless( [
290 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
292 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
295 { artistid => undef, name => undef, charfield => undef, rank => undef },
297 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
298 { tracks => bless( [ [
299 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
300 ] ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
301 ] ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
305 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
308 'Collapsing 1:1 ending in chained has_many with classic prefetch'
312 ([$schema->resultset ('Artist')->search ({}, {
313 result_class => 'DBICTest::_IRCapture',
314 join => { cds => 'tracks' },
316 (map { "cds.$_" } $schema->source('CD')->columns),
317 (map { +{ "cds.tracks.$_" => "tracks.$_" } } $schema->source('Track')->columns),
319 order_by => [qw/cds.cdid tracks.trackid/],
323 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
325 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
327 { trackid => 1, title => 'm1', position => 1, cd => 1, last_updated_at => undef, last_updated_on => undef },
332 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
334 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
336 { trackid => 2, title => 'm2', position => 2, cd => 1, last_updated_at => undef, last_updated_on => undef },
341 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
343 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
345 { trackid => 3, title => 'm3', position => 3, cd => 1, last_updated_at => undef, last_updated_on => undef },
350 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
352 { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
354 { trackid => 4, title => 'm4', position => 4, cd => 1, last_updated_at => undef, last_updated_on => undef },
359 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
361 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
363 { trackid => 5, title => 'o2', position => 2, cd => 2, last_updated_at => undef, last_updated_on => undef },
368 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
370 { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
372 { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
377 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
379 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
381 { trackid => 7, title => 'e1', position => 1, cd => 3, last_updated_at => undef, last_updated_on => undef },
386 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
388 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
390 { trackid => 8, title => 'e2', position => 2, cd => 3, last_updated_at => undef, last_updated_on => undef },
395 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
397 { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
399 { trackid => 9, title => 'e3', position => 3, cd => 3, last_updated_at => undef, last_updated_on => undef },
404 { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
406 { cdid => 4, single_track => undef, artist => 1, genreid => undef, year => 1977, title => "fuzzy_1" },
408 { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
409 ], $DBIx::Class::ResultSource::RowParser::Util::null_branch_class ) },
413 'Non-Collapsing chained has_many'
416 $schema->resultset('Artist')->create({ name => "${_}_cdless" })
420 [$schema->resultset ('Artist')->search ({}, {
421 result_class => 'DBICTest::_IRCapture',
424 columns => [qw( cds.title cds.artist )],
425 order_by => [qw( me.name cds.title )],
431 [ { artist => undef, title => undef } ]
432 ], 'DBIx::ResultParser::RelatedNullBranch' ) },
437 [ { artist => 1, title => "Equinoxe" } ],
438 [ { artist => 1, title => "Magnetic Fields" } ],
439 [ { artist => 1, title => "Oxygene" } ],
440 [ { artist => 1, title => "fuzzy_1" } ],
446 [ { artist => undef, title => undef } ]
447 ], 'DBIx::ResultParser::RelatedNullBranch' ) },
450 'Expected output of collapsing 1:M with empty root selection',
454 my ($left, $right, $msg) = @_;
456 local $Test::Builder::Level = $Test::Builder::Level + 1;
457 cmp_deeply($left, $right, $msg||());