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