X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fcount%2Fdistinct.t;h=1b44b9a5f05c11555b0b892135dd36040c0182e1;hb=11343b34c7d3278f087f81d9bb281512d55b34c9;hp=cd10793d2e7904e8fcfaf722ea7246685a20903a;hpb=4b08a8b982ab4e7e7dde8f10633c558b11343b8f;p=dbsrgits%2FDBIx-Class.git diff --git a/t/count/distinct.t b/t/count/distinct.t index cd10793..1b44b9a 100644 --- a/t/count/distinct.t +++ b/t/count/distinct.t @@ -1,5 +1,5 @@ use strict; -use warnings; +use warnings; use Test::More; use Test::Exception; @@ -47,7 +47,7 @@ for my $get_count ( $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 }); is($get_count->($rs), 7, 'Count with IN subquery with outside distinct'); - $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }), + $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }), is($get_count->($rs), 2, 'Count with IN subquery with outside distinct on a single column'); $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } }); @@ -80,7 +80,7 @@ for my $get_count ( throws_ok( sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first }, - qr/select => { distinct => \.\.\. } syntax is not supported for multiple columns/, + qr/\Qselect => { distinct => ... } syntax is not supported for multiple columns/, 'throw on unsupported syntax' ); @@ -111,6 +111,64 @@ throws_ok( is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly'); } +# and check distinct has_many join count +{ + my $rs = $schema->resultset('Artist')->search( + { 'cds.title' => { '!=', 'fooooo' } }, + { + join => 'cds', + distinct => 1, + '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ], + '+as' => [qw/num_cds/], + order_by => { -desc => 'amount_of_cds' }, + } + ); + + is_same_sql_bind ( + $rs->as_query, + '( + SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds + FROM artist me + LEFT JOIN cd cds + ON cds.artist = me.artistid + WHERE cds.title != ? + GROUP BY me.artistid, me.name, me.rank, me.charfield + ORDER BY amount_of_cds DESC + )', + [ + [{ + sqlt_datatype => 'varchar', + dbic_colname => 'cds.title', + sqlt_size => 100, + } => 'fooooo' ], + ], + ); + + is_same_sql_bind ( + $rs->count_rs->as_query, + '( + SELECT COUNT( * ) + FROM ( + SELECT me.artistid, me.name, me.rank, me.charfield + FROM artist me + LEFT JOIN cd cds + ON cds.artist = me.artistid + WHERE cds.title != ? + GROUP BY me.artistid, me.name, me.rank, me.charfield + ) me + )', + [ + [{ + sqlt_datatype => 'varchar', + dbic_colname => 'cds.title', + sqlt_size => 100, + } => 'fooooo' ], + ], + ); + + is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly'); +} + # These two rely on the database to throw an exception. This might not be the case one day. Please revise. dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');