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