Commit | Line | Data |
b5c8410c |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
c200d949 |
6 | use Test::Warn; |
b5c8410c |
7 | use lib qw(t/lib); |
a5a7bb73 |
8 | use DBICTest ':DiffSQL'; |
b5c8410c |
9 | |
10 | my $schema = DBICTest->init_schema(); |
11 | |
abf8d91e |
12 | $schema->resultset('Artist')->delete; |
13 | $schema->resultset('CD')->delete; |
14 | |
15 | my $artist = $schema->resultset("Artist")->create({ artistid => 21, name => 'Michael Jackson', rank => 20 }); |
16 | my $artist2 = $schema->resultset("Artist")->create({ artistid => 22, name => 'Chico Buarque', rank => 1 }) ; |
17 | my $artist3 = $schema->resultset("Artist")->create({ artistid => 23, name => 'Ziraldo', rank => 1 }); |
18 | my $artist4 = $schema->resultset("Artist")->create({ artistid => 24, name => 'Paulo Caruso', rank => 20 }); |
e98e6478 |
19 | |
20 | my @artworks; |
b5c8410c |
21 | |
b5c8410c |
22 | foreach my $year (1975..1985) { |
e98e6478 |
23 | my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); |
24 | push @artworks, $cd->create_related('artwork', {}); |
b5c8410c |
25 | } |
26 | |
9aae3566 |
27 | foreach my $year (1975..1995) { |
e98e6478 |
28 | my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); |
29 | push @artworks, $cd->create_related('artwork', {}); |
30 | } |
31 | |
32 | foreach my $artwork (@artworks) { |
33 | $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4); |
9aae3566 |
34 | } |
b5c8410c |
35 | |
abf8d91e |
36 | |
7689b9e5 |
37 | my $cds_80s_rs = $artist->cds_80s; |
eb3bb737 |
38 | is_same_sql_bind( |
39 | $cds_80s_rs->as_query, |
40 | '( |
41 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track |
42 | FROM cd me |
43 | WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) ) |
44 | )', |
45 | [ |
b4e9f590 |
46 | [ |
f8193780 |
47 | {} |
b4e9f590 |
48 | => 21 |
49 | ], |
50 | [ |
51 | { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } |
52 | => 1990 |
53 | ], |
54 | [ |
55 | { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } |
56 | => 1979 |
57 | ], |
58 | ], |
eb3bb737 |
59 | ); |
7689b9e5 |
60 | my @cds_80s = $cds_80s_rs->all; |
d5a14c53 |
61 | is(@cds_80s, 6, '6 80s cds found (1980 - 1985)'); |
62 | map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; |
b5c8410c |
63 | |
7689b9e5 |
64 | |
eb3bb737 |
65 | my $cds_90s_rs = $artist2->cds_90s; |
66 | is_same_sql_bind( |
67 | $cds_90s_rs->as_query, |
68 | '( |
69 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track |
70 | FROM artist artist__row |
71 | JOIN cd me |
72 | ON ( me.artist = artist__row.artistid AND ( me.year < ? AND me.year > ? ) ) |
73 | WHERE ( artist__row.artistid = ? ) |
74 | )', |
75 | [ |
b4e9f590 |
76 | [ |
77 | { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } |
78 | => 2000 |
79 | ], |
80 | [ |
81 | { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } |
82 | => 1989 |
83 | ], |
84 | [ { sqlt_datatype => 'integer', dbic_colname => 'artist__row.artistid' } |
85 | => 22 |
86 | ], |
eb3bb737 |
87 | ] |
88 | ); |
93508f48 |
89 | |
90 | # re-test with ::-containing moniker name |
91 | # (we don't have any currently, so fudge it with lots of local() ) |
92 | { |
93 | local $schema->source('Artist')->{source_name} = 'Ar::Tist'; |
94 | local $artist2->{related_resultsets}; |
95 | |
96 | is_same_sql_bind( |
97 | $artist2->cds_90s->as_query, |
98 | '( |
99 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track |
100 | FROM artist ar_tist__row |
101 | JOIN cd me |
102 | ON ( me.artist = ar_tist__row.artistid AND ( me.year < ? AND me.year > ? ) ) |
103 | WHERE ( ar_tist__row.artistid = ? ) |
104 | )', |
105 | [ |
106 | [ |
107 | { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } |
108 | => 2000 |
109 | ], |
110 | [ |
111 | { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } |
112 | => 1989 |
113 | ], |
114 | [ { sqlt_datatype => 'integer', dbic_colname => 'ar_tist__row.artistid' } |
115 | => 22 |
116 | ], |
117 | ] |
118 | ); |
119 | } |
120 | |
121 | |
7689b9e5 |
122 | my @cds_90s = $cds_90s_rs->all; |
d5a14c53 |
123 | is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search'); |
124 | map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s; |
b5c8410c |
125 | |
eb3bb737 |
126 | lives_ok { |
127 | my @cds_90s_95 = $artist2->cds_90s->search({ 'me.year' => 1995 }); |
128 | is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search'); |
129 | map { ok($_->year == 1995) } @cds_90s_95; |
130 | } 'should preserve chain-head "me" alias (API-consistency)'; |
7689b9e5 |
131 | |
d5a14c53 |
132 | # search for all artists prefetching published cds in the 80s... |
d5a14c53 |
133 | my @all_artists_with_80_cds = $schema->resultset("Artist")->search |
134 | ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 }); |
135 | |
136 | is_deeply( |
137 | [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ], |
138 | [ sort (1980..1989, 1980..1985) ], |
139 | '16 correct cds found' |
140 | ); |
141 | |
abf8d91e |
142 | lives_ok { |
143 | |
144 | my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search |
145 | ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' }); |
146 | |
147 | is_deeply( |
148 | [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds_pref ) ], |
149 | [ sort (1980..1989, 1980..1985) ], |
150 | '16 correct cds found' |
151 | ); |
152 | |
153 | } 'prefetchy-fetchy-fetch'; |
abf8d91e |
154 | |
8c7c8398 |
155 | # create_related a plain cd via the equoivalent coderef cond, with no extra conditions |
156 | lives_ok { |
157 | $artist->create_related('cds_cref_cond', { title => 'related creation via coderef cond', year => '2010' } ); |
158 | } 'created_related with simple condition works'; |
abf8d91e |
159 | |
d5a14c53 |
160 | # try to create_related a 80s cd |
161 | throws_ok { |
162 | $artist->create_related('cds_80s', { title => 'related creation 1' }); |
209a2064 |
163 | } qr/\QUnable to complete value inferrence - custom relationship 'cds_80s' on source 'Artist' returns conditions instead of values for column(s): 'year'/, |
78b948c3 |
164 | 'Create failed - complex cond'; |
d5a14c53 |
165 | |
166 | # now supply an explicit arg overwriting the ambiguous cond |
8c7c8398 |
167 | my $cd_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' }); |
168 | my $id_2020 = $cd_2020->id; |
d5a14c53 |
169 | is( |
170 | $schema->resultset('CD')->find($id_2020)->title, |
171 | 'related creation 2', |
172 | '2020 CD created correctly' |
173 | ); |
174 | |
175 | # try a default year from a specific rel |
176 | my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id; |
177 | is( |
178 | $schema->resultset('CD')->find($id_1984)->title, |
179 | 'related creation 3', |
180 | '1984 CD created correctly' |
181 | ); |
182 | |
183 | # try a specific everything via a non-simplified rel |
184 | throws_ok { |
185 | $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' }); |
209a2064 |
186 | } qr/\QRelationship 'cds_90s' on source 'Artist' does not resolve to a join-free condition fragment/, |
78b948c3 |
187 | 'Create failed - non-simplified rel'; |
b5c8410c |
188 | |
d5a14c53 |
189 | # Do a self-join last-entry search |
c8d7a1d1 |
190 | my @last_tracks; |
b5c8410c |
191 | for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) { |
c8d7a1d1 |
192 | push @last_tracks, $cd->tracks |
193 | ->search ({}, { order_by => { -desc => 'position'} }) |
194 | ->next || (); |
b5c8410c |
195 | } |
196 | |
c8d7a1d1 |
197 | my $last_tracks_rs = $schema->resultset('Track')->search ( |
bdf6d515 |
198 | {'next_tracks.trackid' => undef}, |
199 | { join => 'next_tracks', order_by => 'me.cd' }, |
b5c8410c |
200 | ); |
201 | |
202 | is_deeply ( |
c8d7a1d1 |
203 | [$last_tracks_rs->get_column ('trackid')->all], |
204 | [ map { $_->trackid } @last_tracks ], |
b5c8410c |
205 | 'last group-entry via self-join works', |
206 | ); |
207 | |
fe4118b1 |
208 | is_deeply ( |
209 | [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid', prefetch => 'last_track'})->all], |
210 | [ map { $_->trackid } @last_tracks ], |
211 | 'last_track via insane subquery condition works', |
212 | ); |
213 | |
214 | is_deeply ( |
215 | [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all], |
216 | [ map { $_->trackid } @last_tracks ], |
217 | 'last_track via insane subquery condition works, even without prefetch', |
218 | ); |
219 | |
b79eae3b |
220 | |
e98e6478 |
221 | my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first; |
5294fdc6 |
222 | my @artists = $artwork->artists->search({}, { order_by => 'artistid' } ); |
e98e6478 |
223 | is(scalar @artists, 2, 'the two artists are associated'); |
224 | |
225 | my @artwork_artists = $artwork->artwork_to_artist->all; |
226 | foreach (@artwork_artists) { |
227 | lives_ok { |
228 | my $artista = $_->artist; |
576ae54b |
229 | my $artistb = $_->artist_limited_rank; |
e98e6478 |
230 | ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.'); |
576ae54b |
231 | my $artistc = $_->artist_limited_rank_opaque; |
e98e6478 |
232 | ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.'); |
233 | } 'belongs_to works with custom rels'; |
234 | } |
235 | |
b79eae3b |
236 | is( |
237 | $schema->resultset('Artwork') |
238 | ->related_resultset( 'artwork_to_artist_via_opaque_customcond' ) |
239 | ->related_resultset( 'artist' ) |
240 | ->search({}, { collapse => 1 }) |
241 | ->count, |
242 | 2, |
243 | 'Custom rel works correctly', |
244 | ); |
e98e6478 |
245 | |
b79eae3b |
246 | is ( |
247 | scalar $artwork->all_artists_via_opaque_customcond->all, |
248 | 2, |
249 | 'Expected two m2m associated artist objects via opaque costom cond' |
250 | ); |
e98e6478 |
251 | |
b79eae3b |
252 | for (qw( artist_limited_rank artist_limited_rank_opaque )) { |
253 | is( |
254 | $schema->resultset('Artwork') |
255 | ->related_resultset( 'artwork_to_artist_via_opaque_customcond' ) |
256 | ->related_resultset( $_ ) |
257 | ->search({}, { collapse => 1 }) |
258 | ->count, |
259 | 1, |
260 | 'Condition over double custom rel works correctly', |
261 | ); |
262 | |
263 | is ( |
264 | scalar $artwork->$_->all, |
265 | 1, |
266 | 'Expected one m2m associated artist object via opaque custom cond + conditional far cond' |
267 | ); |
5294fdc6 |
268 | |
269 | cmp_ok( |
270 | $artwork->${\"remove_from_$_"} ( $artists[1] ), |
271 | '==', |
272 | 0, |
273 | 'deletion action reports 0' |
274 | ); |
275 | |
276 | is ( |
277 | scalar $artwork->all_artists_via_opaque_customcond->all, |
278 | 2, |
279 | 'Indeed nothing was removed' |
280 | ); |
281 | |
282 | cmp_ok( |
283 | $artwork->${\"remove_from_$_"} ( $artists[0] ), |
284 | '==', |
285 | 1, |
286 | 'Removal reports correct count' |
287 | ); |
288 | |
289 | is ( |
290 | scalar $artwork->all_artists_via_opaque_customcond->all, |
291 | 1, |
292 | 'Indeed removed the matching artist' |
293 | ); |
294 | |
295 | $artwork->${\"set_$_"}([]); |
296 | |
297 | is ( |
298 | scalar $artwork->all_artists_via_opaque_customcond->all, |
299 | 0, |
300 | 'Everything removed via limited far cond' |
301 | ); |
302 | |
303 | # can't use the opaque one - need set_from_related to work |
304 | $artwork->set_artist_limited_rank( @artists ); |
305 | |
306 | { |
307 | local $TODO = 'Taking into account the relationship bridge condition is not likely to ever work... unless we get DQ hooked somehow'; |
308 | |
309 | is ( |
310 | scalar $artwork->all_artists_via_opaque_customcond->all, |
311 | 1, |
312 | 'Re-Addition passed through only one of the artists' |
313 | ); |
314 | } |
315 | |
316 | throws_ok { $artwork->set_all_artists_via_opaque_customcond( \@artists ) } |
317 | qr/\QRelationship 'artwork_to_artist_via_opaque_customcond' on source 'Artwork' does not resolve to a join-free condition fragment/; |
318 | |
319 | is ( |
320 | scalar $artwork->all_artists_via_opaque_customcond->all, |
321 | 2, |
322 | 'Everything still there as expected' |
323 | ); |
b79eae3b |
324 | } |
e98e6478 |
325 | |
326 | |
c8d7a1d1 |
327 | # Make a single for each last_track |
328 | my @singles = map { |
329 | $_->create_related('cd_single', { |
330 | title => $_->title . ' (the single)', |
331 | artist => $artist, |
332 | year => 1999, |
333 | }) } @last_tracks |
334 | ; |
335 | |
336 | # See if chaining works |
337 | is_deeply ( |
338 | [ map { $_->title } $last_tracks_rs->search_related('cd_single')->all ], |
339 | [ map { $_->title } @singles ], |
340 | 'Retrieved singles in proper order' |
341 | ); |
342 | |
343 | # See if prefetch works |
344 | is_deeply ( |
345 | [ map { $_->cd_single->title } $last_tracks_rs->search({}, { prefetch => 'cd_single' })->all ], |
346 | [ map { $_->title } @singles ], |
347 | 'Prefetched singles in proper order' |
348 | ); |
349 | |
e884e5d9 |
350 | # test set_from_related/find_related with a belongs_to custom condition |
351 | my $preexisting_cd = $schema->resultset('CD')->find(1); |
352 | |
353 | my $cd_single_track = $schema->resultset('CD')->create({ |
354 | artist => $artist, |
355 | title => 'one one one', |
356 | year => 2001, |
357 | tracks => [{ title => 'uno uno uno' }] |
358 | }); |
359 | |
360 | my $single_track = $cd_single_track->tracks->next; |
361 | |
7df2b5df |
362 | is( |
363 | $single_track->cd_cref_cond->title, |
364 | $cd_single_track->title, |
365 | 'Got back the expected single-track cd title', |
366 | ); |
367 | |
e884e5d9 |
368 | is_deeply |
369 | { $schema->resultset('Track')->find({ cd_cref_cond => { cdid => $cd_single_track->id } })->get_columns }, |
370 | { $single_track->get_columns }, |
371 | 'Proper find with related via coderef cond', |
372 | ; |
373 | |
c200d949 |
374 | warnings_exist { |
375 | is_same_sql_bind( |
376 | $single_track->deliberately_broken_all_cd_tracks->as_query, |
377 | '( |
378 | SELECT me.trackid, me.cd, me.position, me.title, me.last_updated_on, me.last_updated_at |
379 | FROM track track__row |
380 | JOIN track me |
381 | ON me.cd = ? |
382 | WHERE track__row.trackid = ? |
383 | )', |
384 | [ |
385 | [{ dbic_colname => "me.cd", sqlt_datatype => "integer" } |
386 | => "track__row.cd" ], |
387 | [{ dbic_colname => "track__row.trackid", sqlt_datatype => "integer" } |
388 | => 19 ], |
389 | ], |
390 | 'Expected nonsensical JOIN cond', |
391 | ), |
392 | } qr/\Qrelationship 'deliberately_broken_all_cd_tracks' on source 'Track' specifies equality of column 'cd' and the *VALUE* 'cd' (you did not use the { -ident => ... } operator)/, |
393 | 'Warning on 99.9999% malformed custom cond' |
394 | ; |
395 | |
e884e5d9 |
396 | $single_track->set_from_related( cd_cref_cond => undef ); |
397 | ok $single_track->is_column_changed('cd'); |
398 | is $single_track->get_column('cd'), undef, 'UNset from related via coderef cond'; |
399 | is $single_track->cd, undef, 'UNset related object via coderef cond'; |
400 | |
401 | $single_track->discard_changes; |
402 | |
403 | $single_track->set_from_related( cd_cref_cond => $preexisting_cd ); |
404 | ok $single_track->is_column_changed('cd'); |
405 | is $single_track->get_column('cd'), 1, 'set from related via coderef cond'; |
406 | is_deeply |
407 | { $single_track->cd->get_columns }, |
408 | { $preexisting_cd->get_columns }, |
8c7c8398 |
409 | 'set from related via coderef cond inflates properly', |
e884e5d9 |
410 | ; |
8c7c8398 |
411 | |
7967dcec |
412 | throws_ok { |
413 | local $schema->source('Track')->relationship_info('cd_cref_cond')->{cond} = sub { 1,2,3 }; |
414 | $schema->resultset('Track')->find({ cd_cref_cond => {} }); |
415 | } qr/\QA custom condition coderef can return at most 2 conditions, but relationship 'cd_cref_cond' on source 'Track' returned extra values: 3/; |
416 | |
b5c8410c |
417 | done_testing; |