clearer
[dbsrgits/DBIx-Class.git] / t / prefetch / grouped.t
CommitLineData
0c5ea449 1use strict;
2use warnings;
3use Test::More;
4
5use lib qw(t/lib);
6use DBICTest;
7use DBIC::SqlMakerTest;
8
9#plan tests => 6;
10plan 'no_plan';
11
12my $schema = DBICTest->init_schema();
a2287768 13my $sdebug = $schema->storage->debug;
0c5ea449 14
15my $cd_rs = $schema->resultset('CD')->search (
16 { 'tracks.cd' => { '!=', undef } },
17 { prefetch => 'tracks' },
18);
19
20# Database sanity check
21is($cd_rs->count, 5, 'CDs with tracks count');
22for ($cd_rs->all) {
23 is ($_->tracks->count, 3, '3 tracks for CD' . $_->id );
24}
25
26# Test a belongs_to prefetch of a has_many
27{
28 my $track_rs = $schema->resultset ('Track')->search (
29 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
30 {
31 # the select/as is deliberately silly to test both funcs and refs below
32 select => [
33 'me.cd',
0c5ea449 34 { count => 'me.trackid' },
35 ],
36 as => [qw/
37 cd
38 track_count
0c5ea449 39 /],
40 group_by => [qw/me.cd/],
41 prefetch => 'cd',
42 },
43 );
44
45 is($track_rs->count, 5, 'Prefetched count with groupby');
46 is($track_rs->all, 5, 'Prefetched objects with groupby');
47
48 {
49 my $query_cnt = 0;
50 $schema->storage->debugcb ( sub { $query_cnt++ } );
a2287768 51 $schema->storage->debug (1);
0c5ea449 52
53 $track_rs->reset;
54 while (my $collapsed_track = $track_rs->next) {
0c5ea449 55 my $cdid = $collapsed_track->get_column('cd');
56 is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
57 ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
58 }
59
60 is ($query_cnt, 0, 'No queries on prefetched titles');
61 $schema->storage->debugcb (undef);
a2287768 62 $schema->storage->debug ($sdebug);
0c5ea449 63 }
64
65 # Test sql by hand, as the sqlite db will simply paper over
66 # improper group/select combinations
67 #
68 # the exploded IN needs fixing below, coming in another branch
69 #
70 is_same_sql_bind (
71 $track_rs->count_rs->as_query,
72 '(
73 SELECT COUNT( * )
74 FROM (
75 SELECT me.cd
76 FROM track me
77 JOIN cd cd ON cd.cdid = me.cd
78 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
79 GROUP BY me.cd
80 )
81 count_subq
82 )',
83 [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
84 'count() query generated expected SQL',
85 );
86
0c5ea449 87 is_same_sql_bind (
88 $track_rs->as_query,
89 '(
5b45001f 90 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
0c5ea449 91 FROM (
5b45001f 92 SELECT me.cd, COUNT (me.trackid) AS track_count,
0c5ea449 93 FROM track me
1c1937b7 94 JOIN cd cd ON cd.cdid = me.cd
95 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
0c5ea449 96 GROUP BY me.cd
97 ) as me
98 JOIN cd cd ON cd.cdid = me.cd
99 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
100 )',
0bdff769 101 [ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
0c5ea449 102 'next() query generated expected SQL',
103 );
104
105
106 # add an extra track to one of the cds, and then make sure we can get it on top
107 # (check if limit works)
108 my $top_cd = $cd_rs->slice (1,1)->next;
109 $top_cd->create_related ('tracks', {
110 title => 'over the top',
111 });
112
113 my $top_cd_collapsed_track = $track_rs->search ({}, {
114 rows => 2,
115 order_by => [
116 { -desc => 'track_count' },
117 ],
118 });
119
120 is ($top_cd_collapsed_track->count, 2);
121
122 is (
123 $top_cd->title,
124 $top_cd_collapsed_track->first->cd->title,
125 'Correct collapsed track with prefetched CD returned on top'
126 );
127}
128
129# test a has_many/might_have prefetch at the same level
0c5ea449 130# Note that one of the CDs now has 4 tracks instead of 3
131{
132 my $most_tracks_rs = $cd_rs->search ({}, {
133 prefetch => 'liner_notes', # tracks are alredy prefetched
134 select => ['me.cdid', { count => 'tracks.trackid' } ],
135 as => [qw/cdid track_count/],
136 group_by => 'me.cdid',
5b45001f 137 order_by => { -desc => 'track_count' },
0c5ea449 138 rows => 2,
139 });
140
141 is_same_sql_bind (
142 $most_tracks_rs->count_rs->as_query,
143 '(
144 SELECT COUNT( * )
145 FROM (
146 SELECT me.cdid
147 FROM cd me
148 LEFT JOIN track tracks ON tracks.cd = me.cdid
149 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
150 WHERE ( tracks.cd IS NOT NULL )
151 GROUP BY me.cdid
152 LIMIT 2
153 ) count_subq
154 )',
155 [],
156 'count() query generated expected SQL',
157 );
158
159 is_same_sql_bind (
160 $most_tracks_rs->as_query,
161 '(
1c1937b7 162 SELECT me.cdid, me.track_count, tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, liner_notes.liner_id, liner_notes.notes
0c5ea449 163 FROM (
a57827b6 164 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count
0c5ea449 165 FROM cd me
166 LEFT JOIN track tracks ON tracks.cd = me.cdid
167 WHERE ( tracks.cd IS NOT NULL )
168 GROUP BY me.cdid
1c1937b7 169 ORDER BY track_count DESC
0c5ea449 170 LIMIT 2
171 ) me
172 LEFT JOIN track tracks ON tracks.cd = me.cdid
173 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
174 WHERE ( tracks.cd IS NOT NULL )
1c1937b7 175 ORDER BY track_count DESC, tracks.cd
0c5ea449 176 )',
177 [],
178 'next() query generated expected SQL',
179 );
180
181 is ($most_tracks_rs->count, 2, 'Limit works');
182 my $top_cd = $most_tracks_rs->first;
183 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) above
184
185 my $query_cnt = 0;
186 $schema->storage->debugcb ( sub { $query_cnt++ } );
a2287768 187 $schema->storage->debug (1);
0c5ea449 188
189 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
190 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
191 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
192 is (
193 $top_cd->liner_notes->notes,
194 'Buy Whiskey!',
195 'Correct liner pre-fetched with top cd',
196 );
197
198 is ($query_cnt, 0, 'No queries executed during prefetched data access');
199 $schema->storage->debugcb (undef);
a2287768 200 $schema->storage->debug ($sdebug);
0c5ea449 201}