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