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