Commit | Line | Data |
b5c8410c |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
7689b9e5 |
8 | use DBIC::SqlMakerTest; |
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 | [ |
47 | { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } |
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 | |
c9733800 |
142 | TODO: { |
143 | local $TODO = 'Prefetch on custom rels can not work until the collapse rewrite is finished ' |
144 | . '(currently collapser requires a right-side (which is indeterministic) order-by)'; |
abf8d91e |
145 | lives_ok { |
146 | |
147 | my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search |
148 | ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' }); |
149 | |
150 | is_deeply( |
151 | [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds_pref ) ], |
152 | [ sort (1980..1989, 1980..1985) ], |
153 | '16 correct cds found' |
154 | ); |
155 | |
156 | } 'prefetchy-fetchy-fetch'; |
c9733800 |
157 | } # end of TODO |
abf8d91e |
158 | |
159 | |
d5a14c53 |
160 | # try to create_related a 80s cd |
161 | throws_ok { |
162 | $artist->create_related('cds_80s', { title => 'related creation 1' }); |
78b948c3 |
163 | } qr/\QCustom relationship 'cds_80s' not definitive - returns conditions instead of values for column(s): 'year'/, |
164 | 'Create failed - complex cond'; |
d5a14c53 |
165 | |
166 | # now supply an explicit arg overwriting the ambiguous cond |
167 | my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id; |
168 | is( |
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 |
175 | my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id; |
176 | is( |
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 |
183 | throws_ok { |
184 | $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' }); |
78b948c3 |
185 | } qr/\QCustom relationship 'cds_90s' does not resolve to a join-free condition fragment/, |
186 | 'Create failed - non-simplified rel'; |
b5c8410c |
187 | |
d5a14c53 |
188 | # Do a self-join last-entry search |
c8d7a1d1 |
189 | my @last_tracks; |
b5c8410c |
190 | for 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 |
196 | my $last_tracks_rs = $schema->resultset('Track')->search ( |
bdf6d515 |
197 | {'next_tracks.trackid' => undef}, |
198 | { join => 'next_tracks', order_by => 'me.cd' }, |
b5c8410c |
199 | ); |
200 | |
201 | is_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 |
207 | is_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 | |
213 | is_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 |
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'); |
222 | |
223 | my @artwork_artists = $artwork->artwork_to_artist->all; |
224 | foreach (@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 = (); |
235 | lives_ok { |
236 | @artists = $artwork->artists_test_m2m2->all; |
237 | } 'manytomany with extended rels in the has many works'; |
238 | is(scalar @artists, 2, 'two artists'); |
239 | |
240 | @artists = (); |
241 | lives_ok { |
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'); |
245 | |
246 | @artists = (); |
247 | lives_ok { |
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'); |
251 | |
252 | |
c8d7a1d1 |
253 | # Make a single for each last_track |
254 | my @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 |
263 | is_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 |
270 | is_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 | |
b5c8410c |
276 | done_testing; |