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