X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fprefetch%2Fgrouped.t;h=af9b1f071ff2c8f1f0dfeb9e843e877c34d51597;hb=3fc4b39ed9cb03c5397a6581a77bfcbcd450470c;hp=077cba63919c51feb3e7556598ec2898ccd802b1;hpb=5b45001f9e0297da60a1ac4caa5a100ad6f374d3;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/prefetch/grouped.t b/t/prefetch/grouped.t index 077cba6..af9b1f0 100644 --- a/t/prefetch/grouped.t +++ b/t/prefetch/grouped.t @@ -1,15 +1,15 @@ use strict; use warnings; + use Test::More; +use Test::Exception; use lib qw(t/lib); use DBICTest; use DBIC::SqlMakerTest; -#plan tests => 6; -plan 'no_plan'; - my $schema = DBICTest->init_schema(); +my $sdebug = $schema->storage->debug; my $cd_rs = $schema->resultset('CD')->search ( { 'tracks.cd' => { '!=', undef } }, @@ -27,7 +27,6 @@ for ($cd_rs->all) { my $track_rs = $schema->resultset ('Track')->search ( { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } }, { - # the select/as is deliberately silly to test both funcs and refs below select => [ 'me.cd', { count => 'me.trackid' }, @@ -41,30 +40,31 @@ for ($cd_rs->all) { }, ); + # this used to fuck up ->all, do not remove! + ok ($track_rs->first, 'There is stuff in the rs'); + is($track_rs->count, 5, 'Prefetched count with groupby'); is($track_rs->all, 5, 'Prefetched objects with groupby'); { my $query_cnt = 0; $schema->storage->debugcb ( sub { $query_cnt++ } ); + $schema->storage->debug (1); - $track_rs->reset; while (my $collapsed_track = $track_rs->next) { - my $cdid = $collapsed_track->get_column('cd'); is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" ); ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" ); } - is ($query_cnt, 0, 'No queries on prefetched titles'); + is ($query_cnt, 1, 'Single query on prefetched titles'); $schema->storage->debugcb (undef); + $schema->storage->debug ($sdebug); } # Test sql by hand, as the sqlite db will simply paper over # improper group/select combinations # - # the exploded IN needs fixing below, coming in another branch - # is_same_sql_bind ( $track_rs->count_rs->as_query, '( @@ -73,7 +73,7 @@ for ($cd_rs->all) { SELECT me.cd FROM track me JOIN cd cd ON cd.cdid = me.cd - WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ) + WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) GROUP BY me.cd ) count_subq @@ -82,7 +82,6 @@ for ($cd_rs->all) { 'count() query generated expected SQL', ); - # the double-count is deliberate is_same_sql_bind ( $track_rs->as_query, '( @@ -90,13 +89,14 @@ for ($cd_rs->all) { FROM ( SELECT me.cd, COUNT (me.trackid) AS track_count, FROM track me - WHERE ( cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ) + JOIN cd cd ON cd.cdid = me.cd + WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) GROUP BY me.cd ) as me JOIN cd cd ON cd.cdid = me.cd - WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ) + WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) )', - [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ], + [ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ], 'next() query generated expected SQL', ); @@ -125,18 +125,21 @@ for ($cd_rs->all) { } # test a has_many/might_have prefetch at the same level -# double-count for now, until we figure out how to do this cleanly -# with a function # Note that one of the CDs now has 4 tracks instead of 3 { - my $most_tracks_rs = $cd_rs->search ({}, { - prefetch => 'liner_notes', # tracks are alredy prefetched - select => ['me.cdid', { count => 'tracks.trackid' } ], - as => [qw/cdid track_count/], - group_by => 'me.cdid', - order_by => { -desc => 'track_count' }, - rows => 2, - }); + my $most_tracks_rs = $schema->resultset ('CD')->search ( + { + 'me.cdid' => { '!=' => undef }, # duh - this is just to test WHERE + }, + { + prefetch => [qw/tracks liner_notes/], + select => ['me.cdid', { count => 'tracks.trackid' } ], + as => [qw/cdid track_count/], + group_by => 'me.cdid', + order_by => { -desc => 'track_count' }, + rows => 2, + } + ); is_same_sql_bind ( $most_tracks_rs->count_rs->as_query, @@ -147,7 +150,7 @@ for ($cd_rs->all) { FROM cd me LEFT JOIN track tracks ON tracks.cd = me.cdid LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid - WHERE ( tracks.cd IS NOT NULL ) + WHERE ( me.cdid IS NOT NULL ) GROUP BY me.cdid LIMIT 2 ) count_subq @@ -159,20 +162,20 @@ for ($cd_rs->all) { is_same_sql_bind ( $most_tracks_rs->as_query, '( - SELECT me.cdid, track_count, tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, liner_notes.liner_id, liner_notes.notes + SELECT me.cdid, me.track_count, tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, liner_notes.liner_id, liner_notes.notes FROM ( SELECT me.cdid, COUNT( tracks.trackid ) AS track_count FROM cd me LEFT JOIN track tracks ON tracks.cd = me.cdid - WHERE ( tracks.cd IS NOT NULL ) + WHERE ( me.cdid IS NOT NULL ) GROUP BY me.cdid - ORDER BY track_count DESC, + ORDER BY track_count DESC LIMIT 2 ) me LEFT JOIN track tracks ON tracks.cd = me.cdid LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid - WHERE ( tracks.cd IS NOT NULL ) - ORDER BY COUNT track_count DESC, tracks.cd + WHERE ( me.cdid IS NOT NULL ) + ORDER BY track_count DESC, tracks.cd )', [], 'next() query generated expected SQL', @@ -180,10 +183,11 @@ for ($cd_rs->all) { is ($most_tracks_rs->count, 2, 'Limit works'); my $top_cd = $most_tracks_rs->first; - is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) above + is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier my $query_cnt = 0; $schema->storage->debugcb ( sub { $query_cnt++ } ); + $schema->storage->debug (1); is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly'); is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct'); @@ -196,4 +200,75 @@ for ($cd_rs->all) { is ($query_cnt, 0, 'No queries executed during prefetched data access'); $schema->storage->debugcb (undef); + $schema->storage->debug ($sdebug); } + +# make sure that distinct still works +{ + my $rs = $schema->resultset("CD")->search({}, { + prefetch => 'tags', + order_by => 'cdid', + distinct => 1, + }); + + is_same_sql_bind ( + $rs->as_query, + '( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, + tags.tagid, tags.cd, tags.tag + FROM ( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track + FROM cd me + GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track + ORDER BY cdid + ) me + LEFT JOIN tags tags ON tags.cd = me.cdid + ORDER BY cdid, tags.cd, tags.tag + )', + [], + 'Prefetch + distinct resulted in correct group_by', + ); + + is ($rs->all, 5, 'Correct number of CD objects'); + is ($rs->count, 5, 'Correct count of CDs'); +} + +lives_ok (sub { + my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1}) + ->search_related('artwork_to_artist')->search_related('artist', + undef, + { prefetch => q(cds) }, + ); + is($rs->all, 0, 'prefetch without WHERE'); + + $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1}) + ->search_related('artwork_to_artist')->search_related('artist', + { 'cds.title' => 'foo' }, + { prefetch => q(cds) }, + ); + is($rs->all, 0, 'prefetch with WHERE'); + + + # different case + $rs = $schema->resultset("Artist")->search(undef) + ->search_related('cds')->search_related('genre', + { 'genre.name' => 'foo' }, + { prefetch => q(cds) }, + ); + is($rs->all, 0, 'prefetch without distinct'); + + $rs = $schema->resultset("Artist")->search(undef, {distinct => 1}) + ->search_related('cds')->search_related('genre', + { 'genre.name' => 'foo' }, + ); + is($rs->all, 0, 'distinct without prefetch'); + + $rs = $schema->resultset("Artist")->search({artistid => '11'}, {distinct => 1}) + ->search_related('cds')->search_related('genre', + { 'genre.name' => 'foo' }, + { prefetch => q(cds) }, + ); + is($rs->all, 0, 'prefetch with distinct'); +}, 'distinct generally works with prefetch'); + +done_testing;