X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fprefetch%2Fincomplete.t;h=781c1e1eacee7d08ab7fd605ddab773f7353a049;hb=4e9fc3f33df616fb7340d05e304ff985b9cce9cb;hp=8682ba784aeb4789edeb2dc4855319916920a6a1;hpb=ce9f555e55e767a287831c0fcccb337cc36905de;p=dbsrgits%2FDBIx-Class.git diff --git a/t/prefetch/incomplete.t b/t/prefetch/incomplete.t index 8682ba7..781c1e1 100644 --- a/t/prefetch/incomplete.t +++ b/t/prefetch/incomplete.t @@ -1,5 +1,5 @@ use strict; -use warnings; +use warnings; use Test::More; use Test::Exception; @@ -10,18 +10,18 @@ my $schema = DBICTest->init_schema(); lives_ok(sub { # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch) - # only the requested me.name column will be fetched. + # only the requested me.name/me.artistid columns will be fetched. # reference sql with select => [...] - # SELECT me.name, cds.title, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ... + # SELECT me.name, cds.title, me.artistid, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ... my $rs = $schema->resultset('Artist')->search( { 'cds.title' => { '!=', 'Generic Manufactured Singles' } }, { prefetch => [ qw/ cds / ], order_by => [ { -desc => 'me.name' }, 'cds.title' ], - select => [qw/ me.name cds.title / ], - } + select => [qw/ me.name cds.title me.artistid / ], + }, ); is ($rs->count, 2, 'Correct number of collapsed artists'); @@ -31,6 +31,56 @@ lives_ok(sub { is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist'); }, 'explicit prefetch on a keyless object works'); +lives_ok ( sub { + + my $rs = $schema->resultset('CD')->search( + {}, + { + order_by => [ { -desc => 'me.year' } ], + } + ); + my $years = [qw/ 2001 2001 1999 1998 1997/]; + + is_deeply ( + [ $rs->search->get_column('me.year')->all ], + $years, + 'Expected years (at least one duplicate)', + ); + + my @cds_and_tracks; + for my $cd ($rs->all) { + my $data = { year => $cd->year, cdid => $cd->cdid }; + for my $tr ($cd->tracks->all) { + push @{$data->{tracks}}, { $tr->get_columns }; + } + push @cds_and_tracks, $data; + } + + my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' }); + + my @pref_cds_and_tracks; + for my $cd ($pref_rs->all) { + my $data = { $cd->get_columns }; + for my $tr ($cd->tracks->all) { + push @{$data->{tracks}}, { $tr->get_columns }; + } + push @pref_cds_and_tracks, $data; + } + + is_deeply ( + \@pref_cds_and_tracks, + \@cds_and_tracks, + 'Correct collapsing on non-unique primary object' + ); + + is_deeply ( + [ $pref_rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all ], + \@cds_and_tracks, + 'Correct HRI collapsing on non-unique primary object' + ); + +}, 'weird collapse lives'); + lives_ok(sub { # test implicit prefetch as well @@ -55,7 +105,7 @@ throws_ok( sub { $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next; }, - qr|\QCan't inflate manual prefetch into non-existent relationship 'artist' from 'Track', check the inflation specification (columns/as) ending in 'artist.name'|, + qr|\QCan't inflate prefetch into non-existent relationship 'artist' from 'Track', check the inflation specification (columns/as) ending in '...artist.name'|, 'Sensible error message on mis-specified "as"', );