Rewrite the check for nontrivial joinfree conditions to throw on both new_related...
[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);
7use DBICTest;
7689b9e5 8use DBIC::SqlMakerTest;
b5c8410c 9
10my $schema = DBICTest->init_schema();
11
abf8d91e 12$schema->resultset('Artist')->delete;
13$schema->resultset('CD')->delete;
14
15my $artist = $schema->resultset("Artist")->create({ artistid => 21, name => 'Michael Jackson', rank => 20 });
16my $artist2 = $schema->resultset("Artist")->create({ artistid => 22, name => 'Chico Buarque', rank => 1 }) ;
17my $artist3 = $schema->resultset("Artist")->create({ artistid => 23, name => 'Ziraldo', rank => 1 });
18my $artist4 = $schema->resultset("Artist")->create({ artistid => 24, name => 'Paulo Caruso', rank => 20 });
e98e6478 19
20my @artworks;
b5c8410c 21
b5c8410c 22foreach 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 27foreach 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
32foreach my $artwork (@artworks) {
33 $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
9aae3566 34}
b5c8410c 35
abf8d91e 36
7689b9e5 37my $cds_80s_rs = $artist->cds_80s;
eb3bb737 38is_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 [
46 [ 'me.artist' => 21 ],
47 [ 'me.year' => 1990 ],
48 [ 'me.year' => 1979 ],
49 ]
50);
7689b9e5 51my @cds_80s = $cds_80s_rs->all;
d5a14c53 52is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
53map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
b5c8410c 54
7689b9e5 55
eb3bb737 56my $cds_90s_rs = $artist2->cds_90s;
57is_same_sql_bind(
58 $cds_90s_rs->as_query,
59 '(
60 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
61 FROM artist artist__row
62 JOIN cd me
63 ON ( me.artist = artist__row.artistid AND ( me.year < ? AND me.year > ? ) )
64 WHERE ( artist__row.artistid = ? )
65 )',
66 [
67 [ 'me.year' => 2000 ],
68 [ 'me.year' => 1989 ],
69 [ 'artist__row.artistid' => 22 ],
70 ]
71);
7689b9e5 72my @cds_90s = $cds_90s_rs->all;
d5a14c53 73is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
74map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
b5c8410c 75
eb3bb737 76lives_ok {
77 my @cds_90s_95 = $artist2->cds_90s->search({ 'me.year' => 1995 });
78 is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search');
79 map { ok($_->year == 1995) } @cds_90s_95;
80} 'should preserve chain-head "me" alias (API-consistency)';
7689b9e5 81
d5a14c53 82# search for all artists prefetching published cds in the 80s...
d5a14c53 83my @all_artists_with_80_cds = $schema->resultset("Artist")->search
84 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
85
86is_deeply(
87 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
88 [ sort (1980..1989, 1980..1985) ],
89 '16 correct cds found'
90);
91
abf8d91e 92TODO: {
93local $TODO = 'Prefetch on custom rels can not work until the collapse rewrite is finished '
94 . '(currently collapser requires a right-side (which is indeterministic) order-by)';
95lives_ok {
96
97my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search
98 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' });
99
100is_deeply(
101 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds_pref ) ],
102 [ sort (1980..1989, 1980..1985) ],
103 '16 correct cds found'
104);
105
106} 'prefetchy-fetchy-fetch';
107} # end of TODO
108
109
d5a14c53 110# try to create_related a 80s cd
111throws_ok {
112 $artist->create_related('cds_80s', { title => 'related creation 1' });
78b948c3 113} qr/\QCustom relationship 'cds_80s' not definitive - returns conditions instead of values for column(s): 'year'/,
114'Create failed - complex cond';
d5a14c53 115
116# now supply an explicit arg overwriting the ambiguous cond
117my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
118is(
119 $schema->resultset('CD')->find($id_2020)->title,
120 'related creation 2',
121 '2020 CD created correctly'
122);
123
124# try a default year from a specific rel
125my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
126is(
127 $schema->resultset('CD')->find($id_1984)->title,
128 'related creation 3',
129 '1984 CD created correctly'
130);
131
132# try a specific everything via a non-simplified rel
133throws_ok {
134 $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
78b948c3 135} qr/\QCustom relationship 'cds_90s' does not resolve to a join-free condition fragment/,
136'Create failed - non-simplified rel';
b5c8410c 137
d5a14c53 138# Do a self-join last-entry search
c8d7a1d1 139my @last_tracks;
b5c8410c 140for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
c8d7a1d1 141 push @last_tracks, $cd->tracks
142 ->search ({}, { order_by => { -desc => 'position'} })
143 ->next || ();
b5c8410c 144}
145
c8d7a1d1 146my $last_tracks_rs = $schema->resultset('Track')->search (
b5c8410c 147 {'next_track.trackid' => undef},
148 { join => 'next_track', order_by => 'me.cd' },
149);
150
151is_deeply (
c8d7a1d1 152 [$last_tracks_rs->get_column ('trackid')->all],
153 [ map { $_->trackid } @last_tracks ],
b5c8410c 154 'last group-entry via self-join works',
155);
156
e98e6478 157my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
158my @artists = $artwork->artists->all;
159is(scalar @artists, 2, 'the two artists are associated');
160
161my @artwork_artists = $artwork->artwork_to_artist->all;
162foreach (@artwork_artists) {
163 lives_ok {
164 my $artista = $_->artist;
165 my $artistb = $_->artist_test_m2m;
166 ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.');
167 my $artistc = $_->artist_test_m2m_noopt;
168 ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.');
169 } 'belongs_to works with custom rels';
170}
171
172@artists = ();
173lives_ok {
174 @artists = $artwork->artists_test_m2m2->all;
175} 'manytomany with extended rels in the has many works';
176is(scalar @artists, 2, 'two artists');
177
178@artists = ();
179lives_ok {
180 @artists = $artwork->artists_test_m2m->all;
181} 'can fetch many to many with optimized version';
182is(scalar @artists, 1, 'only one artist is associated');
183
184@artists = ();
185lives_ok {
186 @artists = $artwork->artists_test_m2m_noopt->all;
187} 'can fetch many to many with non-optimized version';
188is(scalar @artists, 1, 'only one artist is associated');
189
190
c8d7a1d1 191# Make a single for each last_track
192my @singles = map {
193 $_->create_related('cd_single', {
194 title => $_->title . ' (the single)',
195 artist => $artist,
196 year => 1999,
197 }) } @last_tracks
198;
199
200# See if chaining works
201is_deeply (
202 [ map { $_->title } $last_tracks_rs->search_related('cd_single')->all ],
203 [ map { $_->title } @singles ],
204 'Retrieved singles in proper order'
205);
206
207# See if prefetch works
208is_deeply (
209 [ map { $_->cd_single->title } $last_tracks_rs->search({}, { prefetch => 'cd_single' })->all ],
210 [ map { $_->title } @singles ],
211 'Prefetched singles in proper order'
212);
213
b5c8410c 214done_testing;