e916ab941eb30c5150d2abef130704754575a5e9
[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 ':DiffSQL';
10
11 my $schema = DBICTest->init_schema();
12
13 # The tag Blue is assigned to cds 1 2 3 and 5
14 # The tag Cheesy is assigned to cds 2 4 and 5
15 #
16 # This combination should make some interesting group_by's
17 #
18 my $rs;
19 my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
20
21 for my $get_count (
22   sub { shift->count },
23   sub { my $crs = shift->count_rs; isa_ok ($crs, 'DBIx::Class::ResultSetColumn'); $crs->next }
24 ) {
25   $rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
26   is($get_count->($rs), 4, 'Count without DISTINCT');
27
28   $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
29   is($get_count->($rs), 2, 'Count with single column group_by');
30
31   $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
32   is($get_count->($rs), 5, 'Count with another single column group_by');
33
34   $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
35   is($get_count->($rs), 4, 'Count with multiple column group_by');
36
37   $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
38   is($get_count->($rs), 4, 'Count with single column distinct');
39
40   $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
41   is($get_count->($rs), 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($get_count->($rs), 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($get_count->($rs), 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($get_count->($rs), 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($get_count->($rs), 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($get_count->($rs), 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($get_count->($rs), 7, 'Count with IN subquery with multiple group_by');
60
61   $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
62   is($get_count->($rs), 4, 'Count without DISTINCT, using literal SQL');
63
64   $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
65   is($get_count->($rs), 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($get_count->($rs), 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($get_count->($rs), 7, 'Count with literal SQL and multiple group_by');
72
73   $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 });
74   is($get_count->($rs), 4, 'Count with +select aggreggate');
75
76   $rs = $schema->resultset('Tag')->search({}, { select => [\'length(me.tag)'], distinct => 1 });
77   is($get_count->($rs), 3, 'Count by distinct function result as select literal');
78 }
79
80 throws_ok(
81   sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first },
82   qr/\Qselect => { distinct => ... } syntax is not supported for multiple columns/,
83   'throw on unsupported syntax'
84 );
85
86 # make sure distinct+func works
87 {
88   my $rs = $schema->resultset('Artist')->search(
89     {},
90     {
91       join => 'cds',
92       distinct => 1,
93       '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ],
94       '+as' => [qw/num_cds/],
95       order_by => { -desc => 'amount_of_cds' },
96     }
97   );
98
99   is_same_sql_bind (
100     $rs->as_query,
101     '(
102       SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds
103         FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid
104       GROUP BY me.artistid, me.name, me.rank, me.charfield
105       ORDER BY amount_of_cds DESC
106     )',
107     [],
108   );
109
110   is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly');
111 }
112
113 # and check distinct has_many join count
114 {
115   my $rs = $schema->resultset('Artist')->search(
116     { 'cds.title' => { '!=', 'fooooo' } },
117     {
118       join => 'cds',
119       distinct => 1,
120       '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ],
121       '+as' => [qw/num_cds/],
122       order_by => { -desc => 'amount_of_cds' },
123     }
124   );
125
126   is_same_sql_bind (
127     $rs->as_query,
128     '(
129       SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds
130         FROM artist me
131         LEFT JOIN cd cds
132           ON cds.artist = me.artistid
133       WHERE cds.title != ?
134       GROUP BY me.artistid, me.name, me.rank, me.charfield
135       ORDER BY amount_of_cds DESC
136     )',
137     [
138       [{
139         sqlt_datatype => 'varchar',
140         dbic_colname => 'cds.title',
141         sqlt_size => 100,
142       } => 'fooooo' ],
143     ],
144   );
145
146   is_same_sql_bind (
147     $rs->count_rs->as_query,
148     '(
149       SELECT COUNT( * )
150         FROM (
151           SELECT me.artistid, me.name, me.rank, me.charfield
152             FROM artist me
153             LEFT JOIN cd cds
154               ON cds.artist = me.artistid
155           WHERE cds.title != ?
156           GROUP BY me.artistid, me.name, me.rank, me.charfield
157         ) me
158     )',
159     [
160       [{
161         sqlt_datatype => 'varchar',
162         dbic_colname => 'cds.title',
163         sqlt_size => 100,
164       } => 'fooooo' ],
165     ],
166   );
167
168   is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly');
169 }
170
171 # These two rely on the database to throw an exception. This might not be the case one day. Please revise.
172 dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');
173
174 done_testing;