8 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema();
12 $schema->resultset('Artist')->delete;
13 $schema->resultset('CD')->delete;
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 });
22 foreach my $year (1975..1985) {
23 my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
24 push @artworks, $cd->create_related('artwork', {});
27 foreach my $year (1975..1995) {
28 my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
29 push @artworks, $cd->create_related('artwork', {});
32 foreach my $artwork (@artworks) {
33 $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
37 my $cds_80s_rs = $artist->cds_80s;
39 $cds_80s_rs->as_query,
41 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
43 WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) )
47 { sqlt_datatype => 'integer', dbic_colname => 'me.artist' }
51 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
55 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
60 my @cds_80s = $cds_80s_rs->all;
61 is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
62 map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
65 my $cds_90s_rs = $artist2->cds_90s;
67 $cds_90s_rs->as_query,
69 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
70 FROM artist artist__row
72 ON ( me.artist = artist__row.artistid AND ( me.year < ? AND me.year > ? ) )
73 WHERE ( artist__row.artistid = ? )
77 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
81 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' }
84 [ { sqlt_datatype => 'integer', dbic_colname => 'artist__row.artistid' }
89 my @cds_90s = $cds_90s_rs->all;
90 is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
91 map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
94 my @cds_90s_95 = $artist2->cds_90s->search({ 'me.year' => 1995 });
95 is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search');
96 map { ok($_->year == 1995) } @cds_90s_95;
97 } 'should preserve chain-head "me" alias (API-consistency)';
99 # search for all artists prefetching published cds in the 80s...
100 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
101 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
104 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
105 [ sort (1980..1989, 1980..1985) ],
106 '16 correct cds found'
110 local $TODO = 'Prefetch on custom rels can not work until the collapse rewrite is finished '
111 . '(currently collapser requires a right-side (which is indeterministic) order-by)';
114 my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search
115 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' });
118 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds_pref ) ],
119 [ sort (1980..1989, 1980..1985) ],
120 '16 correct cds found'
123 } 'prefetchy-fetchy-fetch';
127 # try to create_related a 80s cd
129 $artist->create_related('cds_80s', { title => 'related creation 1' });
130 } qr/\QCustom relationship 'cds_80s' not definitive - returns conditions instead of values for column(s): 'year'/,
131 'Create failed - complex cond';
133 # now supply an explicit arg overwriting the ambiguous cond
134 my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
136 $schema->resultset('CD')->find($id_2020)->title,
137 'related creation 2',
138 '2020 CD created correctly'
141 # try a default year from a specific rel
142 my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
144 $schema->resultset('CD')->find($id_1984)->title,
145 'related creation 3',
146 '1984 CD created correctly'
149 # try a specific everything via a non-simplified rel
151 $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
152 } qr/\QCustom relationship 'cds_90s' does not resolve to a join-free condition fragment/,
153 'Create failed - non-simplified rel';
155 # Do a self-join last-entry search
157 for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
158 push @last_tracks, $cd->tracks
159 ->search ({}, { order_by => { -desc => 'position'} })
163 my $last_tracks_rs = $schema->resultset('Track')->search (
164 {'next_track.trackid' => undef},
165 { join => 'next_track', order_by => 'me.cd' },
169 [$last_tracks_rs->get_column ('trackid')->all],
170 [ map { $_->trackid } @last_tracks ],
171 'last group-entry via self-join works',
174 my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
175 my @artists = $artwork->artists->all;
176 is(scalar @artists, 2, 'the two artists are associated');
178 my @artwork_artists = $artwork->artwork_to_artist->all;
179 foreach (@artwork_artists) {
181 my $artista = $_->artist;
182 my $artistb = $_->artist_test_m2m;
183 ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.');
184 my $artistc = $_->artist_test_m2m_noopt;
185 ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.');
186 } 'belongs_to works with custom rels';
191 @artists = $artwork->artists_test_m2m2->all;
192 } 'manytomany with extended rels in the has many works';
193 is(scalar @artists, 2, 'two artists');
197 @artists = $artwork->artists_test_m2m->all;
198 } 'can fetch many to many with optimized version';
199 is(scalar @artists, 1, 'only one artist is associated');
203 @artists = $artwork->artists_test_m2m_noopt->all;
204 } 'can fetch many to many with non-optimized version';
205 is(scalar @artists, 1, 'only one artist is associated');
208 # Make a single for each last_track
210 $_->create_related('cd_single', {
211 title => $_->title . ' (the single)',
217 # See if chaining works
219 [ map { $_->title } $last_tracks_rs->search_related('cd_single')->all ],
220 [ map { $_->title } @singles ],
221 'Retrieved singles in proper order'
224 # See if prefetch works
226 [ map { $_->cd_single->title } $last_tracks_rs->search({}, { prefetch => 'cd_single' })->all ],
227 [ map { $_->title } @singles ],
228 'Prefetched singles in proper order'