X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fprefetch%2Fincomplete.t;h=a710fbbb9eb860d1c8ac4014937431eb69783b55;hb=1759f82f9191bd4ec6ad743d066082242ddbce39;hp=02c648bcbcbab68495a69cfbbfc13d59576d1627;hpb=c97338007ede15e7c62095a642b3de382a3508bd;p=dbsrgits%2FDBIx-Class.git diff --git a/t/prefetch/incomplete.t b/t/prefetch/incomplete.t index 02c648b..a710fbb 100644 --- a/t/prefetch/incomplete.t +++ b/t/prefetch/incomplete.t @@ -2,14 +2,16 @@ use strict; use warnings; use Test::More; +use Test::Deep; use Test::Exception; use lib qw(t/lib); use DBICTest; +use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); lives_ok(sub { - # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch) + # while cds.* will be selected anyway (prefetch implies it) # only the requested me.name column will be fetched. # reference sql with select => [...] @@ -20,17 +22,67 @@ lives_ok(sub { { prefetch => [ qw/ cds / ], order_by => [ { -desc => 'me.name' }, 'cds.title' ], - select => [qw/ me.name cds.title / ], - } + select => [qw/ me.name cds.title / ], + }, ); is ($rs->count, 2, 'Correct number of collapsed artists'); - my $we_are_goth = $rs->first; + my ($we_are_goth) = $rs->all; is ($we_are_goth->name, 'We Are Goth', 'Correct first artist'); is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist'); 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/]; + + cmp_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; + } + + cmp_deeply ( + \@pref_cds_and_tracks, + \@cds_and_tracks, + 'Correct collapsing on non-unique primary object' + ); + + cmp_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 +107,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|\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'|, 'Sensible error message on mis-specified "as"', ); @@ -68,9 +120,105 @@ throws_ok( prefetch => 'books', }); - lives_ok { - is ($pref_rs->all, 1, 'Expected count of objects on limtied prefetch') - } "Complex limited prefetch works with non-selected join condition"; + is_same_sql_bind( + $pref_rs->as_query, + '( + SELECT me.name, books.id, books.source, books.owner, books.title, books.price + FROM ( + SELECT me.name, me.id + FROM owners me + LIMIT ? + OFFSET ? + ) me + LEFT JOIN books books + ON books.owner = me.id + )', + [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ], + 'Expected SQL on complex limited prefetch with non-selected join condition', + ); + + is_deeply ( + $pref_rs->all_hri, + [ { + name => "Waltham", + books => [ { + id => 3, + owner => 2, + price => 65, + source => "Library", + title => "Best Recipe Cookbook", + } ], + } ], + 'Expected result on complex limited prefetch with non-selected join condition' + ); + + my $empty_ordered_pref_rs = $pref_rs->search({}, { + columns => [], # nothing, we only prefetch the book data + order_by => 'me.name', + }); + my $empty_ordered_pref_hri = [ { + books => [ { + id => 3, + owner => 2, + price => 65, + source => "Library", + title => "Best Recipe Cookbook", + } ], + } ]; + + is_same_sql_bind( + $empty_ordered_pref_rs->as_query, + '( + SELECT books.id, books.source, books.owner, books.title, books.price + FROM ( + SELECT me.id, me.name + FROM owners me + ORDER BY me.name + LIMIT ? + OFFSET ? + ) me + LEFT JOIN books books + ON books.owner = me.id + ORDER BY me.name + )', + [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ], + 'Expected SQL on *ordered* complex limited prefetch with non-selected root data', + ); + + is_deeply ( + $empty_ordered_pref_rs->all_hri, + $empty_ordered_pref_hri, + 'Expected result on *ordered* complex limited prefetch with non-selected root data' + ); + + $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, { + order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ], + }); + + is_same_sql_bind( + $empty_ordered_pref_rs->as_query, + '( + SELECT books.id, books.source, books.owner, books.title, books.price + FROM ( + SELECT me.id, me.name + FROM owners me + ORDER BY LENGTH(me.name), RANDOM() + LIMIT ? + OFFSET ? + ) me + LEFT JOIN books books + ON books.owner = me.id + ORDER BY LENGTH(me.name), RANDOM() + )', + [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ], + 'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data', + ); + + is_deeply ( + $empty_ordered_pref_rs->all_hri, + $empty_ordered_pref_hri, + 'Expected result on *function-ordered* complex limited prefetch with non-selected root data' + ); }