Add joined count test
[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
17my $in_rs = $schema->resultset("Tag")->search({ tag => [ 'Blue', 'Shiny' ] });
2a2ca43f 18
19cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }),
0027911c 20 '==', 4, 'Count without DISTINCT');
2a2ca43f 21
22cmp_ok($schema->resultset("Tag")->count({ tag => [ 'Blue', 'Shiny' ] }, { group_by => 'tag' }),
0027911c 23 '==', 2, 'Count with single column group_by');
2a2ca43f 24
25cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]}),
0027911c 26 '==', 4, 'Count with multiple column group_by');
2a2ca43f 27
28cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }, { distinct => 1 }),
0027911c 29 '==', 4, "Count with single column distinct");
30
31cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }),
32 '==', 4, "Count with IN subquery");
33
34cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' }),
35 '==', 1, "Count with IN subquery with outside group_by");
36
37cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 }),
38 '==', 4, "Count with IN subquery with outside distinct");
39
40cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
41 '==', 1, "Count with IN subquery with outside distinct on a single column");
42
43cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } }),
44 '==', 4, "Count with IN subquery with single group_by");
45
46cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } }),
47 '==', 4, "Count with IN subquery with multiple group_by");
48
49cmp_ok($schema->resultset("Tag")->count({ tag => \"= 'Blue'" }),
50 '==', 4, "Count without DISTINCT, using literal SQL");
51
52cmp_ok($schema->resultset("Tag")->count({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => 'tag' }),
53 '==', 2, "Count with literal SQL and single group_by");
54
55cmp_ok($schema->resultset("Tag")->count({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => [ qw/tag cd/ ] }),
56 '==', 6, "Count with literal SQL and multiple group_by");