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