6df9ed0cb178d38fc3290920625bd7d90c3a5298
[dbsrgits/DBIx-Class.git] / t / 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 plan tests => 56;
15
16 # The tag Blue is assigned to cds 1 2 3 and 5
17 # The tag Cheesy is assigned to cds 2 4 and 5
18 #
19 # This combination should make some interesting group_by's
20 #
21 my $rs;
22 my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
23
24 for my $get_count (
25   sub { shift->count },
26   sub { my $crs = shift->count_rs; isa_ok ($crs, 'DBIx::Class::ResultSetColumn'); $crs->next }
27 ) {
28   $rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
29   is($get_count->($rs), 4, 'Count without DISTINCT');
30
31   $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
32   is($get_count->($rs), 2, 'Count with single column group_by');
33
34   $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
35   is($get_count->($rs), 5, 'Count with another single column group_by');
36
37   $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
38   is($get_count->($rs), 4, 'Count with multiple column group_by');
39
40   $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
41   is($get_count->($rs), 4, 'Count with single column distinct');
42
43   $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
44   is($get_count->($rs), 7, 'Count with IN subquery');
45
46   $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
47   is($get_count->($rs), 2, 'Count with IN subquery with outside group_by');
48
49   $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
50   is($get_count->($rs), 7, 'Count with IN subquery with outside distinct');
51
52   $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }), 
53   is($get_count->($rs), 2, 'Count with IN subquery with outside distinct on a single column');
54
55   $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
56   is($get_count->($rs), 7, 'Count with IN subquery with single group_by');
57
58   $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'cd' })->get_column('tag')->as_query } });
59   is($get_count->($rs), 7, 'Count with IN subquery with another single group_by');
60
61   $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
62   is($get_count->($rs), 7, 'Count with IN subquery with multiple group_by');
63
64   $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
65   is($get_count->($rs), 4, 'Count without DISTINCT, using literal SQL');
66
67   $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
68   is($get_count->($rs), 2, 'Count with literal SQL and single group_by');
69
70   $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
71   is($get_count->($rs), 5, 'Count with literal SQL and another single group_by');
72
73   $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] });
74   is($get_count->($rs), 7, 'Count with literal SQL and multiple group_by');
75
76   $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 });
77   is($get_count->($rs), 4, 'Count with +select aggreggate');
78
79   $rs = $schema->resultset('Tag')->search({}, { select => 'length(me.tag)', distinct => 1 });
80   is($get_count->($rs), 3, 'Count by distinct function result as select literal');
81 }
82
83 throws_ok(
84   sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first },
85   qr/select => { distinct => \.\.\. } syntax is not supported for multiple columns/,
86   'throw on unsupported syntax'
87 );
88
89 # These two rely on the database to throw an exception. This might not be the case one day. Please revise.
90 dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');