Merge 'trunk' into 'count_distinct'
[dbsrgits/DBIx-Class.git] / t / count / count_distinct.t
CommitLineData
2a2ca43f 1use strict;
2use warnings;
3
4use Test::More;
5
6use lib qw(t/lib);
7
8use DBICTest;
9use DBIC::SqlMakerTest;
10
11my $schema = DBICTest->init_schema();
12
13eval "use DBD::SQLite";
14plan skip_all => 'needs DBD::SQLite for testing' if $@;
0027911c 15plan tests => 13;
16
866557f9 17my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Shiny' ] });
18my $rs;
2a2ca43f 19
866557f9 20$rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
21is($rs->count, 4, 'Count without DISTINCT');
2a2ca43f 22
866557f9 23$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Shiny' ] }, { group_by => 'tag' });
24is($rs->count, 2, 'Count with single column group_by');
2a2ca43f 25
866557f9 26$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
27is($rs->count, 4, 'Count with multiple column group_by');
2a2ca43f 28
866557f9 29$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
30is($rs->count, 4, 'Count with single column distinct');
0027911c 31
866557f9 32$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
33is($rs->count, 4, 'Count with IN subquery');
0027911c 34
866557f9 35$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
36is($rs->count, 1, 'Count with IN subquery with outside group_by');
0027911c 37
866557f9 38$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
39is($rs->count, 4, 'Count with IN subquery with outside distinct');
0027911c 40
866557f9 41$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
42is($rs->count, 1, 'Count with IN subquery with outside distinct on a single column');
0027911c 43
866557f9 44$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
45is($rs->count, 4, 'Count with IN subquery with single group_by');
0027911c 46
866557f9 47$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
48is($rs->count, 4, 'Count with IN subquery with multiple group_by');
0027911c 49
866557f9 50$rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
51is($rs->count, 4, 'Count without DISTINCT, using literal SQL');
0027911c 52
866557f9 53$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => 'tag' });
54is($rs->count, 2, 'Count with literal SQL and single group_by');
0027911c 55
866557f9 56$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => [ qw/tag cd/ ] });
57is($rs->count, 6, 'Count with literal SQL and multiple group_by');