Stop adding empty caches to resultsets chained off prefetched objects
[dbsrgits/DBIx-Class.git] / t / prefetch / via_search_related.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6
7 use lib qw(t/lib);
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
11
12 my $queries;
13 my $debugcb = sub { $queries++; };
14 my $orig_debug = $schema->storage->debug;
15
16 lives_ok ( sub {
17   my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
18     {
19       'cd.year' => "2000",
20     },
21     {
22       join => 'tags',
23       order_by => 'me.trackid',
24       rows => 1,
25     }
26   );
27
28   my $use_prefetch = $no_prefetch->search(
29     {},
30     {
31       prefetch => 'tags',
32     }
33   );
34
35   is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
36   is(
37     scalar ($use_prefetch->all),
38     scalar ($no_prefetch->all),
39     "Amount of returned rows is right"
40   );
41
42 }, 'search_related prefetch with order_by works');
43
44 lives_ok ( sub {
45   my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
46     {
47       'cd.year' => "2000",
48       'tagid' => 1,
49     },
50     {
51       join => 'tags',
52       rows => 1,
53     }
54   );
55
56   my $use_prefetch = $no_prefetch->search(
57     undef,
58     {
59       prefetch => 'tags',
60     }
61   );
62
63   is(
64     scalar ($use_prefetch->all),
65     scalar ($no_prefetch->all),
66     "Amount of returned rows is right"
67   );
68   is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
69
70 }, 'search_related prefetch with condition referencing unqualified column of a joined table works');
71
72 # make sure chains off prefetched results still work
73 {
74   my $cd = $schema->resultset('CD')->search({}, { prefetch => 'cd_to_producer' })->find(1);
75
76   $queries = 0;
77   $schema->storage->debugcb ($debugcb);
78   $schema->storage->debug (1);
79
80   is( $cd->cd_to_producer->count, 3 ,'Count of prefetched m2m links via accessor' );
81   is( scalar $cd->cd_to_producer->all, 3, 'Amount of prefetched m2m link objects via accessor' );
82   is( $cd->search_related('cd_to_producer')->count, 3, 'Count of prefetched m2m links via search_related' );
83   is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Amount of prefetched m2m links via search_related' );
84
85   is($queries, 0, 'No queries ran so far');
86
87   is( scalar $cd->cd_to_producer->search_related('producer')->all, 3,
88       'Amount of objects via search_related off prefetched linker' );
89   is( $cd->cd_to_producer->search_related('producer')->count, 3,
90       'Count via search_related off prefetched linker' );
91   is( scalar $cd->search_related('cd_to_producer')->search_related('producer')->all, 3,
92       'Amount of objects via chained search_related off prefetched linker' );
93   is( $cd->search_related('cd_to_producer')->search_related('producer')->count, 3,
94       'Count via chained search_related off prefetched linker' );
95   is( scalar $cd->producers->all, 3,
96       'Amount of objects via m2m accessor' );
97   is( $cd->producers->count, 3,
98       'Count via m2m accessor' );
99
100   $queries = 0;
101
102   is( $cd->cd_to_producer->count, 3 ,'Review count of prefetched m2m links via accessor' );
103   is( scalar $cd->cd_to_producer->all, 3, 'Review amount of prefetched m2m link objects via accessor' );
104   is( $cd->search_related('cd_to_producer')->count, 3, 'Review count of prefetched m2m links via search_related' );
105   is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Rreview amount of prefetched m2m links via search_related' );
106
107   is($queries, 0, 'Still no queries on prefetched linker');
108   $schema->storage->debugcb (undef);
109   $schema->storage->debug ($orig_debug);
110 }
111
112 # tests with distinct => 1
113 lives_ok (sub {
114     my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
115               ->search_related('artwork_to_artist')->search_related('artist',
116                 undef,
117                 { prefetch => 'cds' },
118               );
119     is($rs->all, 0, 'prefetch without WHERE (objects)');
120     is($rs->count, 0, 'prefetch without WHERE (count)');
121
122     $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
123               ->search_related('artwork_to_artist')->search_related('artist',
124                 { 'cds.title' => 'foo' },
125                 { prefetch => 'cds' },
126               );
127     is($rs->all, 0, 'prefetch with WHERE (objects)');
128     is($rs->count, 0, 'prefetch with WHERE (count)');
129
130
131 # test where conditions at the root of the related chain
132     my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
133     my $artist = $artist_rs->next;
134     $artist->create_related ('cds', $_) for (
135       {
136         year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
137       },
138       {
139         year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
140       },
141     );
142
143     $rs = $artist_rs->search_related('cds')->search_related('genre',
144                     { 'genre.name' => 'vague genre' },
145                     { prefetch => 'cds' },
146                  );
147     is($rs->all, 1, 'base without distinct (objects)');
148     is($rs->count, 1, 'base without distinct (count)');
149     # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
150     is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
151     is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
152
153
154     $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre',
155                     { 'genre.name' => 'vague genre' },
156                  );
157     is($rs->all, 2, 'distinct does not propagate over search_related (objects)');
158     is($rs->count, 2, 'distinct does not propagate over search_related (count)');
159
160     $rs = $rs->search ({}, { distinct => 1} );
161     is($rs->all, 1, 'distinct without prefetch (objects)');
162     is($rs->count, 1, 'distinct without prefetch (count)');
163
164
165     $rs = $artist_rs->search_related('cds')->search_related('genre',
166                     { 'genre.name' => 'vague genre' },
167                     { prefetch => 'cds', distinct => 1 },
168                  );
169     is($rs->all, 1, 'distinct with prefetch (objects)');
170     is($rs->count, 1, 'distinct with prefetch (count)');
171
172     $queries = 0;
173     $schema->storage->debugcb ($debugcb);
174     $schema->storage->debug (1);
175
176     # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
177     is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
178     is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
179
180     {
181       local $TODO = "This makes another 2 trips to the database, it can't be right";
182       is ($queries, 0, 'No extra queries fired (prefetch survives search_related)');
183     }
184
185     $schema->storage->debugcb (undef);
186     $schema->storage->debug ($orig_debug);
187 }, 'distinct generally works with prefetch on deep search_related chains');
188
189 # pathological "user knows what they're doing" case
190 # lifted from production somewhere
191 {
192   $schema->resultset('CD')
193    ->search({ cdid => [1,2] })
194     ->search_related('tracks', { position => [3,1] })
195      ->delete_all;
196
197   my $rs = $schema->resultset('CD')->search_related('tracks', {}, {
198     group_by => 'me.title',
199     columns => { title => 'me.title', max_trk => \ 'MAX(tracks.position)' },
200   });
201
202   is_deeply(
203     $rs->all_hri,
204     [
205       { title => "Caterwaulin' Blues", max_trk => 3 },
206       { title => "Come Be Depressed With Us", max_trk => 3 },
207       { title => "Forkful of bees", max_trk => 1 },
208       { title => "Generic Manufactured Singles", max_trk => 3 },
209       { title => "Spoonful of bees", max_trk => 1 },
210     ],
211     'Expected nonsense',
212   );
213 }
214
215 done_testing;