Sybase bindvar and IC::DT support
[dbsrgits/DBIx-Class.git] / t / count / distinct.t
CommitLineData
2a2ca43f 1use strict;
2use warnings;
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
14eval "use DBD::SQLite";
15plan skip_all => 'needs DBD::SQLite for testing' if $@;
0814e804 16plan tests => 22;
0027911c 17
2d4f6f0b 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#
866557f9 23my $rs;
2d4f6f0b 24my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
2a2ca43f 25
866557f9 26$rs = $schema->resultset('Tag')->search({ tag => 'Blue' });
27is($rs->count, 4, 'Count without DISTINCT');
2a2ca43f 28
2d4f6f0b 29$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
866557f9 30is($rs->count, 2, 'Count with single column group_by');
2a2ca43f 31
2d4f6f0b 32$rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' });
33is($rs->count, 5, 'Count with another single column group_by');
34
866557f9 35$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]});
36is($rs->count, 4, 'Count with multiple column group_by');
2a2ca43f 37
866557f9 38$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
39is($rs->count, 4, 'Count with single column distinct');
0027911c 40
866557f9 41$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } });
2d4f6f0b 42is($rs->count, 7, 'Count with IN subquery');
0027911c 43
866557f9 44$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' });
5a825e67 45is($rs->count, 2, 'Count with IN subquery with outside group_by');
0027911c 46
866557f9 47$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 });
2d4f6f0b 48is($rs->count, 7, 'Count with IN subquery with outside distinct');
0027911c 49
866557f9 50$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }),
5a825e67 51is($rs->count, 2, 'Count with IN subquery with outside distinct on a single column');
0027911c 52
866557f9 53$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } });
2d4f6f0b 54is($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 } });
57is($rs->count, 7, 'Count with IN subquery with another single group_by');
0027911c 58
866557f9 59$rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } });
2d4f6f0b 60is($rs->count, 7, 'Count with IN subquery with multiple group_by');
0027911c 61
866557f9 62$rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
63is($rs->count, 4, 'Count without DISTINCT, using literal SQL');
0027911c 64
2d4f6f0b 65$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' });
866557f9 66is($rs->count, 2, 'Count with literal SQL and single group_by');
0027911c 67
2d4f6f0b 68$rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' });
69is($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/ ] });
72is($rs->count, 7, 'Count with literal SQL and multiple group_by');
7655b3c9 73
040fc6ba 74$rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 });
75is($rs->count, 4, 'Count with +select aggreggate');
909a20df 76
0814e804 77$rs = $schema->resultset('Tag')->search({}, { select => 'length(me.tag)', distinct => 1 });
78is($rs->count, 3, 'Count by distinct function result as select literal');
79
7655b3c9 80my @warnings;
81{
82 local $SIG{__WARN__} = sub { push @warnings, shift };
83 my $row = $schema->resultset('Tag')->search({}, { select => { distinct => 'tag' } })->first;
84}
85
86is(@warnings, 1, 'expecteing warn');
87
88dies_ok(sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first }, 'expecting to die');
040fc6ba 89dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');
90dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { select => { length => 'tag' }, distinct => 1 })->count }, 'expecting to die');