Add some extra tests written while debugging, remove design draft
[dbsrgits/DBIx-Class.git] / t / relationship / custom.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use DBIC::SqlMakerTest;
9
10 my $schema = DBICTest->init_schema();
11
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 });
19
20 my @artworks;
21
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', {});
25 }
26
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', {});
30 }
31
32 foreach my $artwork (@artworks) {
33   $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
34 }
35
36
37 my $cds_80s_rs = $artist->cds_80s;
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   [
46     [ 'me.artist' => 21   ],
47     [ 'me.year' => 1990 ],
48     [ 'me.year' => 1979 ],
49   ]
50 );
51 my @cds_80s = $cds_80s_rs->all;
52 is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
53 map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
54
55
56 my $cds_90s_rs = $artist2->cds_90s;
57 is_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 );
72 my @cds_90s = $cds_90s_rs->all;
73 is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
74 map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
75
76 lives_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)';
81
82 # search for all artists prefetching published cds in the 80s...
83 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
84   ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
85
86 is_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
92 TODO: {
93 local $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)';
95 lives_ok {
96
97 my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search
98   ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' });
99
100 is_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
110 # try to create_related a 80s cd
111 throws_ok {
112   $artist->create_related('cds_80s', { title => 'related creation 1' });
113 } qr/\Qunable to set_from_related via complex 'cds_80s' condition on column(s): 'year'/, 'Create failed - complex cond';
114
115 # now supply an explicit arg overwriting the ambiguous cond
116 my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
117 is(
118   $schema->resultset('CD')->find($id_2020)->title,
119   'related creation 2',
120   '2020 CD created correctly'
121 );
122
123 # try a default year from a specific rel
124 my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
125 is(
126   $schema->resultset('CD')->find($id_1984)->title,
127   'related creation 3',
128   '1984 CD created correctly'
129 );
130
131 # try a specific everything via a non-simplified rel
132 throws_ok {
133   $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
134 } qr/\Qunable to set_from_related - no simplified condition available for 'cds_90s'/, 'Create failed - non-simplified rel';
135
136 # Do a self-join last-entry search
137 my @last_tracks;
138 for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
139   push @last_tracks, $cd->tracks
140                          ->search ({}, { order_by => { -desc => 'position'} })
141                           ->next || ();
142 }
143
144 my $last_tracks_rs = $schema->resultset('Track')->search (
145   {'next_track.trackid' => undef},
146   { join => 'next_track', order_by => 'me.cd' },
147 );
148
149 is_deeply (
150   [$last_tracks_rs->get_column ('trackid')->all],
151   [ map { $_->trackid } @last_tracks ],
152   'last group-entry via self-join works',
153 );
154
155 my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
156 my @artists = $artwork->artists->all;
157 is(scalar @artists, 2, 'the two artists are associated');
158
159 my @artwork_artists = $artwork->artwork_to_artist->all;
160 foreach (@artwork_artists) {
161   lives_ok {
162     my $artista = $_->artist;
163     my $artistb = $_->artist_test_m2m;
164     ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.');
165     my $artistc = $_->artist_test_m2m_noopt;
166     ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.');
167   } 'belongs_to works with custom rels';
168 }
169
170 @artists = ();
171 lives_ok {
172   @artists = $artwork->artists_test_m2m2->all;
173 } 'manytomany with extended rels in the has many works';
174 is(scalar @artists, 2, 'two artists');
175
176 @artists = ();
177 lives_ok {
178   @artists = $artwork->artists_test_m2m->all;
179 } 'can fetch many to many with optimized version';
180 is(scalar @artists, 1, 'only one artist is associated');
181
182 @artists = ();
183 lives_ok {
184   @artists = $artwork->artists_test_m2m_noopt->all;
185 } 'can fetch many to many with non-optimized version';
186 is(scalar @artists, 1, 'only one artist is associated');
187
188
189 # Make a single for each last_track
190 my @singles = map {
191   $_->create_related('cd_single', {
192     title => $_->title . ' (the single)',
193     artist => $artist,
194     year => 1999,
195   }) } @last_tracks
196 ;
197
198 # See if chaining works
199 is_deeply (
200   [ map { $_->title } $last_tracks_rs->search_related('cd_single')->all ],
201   [ map { $_->title } @singles ],
202   'Retrieved singles in proper order'
203 );
204
205 # See if prefetch works
206 is_deeply (
207   [ map { $_->cd_single->title } $last_tracks_rs->search({}, { prefetch => 'cd_single' })->all ],
208   [ map { $_->title } @singles ],
209   'Prefetched singles in proper order'
210 );
211
212 done_testing;