Commit | Line | Data |
2a2ca43f |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
7655b3c9 |
5 | use Test::Exception; |
2a2ca43f |
6 | |
7 | use lib qw(t/lib); |
8 | |
9 | use DBICTest; |
10 | use DBIC::SqlMakerTest; |
11 | |
12 | my $schema = DBICTest->init_schema(); |
13 | |
0d2035db |
14 | plan tests => 58; |
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 |
21 | my $rs; |
2d4f6f0b |
22 | my $in_rs = $schema->resultset('Tag')->search({ tag => [ 'Blue', 'Cheesy' ] }); |
2a2ca43f |
23 | |
0d2035db |
24 | for 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 |
83 | eval { |
84 | my @warnings; |
85 | local $SIG{__WARN__} = sub { $_[0] =~ /The select => { distinct => ... } syntax will be deprecated/ |
86 | ? push @warnings, @_ |
87 | : warn @_ |
88 | }; |
7655b3c9 |
89 | my $row = $schema->resultset('Tag')->search({}, { select => { distinct => 'tag' } })->first; |
04ebc6f4 |
90 | is (@warnings, 1, 'Warned about deprecated distinct') if $DBIx::Class::VERSION < 0.09; |
91 | }; |
92 | ok ($@, 'Exception on deprecated distinct usage thrown') if $DBIx::Class::VERSION >= 0.09; |
7655b3c9 |
93 | |
04ebc6f4 |
94 | throws_ok( |
95 | sub { my $row = $schema->resultset('Tag')->search({}, { select => { distinct => [qw/tag cd/] } })->first }, |
0d2035db |
96 | qr/select => { distinct => \.\.\. } syntax is not supported for multiple columns/, |
04ebc6f4 |
97 | 'throw on unsupported syntax' |
98 | ); |
7655b3c9 |
99 | |
04ebc6f4 |
100 | # These two rely on the database to throw an exception. This might not be the case one day. Please revise. |
040fc6ba |
101 | dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die'); |
102 | dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { select => { length => 'tag' }, distinct => 1 })->count }, 'expecting to die'); |