do not use "me" on the related_resultset pessimization
[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
e98e6478 12my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson', rank => 20 });
13my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque', rank => 1 }) ;
14my $artist3 = $schema->resultset("Artist")->create({ name => 'Ziraldo', rank => 1 });
15my $artist4 = $schema->resultset("Artist")->create({ name => 'Paulo Caruso', rank => 20 });
16
17my @artworks;
b5c8410c 18
b5c8410c 19foreach my $year (1975..1985) {
e98e6478 20 my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
21 push @artworks, $cd->create_related('artwork', {});
b5c8410c 22}
23
9aae3566 24foreach my $year (1975..1995) {
e98e6478 25 my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
26 push @artworks, $cd->create_related('artwork', {});
27}
28
29foreach my $artwork (@artworks) {
30 $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
9aae3566 31}
b5c8410c 32
7689b9e5 33my $cds_80s_rs = $artist->cds_80s;
34is_same_sql_bind($cds_80s_rs->as_query,
35 '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me'.
36 ' WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) ))',
37 [
38 [ 'me.artist' => 4 ],
39 [ 'me.year' => 1990 ],
40 [ 'me.year' => 1979 ],
41 ]);
42my @cds_80s = $cds_80s_rs->all;
d5a14c53 43is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
44map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
b5c8410c 45
f3cfaac6 46# this is the current version, enhanced version below.
7689b9e5 47my $cds_90s_rs = $artist2->cds_90s;
48is_same_sql_bind($cds_90s_rs->as_query,
49 '(SELECT cds_90s.cdid, cds_90s.artist, cds_90s.title, cds_90s.year, cds_90s.genreid,'.
f3cfaac6 50 ' cds_90s.single_track FROM artist artist__row JOIN cd cds_90s ON ( cds_90s.artist = artist__row.artistid'.
51 ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( artist__row.artistid = ? ))',
7689b9e5 52 [
53 [ 'cds_90s.year' => 2000 ],
54 [ 'cds_90s.year' => 1989 ],
f3cfaac6 55 [ 'artist__row.artistid' => 5 ],
7689b9e5 56 ]);
57
f3cfaac6 58TODO: {
59 local $TODO = 'enhanced aliasing in search_related';
60 my $cds_90s_rs = $artist2->cds_90s;
61 is_same_sql_bind($cds_90s_rs->as_query,
62 '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid,'.
63 ' me.single_track FROM artist artist__row JOIN cd me ON ( me.artist = artist__row.artistid'.
64 ' AND ( me.year < ? AND me.year > ? ) ) WHERE ( artist__row.artistid = ? ))',
65 [
66 [ 'me.year' => 2000 ],
67 [ 'me.year' => 1989 ],
68 [ 'artist__row.artistid' => 5 ],
69 ]);
70}
7689b9e5 71my @cds_90s = $cds_90s_rs->all;
d5a14c53 72is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
73map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
b5c8410c 74
7689b9e5 75my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 });
76is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search');
77map { ok($_->year == 1995) } @cds_90s_95;
78
d5a14c53 79# search for all artists prefetching published cds in the 80s...
80#####
81# the join must be a prefetch, but it can't work until the collapse rewrite is finished
82# (right-side vs left-side order)
83#####
3fc16c9a 84lives_ok {
85 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
86 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all;
87
88 is_deeply
89 ([ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
90 [ sort (1980..1989, 1980..1985) ],
91 '16 correct cds found'
92 );
93} 'prefetchy-fetchy-fetch';
b5c8410c 94
d5a14c53 95my @all_artists_with_80_cds = $schema->resultset("Artist")->search
96 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
97
98is_deeply(
99 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
100 [ sort (1980..1989, 1980..1985) ],
101 '16 correct cds found'
102);
103
104# try to create_related a 80s cd
105throws_ok {
106 $artist->create_related('cds_80s', { title => 'related creation 1' });
107} qr/\Qunable to set_from_related via complex 'cds_80s' condition on column(s): 'year'/, 'Create failed - complex cond';
108
109# now supply an explicit arg overwriting the ambiguous cond
110my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
111is(
112 $schema->resultset('CD')->find($id_2020)->title,
113 'related creation 2',
114 '2020 CD created correctly'
115);
116
117# try a default year from a specific rel
118my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
119is(
120 $schema->resultset('CD')->find($id_1984)->title,
121 'related creation 3',
122 '1984 CD created correctly'
123);
124
125# try a specific everything via a non-simplified rel
126throws_ok {
127 $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
128} qr/\Qunable to set_from_related - no simplified condition available for 'cds_90s'/, 'Create failed - non-simplified rel';
b5c8410c 129
d5a14c53 130# Do a self-join last-entry search
b5c8410c 131my @last_track_ids;
132for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
133 push @last_track_ids, $cd->tracks
134 ->search ({}, { order_by => { -desc => 'position'} })
135 ->get_column ('trackid')
136 ->next;
137}
138
139my $last_tracks = $schema->resultset('Track')->search (
140 {'next_track.trackid' => undef},
141 { join => 'next_track', order_by => 'me.cd' },
142);
143
144is_deeply (
145 [$last_tracks->get_column ('trackid')->all],
cf320fd7 146 [ grep { $_ } @last_track_ids ],
b5c8410c 147 'last group-entry via self-join works',
148);
149
e98e6478 150my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
151my @artists = $artwork->artists->all;
152is(scalar @artists, 2, 'the two artists are associated');
153
154my @artwork_artists = $artwork->artwork_to_artist->all;
155foreach (@artwork_artists) {
156 lives_ok {
157 my $artista = $_->artist;
158 my $artistb = $_->artist_test_m2m;
159 ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.');
160 my $artistc = $_->artist_test_m2m_noopt;
161 ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.');
162 } 'belongs_to works with custom rels';
163}
164
165@artists = ();
166lives_ok {
167 @artists = $artwork->artists_test_m2m2->all;
168} 'manytomany with extended rels in the has many works';
169is(scalar @artists, 2, 'two artists');
170
171@artists = ();
172lives_ok {
173 @artists = $artwork->artists_test_m2m->all;
174} 'can fetch many to many with optimized version';
175is(scalar @artists, 1, 'only one artist is associated');
176
177@artists = ();
178lives_ok {
179 @artists = $artwork->artists_test_m2m_noopt->all;
180} 'can fetch many to many with non-optimized version';
181is(scalar @artists, 1, 'only one artist is associated');
182
183
b5c8410c 184done_testing;