Adjust a couple of tests for new behavior (thus all of this might be backwards incomp...
[dbsrgits/DBIx-Class.git] / t / prefetch / standard.t
CommitLineData
96e7f9ec 1use strict;
8124a75c 2use warnings;
96e7f9ec 3
4use Test::More;
1b374bf3 5use Test::Exception;
96e7f9ec 6use lib qw(t/lib);
7use DBICTest;
8use Data::Dumper;
9
10my $schema = DBICTest->init_schema();
11
12my $orig_debug = $schema->storage->debug;
13
14use IO::File;
15
8124a75c 16plan tests => 44;
96e7f9ec 17
96e7f9ec 18my $queries = 0;
19$schema->storage->debugcb(sub { $queries++; });
20$schema->storage->debug(1);
21
e9bd1473 22my $search = { 'artist.name' => 'Caterwauler McCrae' };
23my $attr = { prefetch => [ qw/artist liner_notes/ ],
24 order_by => 'me.cdid' };
25my $search_str = Dumper($search);
26my $attr_str = Dumper($attr);
27
28my $rs = $schema->resultset("CD")->search($search, $attr);
96e7f9ec 29my @cd = $rs->all;
30
31is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
32
33ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
34
35is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
36
37is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
38
39is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
40
41is($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
47my $cd = $schema->resultset('CD')->find(1,
48 {
370f2ba2 49 columns => [qw/title artist artist.name/],
96e7f9ec 50 join => { 'artist' => {} }
51 }
52);
53ok(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
67my $tag = $rs->first;
68
69is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
70
71is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
72
73# count the SELECTs
74#$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
75is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
76
77$queries = 0;
78
79is($tag->search_related('cd')->search_related('artist')->first->name,
80 'Caterwauler McCrae',
81 'chained belongs_to->belongs_to search_related ok');
82
83is($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
89is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
90
91is($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
99is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
100
101is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
102
103$queries = 0;
104
105my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
106
107is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
108
109is($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
122cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
123
124my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
125 { order_by => 'artistid DESC', join => 'cds' });
126
127is($artist->name, 'Random Boy Band', "Join search by object ok");
128
129my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
130 { join => 'liner_notes' });
131
132cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
133
134is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
135
136my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
137 { join => { 'cds' => 'tags' } });
138
139cmp_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 146cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
96e7f9ec 147
148cmp_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 155cmp_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
162cmp_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
166cmp_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 173cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
96e7f9ec 174
175is($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
183my $left_join = $schema->resultset('CD')->search(
184 { 'me.cdid' => $cd->cdid },
185 { prefetch => { cd_to_producer => 'producer' } }
186);
187
188cmp_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
194my $tree_like =
8871d4ad 195 $schema->resultset('TreeLike')->find(5,
96e7f9ec 196 { join => { parent => { parent => 'parent' } },
197 prefetch => { parent => { parent => 'parent' } } });
198
199is($tree_like->name, 'quux', 'Bottom of tree ok');
200$tree_like = $tree_like->parent;
201is($tree_like->name, 'baz', 'First level up ok');
202$tree_like = $tree_like->parent;
203is($tree_like->name, 'bar', 'Second level up ok');
204$tree_like = $tree_like->parent;
205is($tree_like->name, 'foo', 'Third level up ok');
206
207$schema->storage->debug($orig_debug);
208$schema->storage->debugobj->callback(undef);
209
210cmp_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;
214is($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;
220is(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; };
228is(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' });
234is(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
241my $art_rs = $schema->resultset('Artist');
242my $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
258sub 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
276my $prefetch_result = make_hash_struc($art_rs_pr);
277
278is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
279
280my $nonpre_result = make_hash_struc($art_rs);
281
282is_deeply( $prefetch_result, $nonpre_result,
283 'Compare 2 level prefetch result to non-prefetch result' );
284
285$queries = 0;
286
287is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
288 'Fowlin',
289 'chained has_many->has_many search_related ok'
290 );
291
292is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');
b2df8400 293