Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
2 | |
5fc1d686 |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
7 | use Test::Exception; |
8 | |
c0329273 |
9 | |
5fc1d686 |
10 | use DBICTest; |
11 | |
12 | my $schema = DBICTest->init_schema(); |
13 | |
e711f316 |
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 | |
340926fd |
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 | |
340926fd |
61 | is( |
62 | scalar ($use_prefetch->all), |
63 | scalar ($no_prefetch->all), |
64 | "Amount of returned rows is right" |
65 | ); |
36b59369 |
66 | is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match'); |
340926fd |
67 | |
68 | }, 'search_related prefetch with condition referencing unqualified column of a joined table works'); |
340926fd |
69 | |
bdb4d940 |
70 | # make sure chains off prefetched results still work |
71 | { |
72 | my $cd = $schema->resultset('CD')->search({}, { prefetch => 'cd_to_producer' })->find(1); |
73 | |
49eeb48d |
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'); |
bdb4d940 |
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 | |
49eeb48d |
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'); |
bdb4d940 |
100 | } |
101 | |
102 | # tests with distinct => 1 |
5fc1d686 |
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 |
5e8cb53c |
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 | ); |
5fc1d686 |
132 | |
133 | $rs = $artist_rs->search_related('cds')->search_related('genre', |
5e8cb53c |
134 | { 'genre.name' => 'vague genre' }, |
5fc1d686 |
135 | { prefetch => 'cds' }, |
136 | ); |
5e8cb53c |
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)'); |
5fc1d686 |
142 | |
143 | |
d992eae8 |
144 | $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre', |
5e8cb53c |
145 | { 'genre.name' => 'vague genre' }, |
5fc1d686 |
146 | ); |
d992eae8 |
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} ); |
5e8cb53c |
151 | is($rs->all, 1, 'distinct without prefetch (objects)'); |
152 | is($rs->count, 1, 'distinct without prefetch (count)'); |
5fc1d686 |
153 | |
154 | |
d992eae8 |
155 | $rs = $artist_rs->search_related('cds')->search_related('genre', |
5e8cb53c |
156 | { 'genre.name' => 'vague genre' }, |
d992eae8 |
157 | { prefetch => 'cds', distinct => 1 }, |
5fc1d686 |
158 | ); |
5e8cb53c |
159 | is($rs->all, 1, 'distinct with prefetch (objects)'); |
160 | is($rs->count, 1, 'distinct with prefetch (count)'); |
d992eae8 |
161 | |
49eeb48d |
162 | local $TODO = "This makes another 2 trips to the database, it can't be right"; |
163 | $schema->is_executed_querycount( sub { |
b9df8e39 |
164 | |
49eeb48d |
165 | # the is() calls are not todoified |
166 | local $TODO; |
b9df8e39 |
167 | |
49eeb48d |
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)'); |
b9df8e39 |
173 | |
e711f316 |
174 | }, 'distinct generally works with prefetch on deep search_related chains'); |
5fc1d686 |
175 | |
90f10b5a |
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( |
1b658919 |
190 | $rs->search({}, { order_by => 'me.title' })->all_hri, |
90f10b5a |
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 | |
5fc1d686 |
202 | done_testing; |