7 use DBICTest ':DiffSQL';
9 my $schema = DBICTest->init_schema();
11 $schema->resultset('Artist')->delete;
12 $schema->resultset('CD')->delete;
14 my $artist = $schema->resultset("Artist")->create({ artistid => 21, name => 'Michael Jackson', rank => 20 });
15 my $artist2 = $schema->resultset("Artist")->create({ artistid => 22, name => 'Chico Buarque', rank => 1 }) ;
16 my $artist3 = $schema->resultset("Artist")->create({ artistid => 23, name => 'Ziraldo', rank => 1 });
17 my $artist4 = $schema->resultset("Artist")->create({ artistid => 24, name => 'Paulo Caruso', rank => 20 });
21 foreach my $year (1975..1985) {
22 my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
23 push @artworks, $cd->create_related('artwork', {});
26 foreach my $year (1975..1995) {
27 my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
28 push @artworks, $cd->create_related('artwork', {});
31 foreach my $artwork (@artworks) {
32 $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
36 my $cds_80s_rs = $artist->cds_80s;
38 $cds_80s_rs->as_query,
40 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
42 WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) )
50 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
54 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
59 my @cds_80s = $cds_80s_rs->all;
60 is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
61 map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
64 my $cds_90s_rs = $artist2->cds_90s;
66 $cds_90s_rs->as_query,
68 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
69 FROM artist artist__row
71 ON ( me.artist = artist__row.artistid AND ( me.year < ? AND me.year > ? ) )
72 WHERE ( artist__row.artistid = ? )
76 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
80 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
83 [ { sqlt_datatype => 'integer', dbic_colname => 'artist__row.artistid' }
89 # re-test with ::-containing moniker name
90 # (we don't have any currently, so fudge it with lots of local() )
92 local $schema->source('Artist')->{source_name} = 'Ar::Tist';
93 local $artist2->{related_resultsets};
96 $artist2->cds_90s->as_query,
98 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
99 FROM artist ar_tist__row
101 ON ( me.artist = ar_tist__row.artistid AND ( me.year < ? AND me.year > ? ) )
102 WHERE ( ar_tist__row.artistid = ? )
106 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
110 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
113 [ { sqlt_datatype => 'integer', dbic_colname => 'ar_tist__row.artistid' }
121 my @cds_90s = $cds_90s_rs->all;
122 is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
123 map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
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)';
131 # search for all artists prefetching published cds in the 80s...
132 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
133 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
136 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
137 [ sort (1980..1989, 1980..1985) ],
138 '16 correct cds found'
143 my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search
144 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' });
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'
152 } 'prefetchy-fetchy-fetch';
154 # create_related a plain cd via the equoivalent coderef cond, with no extra conditions
156 $artist->create_related('cds_cref_cond', { title => 'related creation via coderef cond', year => '2010' } );
157 } 'created_related with simple condition works';
159 # try to create_related a 80s cd
161 $artist->create_related('cds_80s', { title => 'related creation 1' });
162 } qr/\QCustom relationship 'cds_80s' not definitive - returns conditions instead of values for column(s): 'year'/,
163 'Create failed - complex cond';
165 # now supply an explicit arg overwriting the ambiguous cond
166 my $cd_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' });
167 my $id_2020 = $cd_2020->id;
169 $schema->resultset('CD')->find($id_2020)->title,
170 'related creation 2',
171 '2020 CD created correctly'
174 # try a default year from a specific rel
175 my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
177 $schema->resultset('CD')->find($id_1984)->title,
178 'related creation 3',
179 '1984 CD created correctly'
182 # try a specific everything via a non-simplified rel
184 $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
185 } qr/\QRelationship 'cds_90s' does not resolve to a join-free condition fragment/,
186 'Create failed - non-simplified rel';
188 # Do a self-join last-entry search
190 for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
191 push @last_tracks, $cd->tracks
192 ->search ({}, { order_by => { -desc => 'position'} })
196 my $last_tracks_rs = $schema->resultset('Track')->search (
197 {'next_tracks.trackid' => undef},
198 { join => 'next_tracks', order_by => 'me.cd' },
202 [$last_tracks_rs->get_column ('trackid')->all],
203 [ map { $_->trackid } @last_tracks ],
204 'last group-entry via self-join works',
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',
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',
219 my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
220 my @artists = $artwork->artists->all;
221 is(scalar @artists, 2, 'the two artists are associated');
223 my @artwork_artists = $artwork->artwork_to_artist->all;
224 foreach (@artwork_artists) {
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';
236 @artists = $artwork->artists_test_m2m2->all;
237 } 'manytomany with extended rels in the has many works';
238 is(scalar @artists, 2, 'two artists');
242 @artists = $artwork->artists_test_m2m->all;
243 } 'can fetch many to many with optimized version';
244 is(scalar @artists, 1, 'only one artist is associated');
248 @artists = $artwork->artists_test_m2m_noopt->all;
249 } 'can fetch many to many with non-optimized version';
250 is(scalar @artists, 1, 'only one artist is associated');
253 # Make a single for each last_track
255 $_->create_related('cd_single', {
256 title => $_->title . ' (the single)',
262 # See if chaining works
264 [ map { $_->title } $last_tracks_rs->search_related('cd_single')->all ],
265 [ map { $_->title } @singles ],
266 'Retrieved singles in proper order'
269 # See if prefetch works
271 [ map { $_->cd_single->title } $last_tracks_rs->search({}, { prefetch => 'cd_single' })->all ],
272 [ map { $_->title } @singles ],
273 'Prefetched singles in proper order'
276 # test set_from_related with a belongs_to custom condition
277 my $cd = $schema->resultset("CD")->find(4);
278 $artist = $cd->search_related('artist');
279 my $track = $schema->resultset("Track")->create( {
283 title => 'Some Track'
285 $track->set_from_related( cd_cref_cond => $cd );
286 is ($track->get_column('cd'), 4, 'set from related via coderef cond');
288 { $track->cd->get_columns },
289 { $cd->get_columns },
290 'set from related via coderef cond inflates properly',