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