do not use "me" on the related_resultset pessimization
[dbsrgits/DBIx-Class-Historic.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 my $artist  = $schema->resultset("Artist")->create({ name => 'Michael Jackson', rank => 20 });
13 my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque', rank => 1 }) ;
14 my $artist3 = $schema->resultset("Artist")->create({ name => 'Ziraldo', rank => 1 });
15 my $artist4 = $schema->resultset("Artist")->create({ name => 'Paulo Caruso', rank => 20 });
16
17 my @artworks;
18
19 foreach my $year (1975..1985) {
20   my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
21   push @artworks, $cd->create_related('artwork', {});
22 }
23
24 foreach my $year (1975..1995) {
25   my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
26   push @artworks, $cd->create_related('artwork', {});
27 }
28
29 foreach my $artwork (@artworks) {
30   $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
31 }
32
33 my $cds_80s_rs = $artist->cds_80s;
34 is_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                  ]);
42 my @cds_80s = $cds_80s_rs->all;
43 is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
44 map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
45
46 # this is the current version, enhanced version below.
47 my $cds_90s_rs = $artist2->cds_90s;
48 is_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,'.
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 = ? ))',
52                  [
53                   [ 'cds_90s.year' => 2000 ],
54                   [ 'cds_90s.year' => 1989 ],
55                   [ 'artist__row.artistid'  => 5    ],
56                  ]);
57
58 TODO: {
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 }
71 my @cds_90s = $cds_90s_rs->all;
72 is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
73 map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
74
75 my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 });
76 is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search');
77 map { ok($_->year == 1995) } @cds_90s_95;
78
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 #####
84 lives_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';
94
95 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
96   ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
97
98 is_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
105 throws_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
110 my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
111 is(
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
118 my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
119 is(
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
126 throws_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';
129
130 # Do a self-join last-entry search
131 my @last_track_ids;
132 for 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
139 my $last_tracks = $schema->resultset('Track')->search (
140   {'next_track.trackid' => undef},
141   { join => 'next_track', order_by => 'me.cd' },
142 );
143
144 is_deeply (
145   [$last_tracks->get_column ('trackid')->all],
146   [ grep { $_ } @last_track_ids ],
147   'last group-entry via self-join works',
148 );
149
150 my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
151 my @artists = $artwork->artists->all;
152 is(scalar @artists, 2, 'the two artists are associated');
153
154 my @artwork_artists = $artwork->artwork_to_artist->all;
155 foreach (@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 = ();
166 lives_ok {
167   @artists = $artwork->artists_test_m2m2->all;
168 } 'manytomany with extended rels in the has many works';
169 is(scalar @artists, 2, 'two artists');
170
171 @artists = ();
172 lives_ok {
173   @artists = $artwork->artists_test_m2m->all;
174 } 'can fetch many to many with optimized version';
175 is(scalar @artists, 1, 'only one artist is associated');
176
177 @artists = ();
178 lives_ok {
179   @artists = $artwork->artists_test_m2m_noopt->all;
180 } 'can fetch many to many with non-optimized version';
181 is(scalar @artists, 1, 'only one artist is associated');
182
183
184 done_testing;