Confirm prefetch doesn't affect main row fetch, and main row fetch works with and...
[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
22ed9526 45 # this used to fuck up ->all, do not remove!
46 ok ($track_rs->first, 'There is stuff in the rs');
47
0c5ea449 48 is($track_rs->count, 5, 'Prefetched count with groupby');
49 is($track_rs->all, 5, 'Prefetched objects with groupby');
50
51 {
52 my $query_cnt = 0;
53 $schema->storage->debugcb ( sub { $query_cnt++ } );
a2287768 54 $schema->storage->debug (1);
0c5ea449 55
0c5ea449 56 while (my $collapsed_track = $track_rs->next) {
0c5ea449 57 my $cdid = $collapsed_track->get_column('cd');
58 is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
59 ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
60 }
61
22ed9526 62 is ($query_cnt, 1, 'Single query on prefetched titles');
0c5ea449 63 $schema->storage->debugcb (undef);
a2287768 64 $schema->storage->debug ($sdebug);
0c5ea449 65 }
66
67 # Test sql by hand, as the sqlite db will simply paper over
68 # improper group/select combinations
69 #
70 # the exploded IN needs fixing below, coming in another branch
71 #
72 is_same_sql_bind (
73 $track_rs->count_rs->as_query,
74 '(
75 SELECT COUNT( * )
76 FROM (
77 SELECT me.cd
78 FROM track me
79 JOIN cd cd ON cd.cdid = me.cd
d8dbe471 80 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
0c5ea449 81 GROUP BY me.cd
82 )
83 count_subq
84 )',
85 [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
86 'count() query generated expected SQL',
87 );
88
0c5ea449 89 is_same_sql_bind (
90 $track_rs->as_query,
91 '(
5b45001f 92 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
0c5ea449 93 FROM (
5b45001f 94 SELECT me.cd, COUNT (me.trackid) AS track_count,
0c5ea449 95 FROM track me
1c1937b7 96 JOIN cd cd ON cd.cdid = me.cd
d8dbe471 97 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
0c5ea449 98 GROUP BY me.cd
99 ) as me
100 JOIN cd cd ON cd.cdid = me.cd
d8dbe471 101 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
0c5ea449 102 )',
0bdff769 103 [ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
0c5ea449 104 'next() query generated expected SQL',
105 );
106
107
108 # add an extra track to one of the cds, and then make sure we can get it on top
109 # (check if limit works)
110 my $top_cd = $cd_rs->slice (1,1)->next;
111 $top_cd->create_related ('tracks', {
112 title => 'over the top',
113 });
114
115 my $top_cd_collapsed_track = $track_rs->search ({}, {
116 rows => 2,
117 order_by => [
118 { -desc => 'track_count' },
119 ],
120 });
121
122 is ($top_cd_collapsed_track->count, 2);
123
124 is (
125 $top_cd->title,
126 $top_cd_collapsed_track->first->cd->title,
127 'Correct collapsed track with prefetched CD returned on top'
128 );
129}
130
131# test a has_many/might_have prefetch at the same level
0c5ea449 132# Note that one of the CDs now has 4 tracks instead of 3
133{
134 my $most_tracks_rs = $cd_rs->search ({}, {
135 prefetch => 'liner_notes', # tracks are alredy prefetched
136 select => ['me.cdid', { count => 'tracks.trackid' } ],
137 as => [qw/cdid track_count/],
138 group_by => 'me.cdid',
5b45001f 139 order_by => { -desc => 'track_count' },
0c5ea449 140 rows => 2,
141 });
142
143 is_same_sql_bind (
144 $most_tracks_rs->count_rs->as_query,
145 '(
146 SELECT COUNT( * )
147 FROM (
148 SELECT me.cdid
149 FROM cd me
150 LEFT JOIN track tracks ON tracks.cd = me.cdid
151 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
152 WHERE ( tracks.cd IS NOT NULL )
153 GROUP BY me.cdid
154 LIMIT 2
155 ) count_subq
156 )',
157 [],
158 'count() query generated expected SQL',
159 );
160
161 is_same_sql_bind (
162 $most_tracks_rs->as_query,
163 '(
1c1937b7 164 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 165 FROM (
a57827b6 166 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count
0c5ea449 167 FROM cd me
168 LEFT JOIN track tracks ON tracks.cd = me.cdid
169 WHERE ( tracks.cd IS NOT NULL )
170 GROUP BY me.cdid
1c1937b7 171 ORDER BY track_count DESC
0c5ea449 172 LIMIT 2
173 ) me
174 LEFT JOIN track tracks ON tracks.cd = me.cdid
175 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
176 WHERE ( tracks.cd IS NOT NULL )
1c1937b7 177 ORDER BY track_count DESC, tracks.cd
0c5ea449 178 )',
179 [],
180 'next() query generated expected SQL',
181 );
182
183 is ($most_tracks_rs->count, 2, 'Limit works');
184 my $top_cd = $most_tracks_rs->first;
22ed9526 185 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
0c5ea449 186
187 my $query_cnt = 0;
188 $schema->storage->debugcb ( sub { $query_cnt++ } );
a2287768 189 $schema->storage->debug (1);
0c5ea449 190
191 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
192 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
193 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
194 is (
195 $top_cd->liner_notes->notes,
196 'Buy Whiskey!',
197 'Correct liner pre-fetched with top cd',
198 );
199
200 is ($query_cnt, 0, 'No queries executed during prefetched data access');
201 $schema->storage->debugcb (undef);
a2287768 202 $schema->storage->debug ($sdebug);
0c5ea449 203}