Final count tests
[dbsrgits/DBIx-Class-Historic.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 $@;
2d4f6f0b 15plan tests => 16;
0027911c 16
2d4f6f0b 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#
866557f9 22my $rs;
2d4f6f0b 23my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
2a2ca43f 24
866557f9 25$rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
26is($rs->count, 4, 'Count without DISTINCT');
2a2ca43f 27
2d4f6f0b 28$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
866557f9 29is($rs->count, 2, 'Count with single column group_by');
2a2ca43f 30
2d4f6f0b 31$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
32is($rs->count, 5, 'Count with another single column group_by');
33
866557f9 34$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
35is($rs->count, 4, 'Count with multiple column group_by');
2a2ca43f 36
866557f9 37$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
38is($rs->count, 4, 'Count with single column distinct');
0027911c 39
866557f9 40$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
2d4f6f0b 41is($rs->count, 7, 'Count with IN subquery');
0027911c 42
866557f9 43$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
5a825e67 44is($rs->count, 2, 'Count with IN subquery with outside group_by');
0027911c 45
866557f9 46$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
2d4f6f0b 47is($rs->count, 7, 'Count with IN subquery with outside distinct');
0027911c 48
866557f9 49$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
5a825e67 50is($rs->count, 2, 'Count with IN subquery with outside distinct on a single column');
0027911c 51
866557f9 52$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
2d4f6f0b 53is($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 } });
56is($rs->count, 7, 'Count with IN subquery with another single group_by');
0027911c 57
866557f9 58$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
2d4f6f0b 59is($rs->count, 7, 'Count with IN subquery with multiple group_by');
0027911c 60
866557f9 61$rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
62is($rs->count, 4, 'Count without DISTINCT, using literal SQL');
0027911c 63
2d4f6f0b 64$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
866557f9 65is($rs->count, 2, 'Count with literal SQL and single group_by');
0027911c 66
2d4f6f0b 67$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
68is($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/ ] });
71is($rs->count, 7, 'Count with literal SQL and multiple group_by');