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