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