From: Peter Rabbitson Date: Sat, 20 Jun 2009 10:54:11 +0000 (+0000) Subject: Test count_rs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0d2035db29e32f333216c24cfc44c3631f64b7ff;p=dbsrgits%2FDBIx-Class-Historic.git Test count_rs --- diff --git a/t/count/distinct.t b/t/count/distinct.t index 8e956b5..00ee411 100644 --- a/t/count/distinct.t +++ b/t/count/distinct.t @@ -11,9 +11,7 @@ use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); -eval "use DBD::SQLite"; -plan skip_all => 'needs DBD::SQLite for testing' if $@; -plan tests => 22; +plan tests => 58; # The tag Blue is assigned to cds 1 2 3 and 5 # The tag Cheesy is assigned to cds 2 4 and 5 @@ -23,59 +21,64 @@ 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'); +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 => 'tag' }); -is($rs->count, 2, 'Count with 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', '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 => 'cd' }); + is($get_count->($rs), 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'); + $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 => 'Blue' }, { distinct => 1 }); -is($rs->count, 4, 'Count with single column distinct'); + $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 } }); -is($rs->count, 7, 'Count with IN subquery'); + $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 } }, { 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 } }, { 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 }); -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 }); + 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' }), -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, 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 } }); -is($rs->count, 7, 'Count with IN subquery with 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 => '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 => '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 => { -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 => [ 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 => \"= 'Blue'" }); -is($rs->count, 4, 'Count without DISTINCT, using literal SQL'); + $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 => 'tag' }); -is($rs->count, 2, 'Count with literal SQL and 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 => '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 => 'cd' }); + is($get_count->($rs), 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'); + $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({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 }); -is($rs->count, 4, 'Count with +select aggreggate'); + $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 }); + is($get_count->($rs), 4, 'Count with +select aggreggate'); -$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({}, { select => 'length(me.tag)', distinct => 1 }); + is($get_count->($rs), 3, 'Count by distinct function result as select literal'); +} eval { my @warnings; @@ -90,7 +93,7 @@ ok ($@, 'Exception on deprecated distinct usage thrown') if $DBIx::Class::VERSIO 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/select => { distinct => \.\.\. } syntax is not supported for multiple columns/, 'throw on unsupported syntax' );