X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fcount%2Fdistinct.t;h=1b44b9a5f05c11555b0b892135dd36040c0182e1;hb=906c03556a6c5584b3f2f18eb7501cc4c48704a1;hp=8e956b5d1c467c2c41c981900f5150b49bb7beae;hpb=04ebc6f4d9b90ad04258ef202c3879b509dd0b09;p=dbsrgits%2FDBIx-Class.git diff --git a/t/count/distinct.t b/t/count/distinct.t index 8e956b5..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; @@ -11,10 +11,6 @@ use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); -eval "use DBD::SQLite"; -plan skip_all => 'needs DBD::SQLite for testing' if $@; -plan tests => 22; - # The tag Blue is assigned to cds 1 2 3 and 5 # The tag Cheesy is assigned to cds 2 4 and 5 # @@ -23,77 +19,157 @@ plan tests => 22; my $rs; my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }); -$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }); -is($rs->count, 4, 'Count without DISTINCT'); - -$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' }); -is($rs->count, 2, 'Count with single column group_by'); +for my $get_count ( + sub { shift->count }, + sub { my $crs = shift->count_rs; isa_ok ($crs, 'DBIx::Class::ResultSetColumn'); $crs->next } +) { + $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }); + is($get_count->($rs), 4, 'Count without DISTINCT'); -$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' }); -is($rs->count, 5, 'Count with another single column group_by'); + $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' }); + is($get_count->($rs), 2, 'Count with single column group_by'); -$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]}); -is($rs->count, 4, 'Count with multiple column group_by'); + $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' }); + is($get_count->($rs), 5, 'Count with another single column group_by'); -$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 }); -is($rs->count, 4, 'Count with single column distinct'); + $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]}); + is($get_count->($rs), 4, 'Count with multiple column group_by'); -$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }); -is($rs->count, 7, 'Count with IN subquery'); + $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 }); + is($get_count->($rs), 4, 'Count with single column distinct'); -$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' }); -is($rs->count, 2, 'Count with IN subquery with outside group_by'); + $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }); + is($get_count->($rs), 7, 'Count with IN subquery'); -$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 }); -is($rs->count, 7, 'Count with IN subquery with outside distinct'); + $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' }); + is($get_count->($rs), 2, 'Count with IN subquery with outside group_by'); -$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }), -is($rs->count, 2, 'Count with IN subquery with outside distinct on a single column'); + $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->search({}, { group_by => 'tag' })->get_column('tag')->as_query } }); -is($rs->count, 7, 'Count with IN subquery with single group_by'); + $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 => 'cd' })->get_column('tag')->as_query } }); -is($rs->count, 7, 'Count with IN subquery with another single group_by'); + $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } }); + is($get_count->($rs), 7, 'Count with IN subquery with single group_by'); -$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } }); -is($rs->count, 7, 'Count with IN subquery with multiple group_by'); + $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'cd' })->get_column('tag')->as_query } }); + is($get_count->($rs), 7, 'Count with IN subquery with another single group_by'); -$rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" }); -is($rs->count, 4, 'Count without DISTINCT, using literal SQL'); + $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } }); + is($get_count->($rs), 7, 'Count with IN subquery with multiple group_by'); -$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' }); -is($rs->count, 2, 'Count with literal SQL and single group_by'); + $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" }); + is($get_count->($rs), 4, 'Count without DISTINCT, using literal SQL'); -$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' }); -is($rs->count, 5, 'Count with literal SQL and another single group_by'); + $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' }); + is($get_count->($rs), 2, 'Count with literal SQL and single group_by'); -$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] }); -is($rs->count, 7, 'Count with literal SQL and multiple group_by'); + $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' }); + is($get_count->($rs), 5, 'Count with literal SQL and another single group_by'); -$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 }); -is($rs->count, 4, 'Count with +select aggreggate'); + $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] }); + is($get_count->($rs), 7, 'Count with literal SQL and multiple group_by'); -$rs = $schema->resultset('Tag')->search({}, { select => 'length(me.tag)', distinct => 1 }); -is($rs->count, 3, 'Count by distinct function result as select literal'); + $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 }); + is($get_count->($rs), 4, 'Count with +select aggreggate'); -eval { - my @warnings; - local $SIG{__WARN__} = sub { $_[0] =~ /The select => { distinct => ... } syntax will be deprecated/ - ? push @warnings, @_ - : warn @_ - }; - my $row = $schema->resultset('Tag')->search({}, { select => { distinct => 'tag' } })->first; - is (@warnings, 1, 'Warned about deprecated distinct') if $DBIx::Class::VERSION < 0.09; -}; -ok ($@, 'Exception on deprecated distinct usage thrown') if $DBIx::Class::VERSION >= 0.09; + $rs = $schema->resultset('Tag')->search({}, { select => [\'length(me.tag)'], distinct => 1 }); + is($get_count->($rs), 3, 'Count by distinct function result as select literal'); +} 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' ); +# make sure distinct+func works +{ + my $rs = $schema->resultset('Artist')->search( + {}, + { + 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 + GROUP BY me.artistid, me.name, me.rank, me.charfield + ORDER BY amount_of_cds DESC + )', + [], + ); + + 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'); -dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { select => { length => 'tag' }, distinct => 1 })->count }, 'expecting to die'); + +done_testing;