X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fcount%2Fcount_rs.t;h=a5e4098df005e728f4e7dfbdb54579051f78992d;hb=00336453444021a6422e05539ff4fc86c703a6b6;hp=0f2a1a0bc036f63c98aec77e9be52f6faca7b396;hpb=539ffe8768e85b2061aa3bb3616da4f848a582f3;p=dbsrgits%2FDBIx-Class.git diff --git a/t/count/count_rs.t b/t/count/count_rs.t index 0f2a1a0..a5e4098 100644 --- a/t/count/count_rs.t +++ b/t/count/count_rs.t @@ -8,8 +8,6 @@ use DBICTest; use DBIC::SqlMakerTest; use DBIC::DebugObj; -plan tests => 10; - my $schema = DBICTest->init_schema(); # non-collapsing prefetch (no multi prefetches) @@ -54,7 +52,7 @@ my $schema = DBICTest->init_schema(); JOIN cd disc ON disc.cdid = tracks.cd WHERE ( ( position = ? OR position = ? ) ) LIMIT 3 OFFSET 8 - ) count_subq + ) tracks )', [ [ position => 1 ], [ position => 2 ] ], 'count_rs db-side limit applied', @@ -88,7 +86,7 @@ my $schema = DBICTest->init_schema(); JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid - ) count_subq + ) cds ', [ qw/'1' '2'/ ], 'count softlimit applied', @@ -109,9 +107,44 @@ my $schema = DBICTest->init_schema(); WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid LIMIT 3 OFFSET 4 - ) count_subq + ) cds )', [ [ 'tracks.position' => 1 ], [ 'tracks.position' => 2 ] ], 'count_rs db-side limit applied', ); } + +# count with a having clause +{ + my $rs = $schema->resultset("Artist")->search( + {}, + { + join => 'cds', + group_by => 'me.artistid', + '+select' => [ { max => 'cds.year', -as => 'newest_cd_year' } ], + '+as' => ['newest_cd_year'], + having => { 'newest_cd_year' => '2001' } + } + ); + + my $crs = $rs->count_rs; + + is_same_sql_bind ( + $crs->as_query, + '(SELECT COUNT( * ) + FROM ( + SELECT MAX( cds.year ) AS newest_cd_year, me.artistid + FROM artist me + LEFT JOIN cd cds ON cds.artist = me.artistid + GROUP BY me.artistid + HAVING newest_cd_year = ? + ) me + )', + [ [ 'newest_cd_year' => '2001' ],], + 'count with having clause keeps sql as alias', + ); + + is ($crs->next, 2, 'Correct artist count (each with one 2001 cd)'); +} + +done_testing;