Add reference to RT for the fix from d0cefd99
[dbsrgits/DBIx-Class.git] / t / relationship / custom.t
CommitLineData
b5c8410c 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
a5a7bb73 7use DBICTest ':DiffSQL';
b5c8410c 8
9my $schema = DBICTest->init_schema();
10
abf8d91e 11$schema->resultset('Artist')->delete;
12$schema->resultset('CD')->delete;
13
14my $artist = $schema->resultset("Artist")->create({ artistid => 21, name => 'Michael Jackson', rank => 20 });
15my $artist2 = $schema->resultset("Artist")->create({ artistid => 22, name => 'Chico Buarque', rank => 1 }) ;
16my $artist3 = $schema->resultset("Artist")->create({ artistid => 23, name => 'Ziraldo', rank => 1 });
17my $artist4 = $schema->resultset("Artist")->create({ artistid => 24, name => 'Paulo Caruso', rank => 20 });
e98e6478 18
19my @artworks;
b5c8410c 20
b5c8410c 21foreach my $year (1975..1985) {
e98e6478 22 my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
23 push @artworks, $cd->create_related('artwork', {});
b5c8410c 24}
25
9aae3566 26foreach my $year (1975..1995) {
e98e6478 27 my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
28 push @artworks, $cd->create_related('artwork', {});
29}
30
31foreach my $artwork (@artworks) {
32 $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
9aae3566 33}
b5c8410c 34
abf8d91e 35
7689b9e5 36my $cds_80s_rs = $artist->cds_80s;
eb3bb737 37is_same_sql_bind(
38 $cds_80s_rs->as_query,
39 '(
40 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
41 FROM cd me
42 WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) )
43 )',
44 [
b4e9f590 45 [
f8193780 46 {}
b4e9f590 47 => 21
48 ],
49 [
50 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
51 => 1990
52 ],
53 [
54 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
55 => 1979
56 ],
57 ],
eb3bb737 58);
7689b9e5 59my @cds_80s = $cds_80s_rs->all;
d5a14c53 60is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
61map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
b5c8410c 62
7689b9e5 63
eb3bb737 64my $cds_90s_rs = $artist2->cds_90s;
65is_same_sql_bind(
66 $cds_90s_rs->as_query,
67 '(
68 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
69 FROM artist artist__row
70 JOIN cd me
71 ON ( me.artist = artist__row.artistid AND ( me.year < ? AND me.year > ? ) )
72 WHERE ( artist__row.artistid = ? )
73 )',
74 [
b4e9f590 75 [
76 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
77 => 2000
78 ],
79 [
80 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
81 => 1989
82 ],
83 [ { sqlt_datatype => 'integer', dbic_colname => 'artist__row.artistid' }
84 => 22
85 ],
eb3bb737 86 ]
87);
93508f48 88
89# re-test with ::-containing moniker name
90# (we don't have any currently, so fudge it with lots of local() )
91{
92 local $schema->source('Artist')->{source_name} = 'Ar::Tist';
93 local $artist2->{related_resultsets};
94
95 is_same_sql_bind(
96 $artist2->cds_90s->as_query,
97 '(
98 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
99 FROM artist ar_tist__row
100 JOIN cd me
101 ON ( me.artist = ar_tist__row.artistid AND ( me.year < ? AND me.year > ? ) )
102 WHERE ( ar_tist__row.artistid = ? )
103 )',
104 [
105 [
106 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
107 => 2000
108 ],
109 [
110 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
111 => 1989
112 ],
113 [ { sqlt_datatype => 'integer', dbic_colname => 'ar_tist__row.artistid' }
114 => 22
115 ],
116 ]
117 );
118}
119
120
7689b9e5 121my @cds_90s = $cds_90s_rs->all;
d5a14c53 122is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
123map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
b5c8410c 124
eb3bb737 125lives_ok {
126 my @cds_90s_95 = $artist2->cds_90s->search({ 'me.year' => 1995 });
127 is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search');
128 map { ok($_->year == 1995) } @cds_90s_95;
129} 'should preserve chain-head "me" alias (API-consistency)';
7689b9e5 130
d5a14c53 131# search for all artists prefetching published cds in the 80s...
d5a14c53 132my @all_artists_with_80_cds = $schema->resultset("Artist")->search
133 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
134
135is_deeply(
136 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
137 [ sort (1980..1989, 1980..1985) ],
138 '16 correct cds found'
139);
140
abf8d91e 141lives_ok {
142
143my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search
144 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' });
145
146is_deeply(
147 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds_pref ) ],
148 [ sort (1980..1989, 1980..1985) ],
149 '16 correct cds found'
150);
151
152} 'prefetchy-fetchy-fetch';
abf8d91e 153
8c7c8398 154# create_related a plain cd via the equoivalent coderef cond, with no extra conditions
155lives_ok {
156 $artist->create_related('cds_cref_cond', { title => 'related creation via coderef cond', year => '2010' } );
157} 'created_related with simple condition works';
abf8d91e 158
d5a14c53 159# try to create_related a 80s cd
160throws_ok {
161 $artist->create_related('cds_80s', { title => 'related creation 1' });
209a2064 162} qr/\QUnable to complete value inferrence - custom relationship 'cds_80s' on source 'Artist' returns conditions instead of values for column(s): 'year'/,
78b948c3 163'Create failed - complex cond';
d5a14c53 164
165# now supply an explicit arg overwriting the ambiguous cond
8c7c8398 166my $cd_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' });
167my $id_2020 = $cd_2020->id;
d5a14c53 168is(
169 $schema->resultset('CD')->find($id_2020)->title,
170 'related creation 2',
171 '2020 CD created correctly'
172);
173
174# try a default year from a specific rel
175my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
176is(
177 $schema->resultset('CD')->find($id_1984)->title,
178 'related creation 3',
179 '1984 CD created correctly'
180);
181
182# try a specific everything via a non-simplified rel
183throws_ok {
184 $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
209a2064 185} qr/\QRelationship 'cds_90s' on source 'Artist' does not resolve to a join-free condition fragment/,
78b948c3 186'Create failed - non-simplified rel';
b5c8410c 187
d5a14c53 188# Do a self-join last-entry search
c8d7a1d1 189my @last_tracks;
b5c8410c 190for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
c8d7a1d1 191 push @last_tracks, $cd->tracks
192 ->search ({}, { order_by => { -desc => 'position'} })
193 ->next || ();
b5c8410c 194}
195
c8d7a1d1 196my $last_tracks_rs = $schema->resultset('Track')->search (
bdf6d515 197 {'next_tracks.trackid' => undef},
198 { join => 'next_tracks', order_by => 'me.cd' },
b5c8410c 199);
200
201is_deeply (
c8d7a1d1 202 [$last_tracks_rs->get_column ('trackid')->all],
203 [ map { $_->trackid } @last_tracks ],
b5c8410c 204 'last group-entry via self-join works',
205);
206
fe4118b1 207is_deeply (
208 [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid', prefetch => 'last_track'})->all],
209 [ map { $_->trackid } @last_tracks ],
210 'last_track via insane subquery condition works',
211);
212
213is_deeply (
214 [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all],
215 [ map { $_->trackid } @last_tracks ],
216 'last_track via insane subquery condition works, even without prefetch',
217);
218
e98e6478 219my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
220my @artists = $artwork->artists->all;
221is(scalar @artists, 2, 'the two artists are associated');
222
223my @artwork_artists = $artwork->artwork_to_artist->all;
224foreach (@artwork_artists) {
225 lives_ok {
226 my $artista = $_->artist;
227 my $artistb = $_->artist_test_m2m;
228 ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.');
229 my $artistc = $_->artist_test_m2m_noopt;
230 ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.');
231 } 'belongs_to works with custom rels';
232}
233
234@artists = ();
235lives_ok {
236 @artists = $artwork->artists_test_m2m2->all;
237} 'manytomany with extended rels in the has many works';
238is(scalar @artists, 2, 'two artists');
239
240@artists = ();
241lives_ok {
242 @artists = $artwork->artists_test_m2m->all;
243} 'can fetch many to many with optimized version';
244is(scalar @artists, 1, 'only one artist is associated');
245
246@artists = ();
247lives_ok {
248 @artists = $artwork->artists_test_m2m_noopt->all;
249} 'can fetch many to many with non-optimized version';
250is(scalar @artists, 1, 'only one artist is associated');
251
252
c8d7a1d1 253# Make a single for each last_track
254my @singles = map {
255 $_->create_related('cd_single', {
256 title => $_->title . ' (the single)',
257 artist => $artist,
258 year => 1999,
259 }) } @last_tracks
260;
261
262# See if chaining works
263is_deeply (
264 [ map { $_->title } $last_tracks_rs->search_related('cd_single')->all ],
265 [ map { $_->title } @singles ],
266 'Retrieved singles in proper order'
267);
268
269# See if prefetch works
270is_deeply (
271 [ map { $_->cd_single->title } $last_tracks_rs->search({}, { prefetch => 'cd_single' })->all ],
272 [ map { $_->title } @singles ],
273 'Prefetched singles in proper order'
274);
275
e884e5d9 276# test set_from_related/find_related with a belongs_to custom condition
277my $preexisting_cd = $schema->resultset('CD')->find(1);
278
279my $cd_single_track = $schema->resultset('CD')->create({
280 artist => $artist,
281 title => 'one one one',
282 year => 2001,
283 tracks => [{ title => 'uno uno uno' }]
284});
285
286my $single_track = $cd_single_track->tracks->next;
287
288is_deeply
289 { $schema->resultset('Track')->find({ cd_cref_cond => { cdid => $cd_single_track->id } })->get_columns },
290 { $single_track->get_columns },
291 'Proper find with related via coderef cond',
292;
293
294$single_track->set_from_related( cd_cref_cond => undef );
295ok $single_track->is_column_changed('cd');
296is $single_track->get_column('cd'), undef, 'UNset from related via coderef cond';
297is $single_track->cd, undef, 'UNset related object via coderef cond';
298
299$single_track->discard_changes;
300
301$single_track->set_from_related( cd_cref_cond => $preexisting_cd );
302ok $single_track->is_column_changed('cd');
303is $single_track->get_column('cd'), 1, 'set from related via coderef cond';
304is_deeply
305 { $single_track->cd->get_columns },
306 { $preexisting_cd->get_columns },
8c7c8398 307 'set from related via coderef cond inflates properly',
e884e5d9 308;
8c7c8398 309
b5c8410c 310done_testing;