Allow select AS specification for functions only via the -as hash-key (no pod yet)
[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
83e09b5b 14plan tests => 56;
0027911c 15
2d4f6f0b 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#
866557f9 21my $rs;
2d4f6f0b 22my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] });
2a2ca43f 23
0d2035db 24for 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');
2a2ca43f 30
0d2035db 31 $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' });
32 is($get_count->($rs), 2, 'Count with single column group_by');
2a2ca43f 33
0d2035db 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');
2d4f6f0b 36
0d2035db 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');
2a2ca43f 39
0d2035db 40 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 });
41 is($get_count->($rs), 4, 'Count with single column distinct');
0027911c 42
0d2035db 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');
0027911c 45
0d2035db 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');
0027911c 48
0d2035db 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');
0027911c 51
0d2035db 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');
0027911c 54
0d2035db 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');
2d4f6f0b 57
0d2035db 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');
0027911c 60
0d2035db 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');
0027911c 63
0d2035db 64 $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" });
65 is($get_count->($rs), 4, 'Count without DISTINCT, using literal SQL');
0027911c 66
0d2035db 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');
0027911c 69
0d2035db 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');
2d4f6f0b 72
0d2035db 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');
7655b3c9 75
0d2035db 76 $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { '+select' => { max => 'tagid' }, distinct => 1 });
77 is($get_count->($rs), 4, 'Count with +select aggreggate');
909a20df 78
0d2035db 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}
0814e804 82
04ebc6f4 83throws_ok(
84 sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first },
0d2035db 85 qr/select => { distinct => \.\.\. } syntax is not supported for multiple columns/,
04ebc6f4 86 'throw on unsupported syntax'
87);
7655b3c9 88
04ebc6f4 89# These two rely on the database to throw an exception. This might not be the case one day. Please revise.
040fc6ba 90dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die');