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