Commit | Line | Data |
96e7f9ec |
1 | use strict; |
8124a75c |
2 | use warnings; |
96e7f9ec |
3 | |
4 | use Test::More; |
1b374bf3 |
5 | use Test::Exception; |
96e7f9ec |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
a2287768 |
8 | use IO::File; |
96e7f9ec |
9 | |
10 | my $schema = DBICTest->init_schema(); |
96e7f9ec |
11 | my $orig_debug = $schema->storage->debug; |
12 | |
8bcbe239 |
13 | plan tests => 44; |
96e7f9ec |
14 | |
96e7f9ec |
15 | my $queries = 0; |
16 | $schema->storage->debugcb(sub { $queries++; }); |
17 | $schema->storage->debug(1); |
18 | |
e9bd1473 |
19 | my $search = { 'artist.name' => 'Caterwauler McCrae' }; |
20 | my $attr = { prefetch => [ qw/artist liner_notes/ ], |
21 | order_by => 'me.cdid' }; |
e9bd1473 |
22 | |
23 | my $rs = $schema->resultset("CD")->search($search, $attr); |
96e7f9ec |
24 | my @cd = $rs->all; |
25 | |
26 | is($cd[0]->title, 'Spoonful of bees', 'First record returned ok'); |
27 | |
28 | ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join'); |
29 | |
30 | is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN'); |
31 | |
32 | is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class'); |
33 | |
34 | is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok'); |
35 | |
36 | is($queries, 1, 'prefetch ran only 1 select statement'); |
37 | |
38 | $schema->storage->debug($orig_debug); |
39 | $schema->storage->debugobj->callback(undef); |
40 | |
41 | # test for partial prefetch via columns attr |
42 | my $cd = $schema->resultset('CD')->find(1, |
43 | { |
370f2ba2 |
44 | columns => [qw/title artist artist.name/], |
96e7f9ec |
45 | join => { 'artist' => {} } |
46 | } |
47 | ); |
48 | ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched'); |
49 | |
50 | # start test for nested prefetch SELECT count |
51 | $queries = 0; |
52 | $schema->storage->debugcb(sub { $queries++ }); |
53 | $schema->storage->debug(1); |
54 | |
55 | $rs = $schema->resultset('Tag')->search( |
56 | {}, |
57 | { |
58 | prefetch => { cd => 'artist' } |
59 | } |
60 | ); |
61 | |
62 | my $tag = $rs->first; |
63 | |
64 | is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' ); |
65 | |
66 | is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch'); |
67 | |
68 | # count the SELECTs |
69 | #$selects++ if /SELECT(?!.*WHERE 1=0.*)/; |
70 | is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)'); |
71 | |
72 | $queries = 0; |
73 | |
74 | is($tag->search_related('cd')->search_related('artist')->first->name, |
75 | 'Caterwauler McCrae', |
76 | 'chained belongs_to->belongs_to search_related ok'); |
77 | |
78 | is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries'); |
79 | |
80 | $queries = 0; |
81 | |
82 | $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' }); |
83 | |
84 | is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find'); |
85 | |
86 | is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)'); |
87 | |
88 | $queries = 0; |
89 | |
90 | $schema->storage->debugcb(sub { $queries++; }); |
91 | |
92 | $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } }); |
93 | |
94 | is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok'); |
95 | |
96 | is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query'); |
97 | |
98 | $queries = 0; |
99 | |
100 | my $producers = $cd->search_related('cd_to_producer')->search_related('producer'); |
101 | |
102 | is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok'); |
103 | |
104 | is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries'); |
105 | |
106 | $schema->storage->debug($orig_debug); |
107 | $schema->storage->debugobj->callback(undef); |
108 | |
109 | $rs = $schema->resultset('Tag')->search( |
110 | {}, |
111 | { |
112 | join => { cd => 'artist' }, |
113 | prefetch => { cd => 'artist' } |
114 | } |
115 | ); |
116 | |
117 | cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' ); |
118 | |
119 | my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 }, |
120 | { order_by => 'artistid DESC', join => 'cds' }); |
121 | |
122 | is($artist->name, 'Random Boy Band', "Join search by object ok"); |
123 | |
124 | my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' }, |
125 | { join => 'liner_notes' }); |
126 | |
127 | cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have"); |
128 | |
129 | is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved"); |
130 | |
131 | my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' }, |
132 | { join => { 'cds' => 'tags' } }); |
133 | |
134 | cmp_ok( @artists, '==', 2, "two-join search ok" ); |
135 | |
136 | $rs = $schema->resultset("CD")->search( |
137 | {}, |
138 | { group_by => [qw/ title me.cdid /] } |
139 | ); |
140 | |
1cc3ce1e |
141 | cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" ); |
96e7f9ec |
142 | |
143 | cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" ); |
144 | |
145 | $rs = $schema->resultset("CD")->search( |
146 | {}, |
147 | { join => [qw/ artist /], group_by => [qw/ artist.name /] } |
148 | ); |
149 | |
1cc3ce1e |
150 | cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" ); |
96e7f9ec |
151 | |
152 | $rs = $schema->resultset("Artist")->search( |
153 | {}, |
154 | { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } } |
155 | ); |
156 | |
157 | cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" ); |
158 | |
159 | $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }}); |
160 | |
161 | cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" ); |
162 | |
163 | $rs = $schema->resultset("Artist")->search( |
164 | { 'cds.title' => 'Spoonful of bees', |
165 | 'cds_2.title' => 'Forkful of bees' }, |
166 | { join => [ 'cds', 'cds' ] }); |
167 | |
1cc3ce1e |
168 | cmp_ok($rs->count, '==', 1, "single artist returned from multi-join"); |
96e7f9ec |
169 | |
170 | is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned"); |
171 | |
172 | $cd = $schema->resultset('Artist')->first->create_related('cds', |
173 | { |
174 | title => 'Unproduced Single', |
175 | year => 2007 |
176 | }); |
177 | |
178 | my $left_join = $schema->resultset('CD')->search( |
179 | { 'me.cdid' => $cd->cdid }, |
180 | { prefetch => { cd_to_producer => 'producer' } } |
181 | ); |
182 | |
183 | cmp_ok($left_join, '==', 1, 'prefetch with no join record present'); |
184 | |
185 | $queries = 0; |
186 | $schema->storage->debugcb(sub { $queries++ }); |
187 | $schema->storage->debug(1); |
188 | |
189 | my $tree_like = |
8871d4ad |
190 | $schema->resultset('TreeLike')->find(5, |
96e7f9ec |
191 | { join => { parent => { parent => 'parent' } }, |
192 | prefetch => { parent => { parent => 'parent' } } }); |
193 | |
194 | is($tree_like->name, 'quux', 'Bottom of tree ok'); |
195 | $tree_like = $tree_like->parent; |
196 | is($tree_like->name, 'baz', 'First level up ok'); |
197 | $tree_like = $tree_like->parent; |
198 | is($tree_like->name, 'bar', 'Second level up ok'); |
199 | $tree_like = $tree_like->parent; |
200 | is($tree_like->name, 'foo', 'Third level up ok'); |
201 | |
202 | $schema->storage->debug($orig_debug); |
203 | $schema->storage->debugobj->callback(undef); |
204 | |
205 | cmp_ok($queries, '==', 1, 'Only one query run'); |
206 | |
8871d4ad |
207 | $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2}); |
96e7f9ec |
208 | $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first; |
209 | is($tree_like->name, 'quux', 'Tree search_related ok'); |
210 | |
211 | $tree_like = $schema->resultset('TreeLike')->search_related('children', |
8871d4ad |
212 | { 'children.id' => 3, 'children_2.id' => 4 }, |
96e7f9ec |
213 | { prefetch => { children => 'children' } } |
214 | )->first; |
215 | is(eval { $tree_like->children->first->children->first->name }, 'quux', |
216 | 'Tree search_related with prefetch ok'); |
217 | |
218 | $tree_like = eval { $schema->resultset('TreeLike')->search( |
8871d4ad |
219 | { 'children.id' => 3, 'children_2.id' => 6 }, |
8124a75c |
220 | { join => [qw/children children children/] } |
8871d4ad |
221 | )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' } |
96e7f9ec |
222 | )->first->children->first; }; |
223 | is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok'); |
224 | |
96e7f9ec |
225 | $rs = $schema->resultset('Artist'); |
226 | $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' }); |
227 | $rs->create({ artistid => 5, name => 'Emo 4ever' }); |
228 | @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' }); |
229 | is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok'); |
230 | |
231 | # ------------- |
232 | # |
233 | # Tests for multilevel has_many prefetch |
234 | |
235 | # artist resultsets - with and without prefetch |
236 | my $art_rs = $schema->resultset('Artist'); |
237 | my $art_rs_pr = $art_rs->search( |
238 | {}, |
239 | { |
240 | join => [ { cds => ['tracks'] } ], |
241 | prefetch => [ { cds => ['tracks'] } ], |
242 | cache => 1 # last test needs this |
243 | } |
244 | ); |
245 | |
246 | # This test does the same operation twice - once on a |
247 | # set of items fetched from the db with no prefetch of has_many rels |
248 | # The second prefetches 2 levels of has_many |
249 | # We check things are the same by comparing the name or title |
250 | # we build everything into a hash structure and compare the one |
251 | # from each rs to see what differs |
252 | |
253 | sub make_hash_struc { |
254 | my $rs = shift; |
255 | |
256 | my $struc = {}; |
257 | foreach my $art ( $rs->all ) { |
258 | foreach my $cd ( $art->cds ) { |
259 | foreach my $track ( $cd->tracks ) { |
260 | $struc->{ $art->name }{ $cd->title }{ $track->title }++; |
261 | } |
262 | } |
263 | } |
264 | return $struc; |
265 | } |
266 | |
267 | $queries = 0; |
268 | $schema->storage->debugcb(sub { $queries++ }); |
269 | $schema->storage->debug(1); |
270 | |
271 | my $prefetch_result = make_hash_struc($art_rs_pr); |
272 | |
273 | is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query'); |
274 | |
275 | my $nonpre_result = make_hash_struc($art_rs); |
276 | |
277 | is_deeply( $prefetch_result, $nonpre_result, |
278 | 'Compare 2 level prefetch result to non-prefetch result' ); |
279 | |
280 | $queries = 0; |
281 | |
282 | is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title, |
283 | 'Fowlin', |
284 | 'chained has_many->has_many search_related ok' |
285 | ); |
286 | |
287 | is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries'); |
b2df8400 |
288 | |
a2287768 |
289 | $schema->storage->debug($orig_debug); |
290 | $schema->storage->debugobj->callback(undef); |