X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fcount%2Fdistinct.t;h=edd3d35d4c2d6875abf7627d8bf54a8f6888a303;hb=86be9bcb90213db633791fcce074b7268765f615;hp=1ef8ccfc8bddba6adba4513cf500ce2c88d928ec;hpb=8273e845426f0187b4ad6c4a1b42286fa09a648f;p=dbsrgits%2FDBIx-Class.git diff --git a/t/count/distinct.t b/t/count/distinct.t index 1ef8ccf..edd3d35 100644 --- a/t/count/distinct.t +++ b/t/count/distinct.t @@ -1,13 +1,12 @@ +BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } + use strict; use warnings; use Test::More; use Test::Exception; -use lib qw(t/lib); - -use DBICTest; -use DBIC::SqlMakerTest; +use DBICTest ':DiffSQL'; my $schema = DBICTest->init_schema(); @@ -80,7 +79,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 +110,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');