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; |
38 | is_same_sql_bind($cds_80s_rs->as_query, |
39 | '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me'. |
40 | ' WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) ))', |
41 | [ |
42 | [ 'me.artist' => 4 ], |
43 | [ 'me.year' => 1990 ], |
44 | [ 'me.year' => 1979 ], |
45 | ]); |
46 | my @cds_80s = $cds_80s_rs->all; |
d5a14c53 |
47 | is(@cds_80s, 6, '6 80s cds found (1980 - 1985)'); |
48 | map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; |
b5c8410c |
49 | |
f3cfaac6 |
50 | # this is the current version, enhanced version below. |
7689b9e5 |
51 | my $cds_90s_rs = $artist2->cds_90s; |
52 | is_same_sql_bind($cds_90s_rs->as_query, |
53 | '(SELECT cds_90s.cdid, cds_90s.artist, cds_90s.title, cds_90s.year, cds_90s.genreid,'. |
f3cfaac6 |
54 | ' cds_90s.single_track FROM artist artist__row JOIN cd cds_90s ON ( cds_90s.artist = artist__row.artistid'. |
55 | ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( artist__row.artistid = ? ))', |
7689b9e5 |
56 | [ |
57 | [ 'cds_90s.year' => 2000 ], |
58 | [ 'cds_90s.year' => 1989 ], |
f3cfaac6 |
59 | [ 'artist__row.artistid' => 5 ], |
7689b9e5 |
60 | ]); |
61 | |
f3cfaac6 |
62 | TODO: { |
63 | local $TODO = 'enhanced aliasing in search_related'; |
64 | my $cds_90s_rs = $artist2->cds_90s; |
65 | is_same_sql_bind($cds_90s_rs->as_query, |
66 | '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid,'. |
67 | ' me.single_track FROM artist artist__row JOIN cd me ON ( me.artist = artist__row.artistid'. |
68 | ' AND ( me.year < ? AND me.year > ? ) ) WHERE ( artist__row.artistid = ? ))', |
69 | [ |
70 | [ 'me.year' => 2000 ], |
71 | [ 'me.year' => 1989 ], |
72 | [ 'artist__row.artistid' => 5 ], |
73 | ]); |
74 | } |
7689b9e5 |
75 | my @cds_90s = $cds_90s_rs->all; |
d5a14c53 |
76 | is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search'); |
77 | map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s; |
b5c8410c |
78 | |
7689b9e5 |
79 | my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 }); |
80 | is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search'); |
81 | map { ok($_->year == 1995) } @cds_90s_95; |
82 | |
d5a14c53 |
83 | # search for all artists prefetching published cds in the 80s... |
d5a14c53 |
84 | my @all_artists_with_80_cds = $schema->resultset("Artist")->search |
85 | ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 }); |
86 | |
87 | is_deeply( |
88 | [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ], |
89 | [ sort (1980..1989, 1980..1985) ], |
90 | '16 correct cds found' |
91 | ); |
92 | |
abf8d91e |
93 | TODO: { |
94 | local $TODO = 'Prefetch on custom rels can not work until the collapse rewrite is finished ' |
95 | . '(currently collapser requires a right-side (which is indeterministic) order-by)'; |
96 | lives_ok { |
97 | |
98 | my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search |
99 | ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' }); |
100 | |
101 | is_deeply( |
102 | [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds_pref ) ], |
103 | [ sort (1980..1989, 1980..1985) ], |
104 | '16 correct cds found' |
105 | ); |
106 | |
107 | } 'prefetchy-fetchy-fetch'; |
108 | } # end of TODO |
109 | |
110 | |
d5a14c53 |
111 | # try to create_related a 80s cd |
112 | throws_ok { |
113 | $artist->create_related('cds_80s', { title => 'related creation 1' }); |
114 | } qr/\Qunable to set_from_related via complex 'cds_80s' condition on column(s): 'year'/, 'Create failed - complex cond'; |
115 | |
116 | # now supply an explicit arg overwriting the ambiguous cond |
117 | my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id; |
118 | is( |
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 |
125 | my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id; |
126 | is( |
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 |
133 | throws_ok { |
134 | $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' }); |
135 | } qr/\Qunable to set_from_related - no simplified condition available for 'cds_90s'/, 'Create failed - non-simplified rel'; |
b5c8410c |
136 | |
d5a14c53 |
137 | # Do a self-join last-entry search |
b5c8410c |
138 | my @last_track_ids; |
139 | for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) { |
140 | push @last_track_ids, $cd->tracks |
141 | ->search ({}, { order_by => { -desc => 'position'} }) |
142 | ->get_column ('trackid') |
143 | ->next; |
144 | } |
145 | |
146 | my $last_tracks = $schema->resultset('Track')->search ( |
147 | {'next_track.trackid' => undef}, |
148 | { join => 'next_track', order_by => 'me.cd' }, |
149 | ); |
150 | |
151 | is_deeply ( |
152 | [$last_tracks->get_column ('trackid')->all], |
cf320fd7 |
153 | [ grep { $_ } @last_track_ids ], |
b5c8410c |
154 | 'last group-entry via self-join works', |
155 | ); |
156 | |
e98e6478 |
157 | my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first; |
158 | my @artists = $artwork->artists->all; |
159 | is(scalar @artists, 2, 'the two artists are associated'); |
160 | |
161 | my @artwork_artists = $artwork->artwork_to_artist->all; |
162 | foreach (@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 = (); |
173 | lives_ok { |
174 | @artists = $artwork->artists_test_m2m2->all; |
175 | } 'manytomany with extended rels in the has many works'; |
176 | is(scalar @artists, 2, 'two artists'); |
177 | |
178 | @artists = (); |
179 | lives_ok { |
180 | @artists = $artwork->artists_test_m2m->all; |
181 | } 'can fetch many to many with optimized version'; |
182 | is(scalar @artists, 1, 'only one artist is associated'); |
183 | |
184 | @artists = (); |
185 | lives_ok { |
186 | @artists = $artwork->artists_test_m2m_noopt->all; |
187 | } 'can fetch many to many with non-optimized version'; |
188 | is(scalar @artists, 1, 'only one artist is associated'); |
189 | |
190 | |
b5c8410c |
191 | done_testing; |