11 delete $ENV{DBIC_COLUMNS_INCLUDE_FILTER_RELS};
13 my $schema = DBICTest->init_schema(no_populate => 1);
15 $schema->resultset('Artist')->create({ name => 'JMJ', cds => [{
16 title => 'Magnetic Fields',
18 genre => { name => 'electro' },
27 $schema->resultset('CD')->create({
30 artist => { name => 'JMJ' },
31 genre => { name => 'electro' },
42 artist => { name => 'JMJ' },
44 { title => 'o2', position => 2}, # the position should not be here, bug in MC
50 my $rs = $schema->resultset ('CD')->search ({}, {
51 join => [ 'tracks', { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
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
58 { 'single_track.cd.artist.artistid' => 'artist.artistid' }, # uniqufies entire parental chain
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
62 { 'latest_cd' => \ "(SELECT MAX(year) FROM cd)" }, # random function
63 { 'title' => 'me.title' }, # uniquiness for me
64 { 'artist' => 'me.artist' }, # uniquiness for me
66 order_by => [{ -desc => 'cds.year' }, { -desc => 'me.title'}, 'tracks.title', 'tracks_2.title' ],
69 my $hri_rs = $rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
74 { artist => 1, genreid => 1, latest_cd => 1981, title => "Equinoxe", year => 1978,
77 artist => { artistid => 1, cds => [
78 { cdid => 1, genreid => 1, year => 1981, tracks => [
84 { cdid => 3, genreid => 1, year => 1978, tracks => [
89 { cdid => 2, genreid => undef, year => 1976, tracks => [
103 artist => 1, genreid => undef, latest_cd => 1981, title => "Oxygene", year => 1976, single_track => undef,
110 artist => 1, genreid => 1, latest_cd => 1981, title => "Magnetic Fields", year => 1981, single_track => undef,
119 'W00T, manual prefetch with collapse works'
122 lives_ok { my $dummy = $rs; warnings_exist {
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;
129 my $empty_single_columns = {
132 my $empty_single_inflated_columns = {
135 my $empty_cd_columns = {
136 artist => $art_obj->artistid
138 my $empty_cd_inflated_columns = {
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
151 ### this tests the standard root -> single -> filter ->filter
152 my ($row) = $rs->all; # don't trigger order warnings
155 { $row->single_track->get_columns },
156 $empty_single_columns,
157 "No unexpected columns available on intermediate 'single' rel with a chained 'filter' prefetch",
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",
167 { $row->single_track->cd->get_columns },
169 "No unexpected columns available on intermediate 'single' rel with 2x chained 'filter' prefetch",
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",
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' } } },
182 'single_track.disc.artist.artistid' => 'artist.artistid',
183 'single_track.disc.artist.cds.cdid' => 'cds.cdid',
189 { $row->single_track->get_columns },
191 "No unexpected columns available on intermediate 'single' rel with a chained 'single' prefetch",
195 { $row->single_track->get_inflated_columns },
197 "No unexpected inflated columns available on intermediate 'single' rel with a chained 'single' prefetch",
201 { $row->single_track->disc->get_columns },
203 "No unexpected columns available on intermediate 'single' rel with chained 'single' and chained 'filter' prefetch",
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",
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';
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' } } } } ],
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
241 $rs->create({ artist => 1, year => 1977, title => "fuzzy_$_" });
244 my $rs_random = $rs->search({}, { order_by => \ 'RANDOM()' });
245 is ($rs_random->count, 6, 'row count matches');
247 if ($ENV{TEST_VERBOSE}) {
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},
253 $rs_random->cursor->all,
256 diag join ' # ', map { sprintf '% 15s', (defined $_ ? $_ : 'NULL') } @$_
262 $schema->storage->debugcb(sub { $queries++ });
263 my $orig_debug = $schema->storage->debug;
264 $schema->storage->debug (1);
266 for my $use_next (0, 1) {
270 while (my $o = $rs_random->next) {
271 push @random_cds, $o;
273 } qr/performed an eager cursor slurp underneath/,
274 'Warned on auto-eager cursor';
277 @random_cds = $rs_random->all;
280 is (@random_cds, 6, 'object count matches');
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' );
287 elsif ($cd->year == 1976) {
288 is( scalar $cd->tracks, 2, 'Two tracks on 1976 cd' );
289 like( $_->title, qr/^o\d/, "correct title" )
291 is( $cd->single_track, undef, 'no single_track on 1976 cd' );
293 elsif ($cd->year == 1981) {
294 is( scalar $cd->tracks, 4, 'Four tracks on 1981 cd' );
295 like( $_->title, qr/^m\d/, "correct title" )
297 is( $cd->single_track, undef, 'no single_track on 1981 cd' );
299 elsif ($cd->year == 1978) {
300 is( scalar $cd->tracks, 3, 'Three tracks on 1978 cd' );
301 like( $_->title, qr/^e\d/, "correct title" )
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' );
310 $schema->storage->debugcb(undef);
311 $schema->storage->debug($orig_debug);
312 is ($queries, 2, "Only two queries for two prefetch calls total");
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',
320 my @hris_all = sort { $a->{year} cmp $b->{year} } $ord_rs->all;
321 is (@hris_all, 6, 'hri count matches' );
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',
328 while (my $r = $iter_rs->next) {
335 'Iteration works correctly',
339 { year => 1976, single_track => undef, tracks => [
340 { cd => 2, title => "o1" },
341 { cd => 2, title => "o2" },
343 { year => 1977, single_track => undef, tracks => [] },
344 { year => 1977, single_track => undef, tracks => [] },
345 { year => 1977, single_track => undef, tracks => [] },
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 => [
361 { cdid => 1, genreid => 1, year => 1981, tracks => [
367 { cdid => 2, genreid => undef, year => 1976, tracks => [
376 { cd => 3, title => "e1" },
377 { cd => 3, title => "e2" },
378 { cd => 3, title => "e3" },
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" },
389 cmp_deeply (\@hris_all, \@hri_contents, 'W00T, multi-has_many manual underdefined root prefetch with collapse works');
393 order_by => [ 'me.year', 'tracks_2.title', 'tracks.title', 'cds.cdid', { -desc => 'name' } ],
397 [ @hri_contents[2..5] ],
398 'multi-has_many prefetch with limit works too',
401 # left-ordered real iterator
402 $rs = $rs->search({}, { order_by => [ 'me.year', 'me.cdid', \ 'RANDOM()' ] });
404 while (my $r = $rs->next) {
408 for my $i (0 .. $#objs_iter) {
409 is ($objs_iter[$i]->year, $hris_all[$i]{year}, "Expected year on object $i" );
411 (defined $objs_iter[$i]->single_track),
412 (defined $hris_all[$i]{single_track}),
413 "Expected single relation on object $i"
417 $rs = $schema->resultset('Artist')->search({}, {
419 columns => ['cds.title', 'cds.artist' ],
421 order_by => [qw( me.name cds.title )],
424 $rs->create({ name => "${_}_cdless" })
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" },
441 'Expected HRI of 1:M with empty root selection',