Final count tests
[dbsrgits/DBIx-Class.git] / t / count / count_distinct.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5
6 use lib qw(t/lib);
7
8 use DBICTest;
9 use DBIC::SqlMakerTest;
10
11 my $schema = DBICTest->init_schema();
12
13 eval "use DBD::SQLite";
14 plan skip_all => 'needs DBD::SQLite for testing' if $@;
15 plan tests => 16;
16
17 # The tag Blue is assigned to cds 1 2 3 and 5
18 # The tag Cheesy is assigned to cds 2 4 and 5
19 #
20 # This combination should make some interesting group_by's
21 #
22 my $rs;
23 my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
24
25 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
26 is($rs->count, 4, 'Count without DISTINCT');
27
28 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
29 is($rs->count, 2, 'Count with single column group_by');
30
31 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
32 is($rs->count, 5, 'Count with another single column group_by');
33
34 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
35 is($rs->count, 4, 'Count with multiple column group_by');
36
37 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
38 is($rs->count, 4, 'Count with single column distinct');
39
40 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
41 is($rs->count, 7, 'Count with IN subquery');
42
43 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
44 is($rs->count, 2, 'Count with IN subquery with outside group_by');
45
46 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
47 is($rs->count, 7, 'Count with IN subquery with outside distinct');
48
49 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }), 
50 is($rs->count, 2, 'Count with IN subquery with outside distinct on a single column');
51
52 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
53 is($rs->count, 7, 'Count with IN subquery with single group_by');
54
55 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'cd' })->get_column('tag')->as_query } });
56 is($rs->count, 7, 'Count with IN subquery with another single group_by');
57
58 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
59 is($rs->count, 7, 'Count with IN subquery with multiple group_by');
60
61 $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
62 is($rs->count, 4, 'Count without DISTINCT, using literal SQL');
63
64 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
65 is($rs->count, 2, 'Count with literal SQL and single group_by');
66
67 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
68 is($rs->count, 5, 'Count with literal SQL and another single group_by');
69
70 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] });
71 is($rs->count, 7, 'Count with literal SQL and multiple group_by');