Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / prefetch / standard.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 use DBICTest;
10
11 my $schema = DBICTest->init_schema();
12
13 my $rs;
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' };
18
19   $rs = $schema->resultset("CD")->search($search, $attr);
20   my @cd = $rs->all;
21
22   is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
23
24   ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
25
26   is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
27
28   is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
29
30   is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
31 }, 1, 'prefetch ran only 1 select statement');
32
33 # test for partial prefetch via columns attr
34 my $cd = $schema->resultset('CD')->find(1,
35     {
36       columns => [qw/title artist artist.name/],
37       join => { 'artist' => {} }
38     }
39 );
40 is( $cd->artist->name, 'Caterwauler McCrae', 'single related column prefetched');
41
42 # start test for nested prefetch SELECT count
43 my $tag;
44 $schema->is_executed_querycount( sub {
45   $rs = $schema->resultset('Tag')->search(
46     { 'me.tagid' => 1 },
47     {
48       prefetch => { cd => 'artist' }
49     }
50   );
51
52   $tag = $rs->first;
53
54   is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
55
56   is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
57 }, 1, 'nested prefetch ran exactly 1 select statement');
58
59
60 $schema->is_executed_querycount( sub {
61   is($tag->search_related('cd')->search_related('artist')->first->name,
62    'Caterwauler McCrae',
63    'chained belongs_to->belongs_to search_related ok');
64 }, 0, 'chained search_related after belongs_to->belongs_to prefetch ran no queries');
65
66
67 $schema->is_executed_querycount( sub {
68   $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
69
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)');
72
73 $schema->is_executed_querycount( sub {
74   $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' }, order_by => 'producer.producerid' });
75
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');
78
79 $schema->is_executed_querycount( sub {
80   my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
81
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');
84
85 $rs = $schema->resultset('Tag')->search(
86   {},
87   {
88     join => { cd => 'artist' },
89     prefetch => { cd => 'artist' }
90   }
91 );
92
93 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
94
95 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
96                  { order_by => 'artistid DESC', join => 'cds' });
97
98 is($artist->name, 'Random Boy Band', "Join search by object ok");
99
100 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
101                                { join => 'liner_notes' });
102
103 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
104
105 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
106
107 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
108                                        { join => { 'cds' => 'tags' } });
109
110 cmp_ok( @artists, '==', 2, "two-join search ok" );
111
112 $rs = $schema->resultset("CD")->search(
113   {},
114   { group_by => [qw/ title me.cdid /] }
115 );
116
117 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
118
119 cmp_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
126 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
127
128 $rs = $schema->resultset("Artist")->search({}, {
129   join => [qw/ cds /],
130   group_by => [qw/ me.name /],
131   having => \[ 'MAX(cds.cdid) < ?', [ \'int' => 5 ] ],
132 });
133
134 cmp_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
138 cmp_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
145 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
146
147 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
148
149 $cd = $schema->resultset('Artist')->search({ artistid => 1 })->first->create_related('cds',
150     {
151     title   => 'Unproduced Single',
152     year    => 2007
153 });
154
155 my $left_join = $schema->resultset('CD')->search(
156     { 'me.cdid' => $cd->cdid },
157     { prefetch => { cd_to_producer => 'producer' } }
158 );
159
160 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
161
162 my $tree_like;
163 $schema->is_executed_querycount( sub {
164   $tree_like =
165     $schema->resultset('TreeLike')->find(5,
166       { join     => { parent => { parent => 'parent' } },
167          prefetch => { parent => { parent => 'parent' } } });
168
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');
176
177 }, 1, 'Only one query run');
178
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');
182
183 $tree_like = $schema->resultset('TreeLike')->search_related('children',
184     { 'children.id' => 3, 'children_2.id' => 4 },
185     { prefetch => { children => 'children' } }
186   )->first;
187 is( $tree_like->children->first->children->first->name, 'quux',
188    'Tree search_related with prefetch ok');
189
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');
196
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');
202
203 lives_ok { @artists = $rs->search(undef, {
204         join => ['cds'],
205         prefetch => [],
206         rows => 20,
207     });
208 } 'join and empty prefetch ok';
209
210 # -------------
211 #
212 # Tests for multilevel has_many prefetch
213
214 # artist resultsets - with and without prefetch
215 my $art_rs = $schema->resultset('Artist');
216 my $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
232 sub make_hash_struc {
233     my $rs = shift;
234
235     my $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 }++;
245             }
246         }
247     }
248     return $struc;
249 }
250
251
252 my $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');
256
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' );
260
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');
270
271 done_testing;