1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
11 my $schema = DBICTest->init_schema();
14 $schema->is_executed_querycount( sub {
15 my $search = { 'artist.name' => 'Caterwauler McCrae' };
16 my $attr = { prefetch => [ qw/artist liner_notes/ ],
17 order_by => 'me.cdid' };
19 $rs = $schema->resultset("CD")->search($search, $attr);
22 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
24 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
26 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
28 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
30 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
31 }, 1, 'prefetch ran only 1 select statement');
33 # test for partial prefetch via columns attr
34 my $cd = $schema->resultset('CD')->find(1,
36 columns => [qw/title artist artist.name/],
37 join => { 'artist' => {} }
40 is( $cd->artist->name, 'Caterwauler McCrae', 'single related column prefetched');
42 # start test for nested prefetch SELECT count
44 $schema->is_executed_querycount( sub {
45 $rs = $schema->resultset('Tag')->search(
48 prefetch => { cd => 'artist' }
54 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
56 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
57 }, 1, 'nested prefetch ran exactly 1 select statement');
60 $schema->is_executed_querycount( sub {
61 is($tag->search_related('cd')->search_related('artist')->first->name,
63 'chained belongs_to->belongs_to search_related ok');
64 }, 0, 'chained search_related after belongs_to->belongs_to prefetch ran no queries');
67 $schema->is_executed_querycount( sub {
68 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
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)');
73 $schema->is_executed_querycount( sub {
74 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' }, order_by => 'producer.producerid' });
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');
79 $schema->is_executed_querycount( sub {
80 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
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');
85 $rs = $schema->resultset('Tag')->search(
88 join => { cd => 'artist' },
89 prefetch => { cd => 'artist' }
93 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
95 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
96 { order_by => 'artistid DESC', join => 'cds' });
98 is($artist->name, 'Random Boy Band', "Join search by object ok");
100 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
101 { join => 'liner_notes' });
103 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
105 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
107 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
108 { join => { 'cds' => 'tags' } });
110 cmp_ok( @artists, '==', 2, "two-join search ok" );
112 $rs = $schema->resultset("CD")->search(
114 { group_by => [qw/ title me.cdid /] }
117 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
119 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
121 $rs = $schema->resultset("CD")->search(
123 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
126 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
128 $rs = $schema->resultset("Artist")->search({}, {
130 group_by => [qw/ me.name /],
131 having => \[ 'MAX(cds.cdid) < ?', [ \'int' => 5 ] ],
134 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
136 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
138 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
140 $rs = $schema->resultset("Artist")->search(
141 { 'cds.title' => 'Spoonful of bees',
142 'cds_2.title' => 'Forkful of bees' },
143 { join => [ 'cds', 'cds' ] });
145 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
147 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
149 $cd = $schema->resultset('Artist')->search({ artistid => 1 })->first->create_related('cds',
151 title => 'Unproduced Single',
155 my $left_join = $schema->resultset('CD')->search(
156 { 'me.cdid' => $cd->cdid },
157 { prefetch => { cd_to_producer => 'producer' } }
160 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
163 $schema->is_executed_querycount( sub {
165 $schema->resultset('TreeLike')->find(5,
166 { join => { parent => { parent => 'parent' } },
167 prefetch => { parent => { parent => 'parent' } } });
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');
177 }, 1, 'Only one query run');
179 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
180 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
181 is($tree_like->name, 'quux', 'Tree search_related ok');
183 $tree_like = $schema->resultset('TreeLike')->search_related('children',
184 { 'children.id' => 3, 'children_2.id' => 4 },
185 { prefetch => { children => 'children' } }
187 is( $tree_like->children->first->children->first->name, 'quux',
188 'Tree search_related with prefetch ok');
190 $tree_like = $schema->resultset('TreeLike')->search(
191 { 'children.id' => 3, 'children_2.id' => 6 },
192 { join => [qw/children children children/] }
193 )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
194 )->first->children->first;
195 is( $tree_like->name, 'fong', 'Tree with multiple has_many joins ok');
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' });
201 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
203 lives_ok { @artists = $rs->search(undef, {
208 } 'join and empty prefetch ok';
212 # Tests for multilevel has_many prefetch
214 # artist resultsets - with and without prefetch
215 my $art_rs = $schema->resultset('Artist');
216 my $art_rs_pr = $art_rs->search(
219 join => [ { cds => ['tracks'] } ],
220 prefetch => [ { cds => ['tracks'] } ],
221 cache => 1 # last test needs this
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
232 sub make_hash_struc {
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 ) {
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 }++;
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');
257 my $nonpre_result = make_hash_struc($art_rs);
258 is_deeply( $prefetch_result, $nonpre_result,
259 'Compare 2 level prefetch result to non-prefetch result' );
261 $schema->is_executed_querycount( sub {
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'
269 }, 0, 'chained search_related after has_many->has_many prefetch ran no queries');