From: Peter Rabbitson Date: Fri, 8 May 2009 21:11:43 +0000 (+0000) Subject: Final count tests X-Git-Tag: v0.08103~101^2~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2d4f6f0ba3da42a157c581627a9a51dcfa573fc8;p=dbsrgits%2FDBIx-Class.git Final count tests --- diff --git a/t/count/count_distinct.t b/t/count/count_distinct.t index 97cb7f9..958ce6d 100644 --- a/t/count/count_distinct.t +++ b/t/count/count_distinct.t @@ -12,17 +12,25 @@ my $schema = DBICTest->init_schema(); eval "use DBD::SQLite"; plan skip_all => 'needs DBD::SQLite for testing' if $@; -plan tests => 13; +plan tests => 16; -my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Shiny' ] }); +# The tag Blue is assigned to cds 1 2 3 and 5 +# The tag Cheesy is assigned to cds 2 4 and 5 +# +# This combination should make some interesting group_by's +# 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', 'Shiny' ] }, { group_by => 'tag' }); +$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' }); is($rs->count, 2, 'Count with single column group_by'); +$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' }, { group_by => [ qw/tag cd/ ]}); is($rs->count, 4, 'Count with multiple column group_by'); @@ -30,34 +38,34 @@ $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 }); is($rs->count, 4, 'Count with single column distinct'); $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }); -#SELECT COUNT( * ) FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) ) ): 'Blue', 'Shiny' -is($rs->count, 6, 'Count with IN subquery'); +is($rs->count, 7, 'Count with IN subquery'); $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' }); -#SELECT COUNT( * ) FROM (SELECT tag FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) ) ) GROUP BY tag) mesub: 'Blue', 'Shiny' 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 } }, { distinct => 1 }); -#SELECT COUNT( * ) FROM (SELECT me.tagid, me.cd, me.tag FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) ) ) GROUP BY me.tagid, me.cd, me.tag) mesub: 'Blue', 'Shiny' -is($rs->count, 6, 'Count with IN subquery with outside distinct'); +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 } }, { distinct => 1, select => 'tag' }), -#SELECT COUNT( * ) FROM (SELECT tag FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) ) ) GROUP BY tag) mesub: 'Blue', 'Shiny' is($rs->count, 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 } }); -#SELECT COUNT( * ) FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) GROUP BY tag ) ): 'Blue', 'Shiny' -is($rs->count, 6, 'Count with IN subquery with single group_by'); +is($rs->count, 7, 'Count with IN subquery with single group_by'); + +$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 => [ qw/tag cd/ ] })->get_column('tag')->as_query } }); -#SELECT COUNT( * ) FROM tags me WHERE ( tag IN ( SELECT me.tag FROM tags me WHERE ( ( tag = ? OR tag = ? ) ) GROUP BY tag, cd ) ): 'Blue', 'Shiny' -is($rs->count, 6, 'Count with IN subquery with multiple group_by'); +is($rs->count, 7, 'Count with IN subquery with multiple 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 ('Blue', 'Shiny')" }, { group_by => 'tag' }); +$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 => \" IN ('Blue', 'Shiny')" }, { group_by => [ qw/tag cd/ ] }); -is($rs->count, 6, 'Count with literal SQL and multiple group_by'); +$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 => [ qw/tag cd/ ] }); +is($rs->count, 7, 'Count with literal SQL and multiple group_by');