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