Commit | Line | Data |
2a2ca43f |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | |
6 | use lib qw(t/lib); |
7 | |
8 | use DBICTest; |
9 | use DBIC::SqlMakerTest; |
10 | |
11 | my $schema = DBICTest->init_schema(); |
12 | |
13 | eval "use DBD::SQLite"; |
14 | plan skip_all => 'needs DBD::SQLite for testing' if $@; |
2d4f6f0b |
15 | plan tests => 16; |
0027911c |
16 | |
2d4f6f0b |
17 | # The tag Blue is assigned to cds 1 2 3 and 5 |
18 | # The tag Cheesy is assigned to cds 2 4 and 5 |
19 | # |
20 | # This combination should make some interesting group_by's |
21 | # |
866557f9 |
22 | my $rs; |
2d4f6f0b |
23 | my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }); |
2a2ca43f |
24 | |
866557f9 |
25 | $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }); |
26 | is($rs->count, 4, 'Count without DISTINCT'); |
2a2ca43f |
27 | |
2d4f6f0b |
28 | $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'tag' }); |
866557f9 |
29 | is($rs->count, 2, 'Count with single column group_by'); |
2a2ca43f |
30 | |
2d4f6f0b |
31 | $rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }, { group_by => 'cd' }); |
32 | is($rs->count, 5, 'Count with another single column group_by'); |
33 | |
866557f9 |
34 | $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]}); |
35 | is($rs->count, 4, 'Count with multiple column group_by'); |
2a2ca43f |
36 | |
866557f9 |
37 | $rs = $schema->resultset('Tag')->search({ tag => 'Blue' }, { distinct => 1 }); |
38 | is($rs->count, 4, 'Count with single column distinct'); |
0027911c |
39 | |
866557f9 |
40 | $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }); |
2d4f6f0b |
41 | is($rs->count, 7, 'Count with IN subquery'); |
0027911c |
42 | |
866557f9 |
43 | $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' }); |
5a825e67 |
44 | is($rs->count, 2, 'Count with IN subquery with outside group_by'); |
0027911c |
45 | |
866557f9 |
46 | $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 }); |
2d4f6f0b |
47 | is($rs->count, 7, 'Count with IN subquery with outside distinct'); |
0027911c |
48 | |
866557f9 |
49 | $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }), |
5a825e67 |
50 | is($rs->count, 2, 'Count with IN subquery with outside distinct on a single column'); |
0027911c |
51 | |
866557f9 |
52 | $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } }); |
2d4f6f0b |
53 | is($rs->count, 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($rs->count, 7, 'Count with IN subquery with another single group_by'); |
0027911c |
57 | |
866557f9 |
58 | $rs = $schema->resultset('Tag')->search({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } }); |
2d4f6f0b |
59 | is($rs->count, 7, 'Count with IN subquery with multiple group_by'); |
0027911c |
60 | |
866557f9 |
61 | $rs = $schema->resultset('Tag')->search({ tag => \"= 'Blue'" }); |
62 | is($rs->count, 4, 'Count without DISTINCT, using literal SQL'); |
0027911c |
63 | |
2d4f6f0b |
64 | $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'tag' }); |
866557f9 |
65 | is($rs->count, 2, 'Count with literal SQL and single group_by'); |
0027911c |
66 | |
2d4f6f0b |
67 | $rs = $schema->resultset('Tag')->search({ tag => \" IN ('Blue', 'Cheesy')" }, { group_by => 'cd' }); |
68 | is($rs->count, 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($rs->count, 7, 'Count with literal SQL and multiple group_by'); |