Stop using deprecated regex syntax
[dbsrgits/DBIx-Class.git] / t / count / distinct.t
CommitLineData
2a2ca43f 1use strict;
8273e845 2use warnings;
2a2ca43f 3
4use Test::More;
7655b3c9 5use Test::Exception;
2a2ca43f 6
7use lib qw(t/lib);
8
9use DBICTest;
10use DBIC::SqlMakerTest;
11
12my $schema = DBICTest->init_schema();
13
2d4f6f0b 14# The tag Blue is assigned to cds 1 2 3 and 5
15# The tag Cheesy is assigned to cds 2 4 and 5
16#
17# This combination should make some interesting group_by's
18#
866557f9 19my $rs;
2d4f6f0b 20my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
2a2ca43f 21
0d2035db 22for my $get_count (
23 sub { shift->count },
24 sub { my $crs = shift->count_rs; isa_ok ($crs, 'DBIx::Class::ResultSetColumn'); $crs->next }
25) {
26 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
27 is($get_count->($rs), 4, 'Count without DISTINCT');
2a2ca43f 28
0d2035db 29 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
30 is($get_count->($rs), 2, 'Count with single column group_by');
2a2ca43f 31
0d2035db 32 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
33 is($get_count->($rs), 5, 'Count with another single column group_by');
2d4f6f0b 34
0d2035db 35 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
36 is($get_count->($rs), 4, 'Count with multiple column group_by');
2a2ca43f 37
0d2035db 38 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
39 is($get_count->($rs), 4, 'Count with single column distinct');
0027911c 40
0d2035db 41 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
42 is($get_count->($rs), 7, 'Count with IN subquery');
0027911c 43
0d2035db 44 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
45 is($get_count->($rs), 2, 'Count with IN subquery with outside group_by');
0027911c 46
0d2035db 47 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
48 is($get_count->($rs), 7, 'Count with IN subquery with outside distinct');
0027911c 49
8273e845 50 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
0d2035db 51 is($get_count->($rs), 2, 'Count with IN subquery with outside distinct on a single column');
0027911c 52
0d2035db 53 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
54 is($get_count->($rs), 7, 'Count with IN subquery with single group_by');
2d4f6f0b 55
0d2035db 56 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'cd' })->get_column('tag')->as_query } });
57 is($get_count->($rs), 7, 'Count with IN subquery with another single group_by');
0027911c 58
0d2035db 59 $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
60 is($get_count->($rs), 7, 'Count with IN subquery with multiple group_by');
0027911c 61
0d2035db 62 $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
63 is($get_count->($rs), 4, 'Count without DISTINCT, using literal SQL');
0027911c 64
0d2035db 65 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
66 is($get_count->($rs), 2, 'Count with literal SQL and single group_by');
0027911c 67
0d2035db 68 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
69 is($get_count->($rs), 5, 'Count with literal SQL and another single group_by');
2d4f6f0b 70
0d2035db 71 $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => [ qw/tag cd/ ] });
72 is($get_count->($rs), 7, 'Count with literal SQL and multiple group_by');
7655b3c9 73
0d2035db 74 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 });
75 is($get_count->($rs), 4, 'Count with +select aggreggate');
909a20df 76
4b08a8b9 77 $rs = $schema->resultset('Tag')->search({}, { select => [\'length(me.tag)'], distinct => 1 });
0d2035db 78 is($get_count->($rs), 3, 'Count by distinct function result as select literal');
79}
0814e804 80
04ebc6f4 81throws_ok(
82 sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first },
df72bc73 83 qr/\Qselect => { distinct => ... } syntax is not supported for multiple columns/,
04ebc6f4 84 'throw on unsupported syntax'
85);
7655b3c9 86
324bc214 87# make sure distinct+func works
88{
89 my $rs = $schema->resultset('Artist')->search(
90 {},
91 {
92 join => 'cds',
93 distinct => 1,
94 '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ],
95 '+as' => [qw/num_cds/],
96 order_by => { -desc => 'amount_of_cds' },
97 }
98 );
99
100 is_same_sql_bind (
101 $rs->as_query,
102 '(
103 SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds
104 FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid
105 GROUP BY me.artistid, me.name, me.rank, me.charfield
106 ORDER BY amount_of_cds DESC
107 )',
108 [],
109 );
110
111 is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly');
112}
113
ff46b7b3 114# and check distinct has_many join count
115{
116 my $rs = $schema->resultset('Artist')->search(
117 { 'cds.title' => { '!=', 'fooooo' } },
118 {
119 join => 'cds',
120 distinct => 1,
121 '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ],
122 '+as' => [qw/num_cds/],
123 order_by => { -desc => 'amount_of_cds' },
124 }
125 );
126
127 is_same_sql_bind (
128 $rs->as_query,
129 '(
130 SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds
131 FROM artist me
132 LEFT JOIN cd cds
133 ON cds.artist = me.artistid
134 WHERE cds.title != ?
135 GROUP BY me.artistid, me.name, me.rank, me.charfield
136 ORDER BY amount_of_cds DESC
137 )',
138 [
139 [{
140 sqlt_datatype => 'varchar',
141 dbic_colname => 'cds.title',
142 sqlt_size => 100,
143 } => 'fooooo' ],
144 ],
145 );
146
147 is_same_sql_bind (
148 $rs->count_rs->as_query,
149 '(
150 SELECT COUNT( * )
151 FROM (
152 SELECT me.artistid, me.name, me.rank, me.charfield
153 FROM artist me
154 LEFT JOIN cd cds
155 ON cds.artist = me.artistid
156 WHERE cds.title != ?
157 GROUP BY me.artistid, me.name, me.rank, me.charfield
158 ) me
159 )',
160 [
161 [{
162 sqlt_datatype => 'varchar',
163 dbic_colname => 'cds.title',
164 sqlt_size => 100,
165 } => 'fooooo' ],
166 ],
167 );
168
169 is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly');
170}
171
04ebc6f4 172# These two rely on the database to throw an exception. This might not be the case one day. Please revise.
040fc6ba 173dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');
324bc214 174
175done_testing;