Another candidate for somethingawful.com (fix left join-ed count)
[dbsrgits/DBIx-Class.git] / t / count / count_rs.t
CommitLineData
a70f69d1 1use strict;
2use warnings;
3
4use lib qw(t/lib);
5
6use Test::More;
7use DBICTest;
8use DBIC::SqlMakerTest;
9use DBIC::DebugObj;
10
11plan tests => 10;
12
13my $schema = DBICTest->init_schema();
14
15# non-collapsing prefetch (no multi prefetches)
16{
17 my $rs = $schema->resultset("CD")
18 ->search_related('tracks',
19 { position => [1,2] },
20 { prefetch => [qw/disc lyrics/], rows => 3, offset => 8 },
21 );
22 is ($rs->all, 2, 'Correct number of objects');
23
24
25 my ($sql, @bind);
26 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
27 $schema->storage->debug(1);
28
29 is ($rs->count, 2, 'Correct count via count()');
30
31 is_same_sql_bind (
32 $sql,
33 \@bind,
34 'SELECT COUNT( * )
35 FROM cd me
c98169a7 36 JOIN track tracks ON tracks.cd = me.cdid
a70f69d1 37 JOIN cd disc ON disc.cdid = tracks.cd
38 LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid
39 WHERE ( ( position = ? OR position = ? ) )
40 ',
41 [ qw/'1' '2'/ ],
42 'count softlimit applied',
43 );
44
45 my $crs = $rs->count_rs;
46 is ($crs->next, 2, 'Correct count via count_rs()');
47
48 is_same_sql_bind (
49 $crs->as_query,
50 '(SELECT COUNT( * )
51 FROM (
52 SELECT tracks.trackid
53 FROM cd me
c98169a7 54 JOIN track tracks ON tracks.cd = me.cdid
a70f69d1 55 JOIN cd disc ON disc.cdid = tracks.cd
56 LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid
57 WHERE ( ( position = ? OR position = ? ) )
58 LIMIT 3 OFFSET 8
59 ) count_subq
60 )',
61 [ [ position => 1 ], [ position => 2 ] ],
62 'count_rs db-side limit applied',
63 );
64}
65
66# has_many prefetch with limit
67{
68 my $rs = $schema->resultset("Artist")
69 ->search_related('cds',
70 { 'tracks.position' => [1,2] },
71 { prefetch => [qw/tracks artist/], rows => 3, offset => 4 },
72 );
73 is ($rs->all, 1, 'Correct number of objects');
74
75 my ($sql, @bind);
76 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
77 $schema->storage->debug(1);
78
79 is ($rs->count, 1, 'Correct count via count()');
80
81 is_same_sql_bind (
82 $sql,
83 \@bind,
84 'SELECT COUNT( * )
85 FROM (
86 SELECT cds.cdid
87 FROM artist me
c98169a7 88 JOIN cd cds ON cds.artist = me.artistid
a70f69d1 89 LEFT JOIN track tracks ON tracks.cd = cds.cdid
90 JOIN artist artist ON artist.artistid = cds.artist
91 WHERE tracks.position = ? OR tracks.position = ?
92 GROUP BY cds.cdid
93 ) count_subq
94 ',
95 [ qw/'1' '2'/ ],
96 'count softlimit applied',
97 );
98
99 my $crs = $rs->count_rs;
100 is ($crs->next, 1, 'Correct count via count_rs()');
101
102 is_same_sql_bind (
103 $crs->as_query,
104 '(SELECT COUNT( * )
105 FROM (
106 SELECT cds.cdid
107 FROM artist me
c98169a7 108 JOIN cd cds ON cds.artist = me.artistid
a70f69d1 109 LEFT JOIN track tracks ON tracks.cd = cds.cdid
110 JOIN artist artist ON artist.artistid = cds.artist
111 WHERE tracks.position = ? OR tracks.position = ?
112 GROUP BY cds.cdid
113 LIMIT 3 OFFSET 4
114 ) count_subq
115 )',
116 [ [ 'tracks.position' => 1 ], [ 'tracks.position' => 2 ] ],
117 'count_rs db-side limit applied',
118 );
119}