8 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema();
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 });
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', {});
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', {});
29 foreach my $artwork (@artworks) {
30 $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
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 > ? ) ) ))',
39 [ 'me.year' => 1990 ],
40 [ 'me.year' => 1979 ],
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;
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 = ? ))',
53 [ 'cds_90s.year' => 2000 ],
54 [ 'cds_90s.year' => 1989 ],
55 [ 'artist__row.artistid' => 5 ],
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 = ? ))',
66 [ 'me.year' => 2000 ],
67 [ 'me.year' => 1989 ],
68 [ 'artist__row.artistid' => 5 ],
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;
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;
79 # search for all artists prefetching published cds in the 80s...
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)
85 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
86 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all;
89 ([ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
90 [ sort (1980..1989, 1980..1985) ],
91 '16 correct cds found'
93 } 'prefetchy-fetchy-fetch';
95 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
96 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
99 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
100 [ sort (1980..1989, 1980..1985) ],
101 '16 correct cds found'
104 # try to create_related a 80s cd
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';
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;
112 $schema->resultset('CD')->find($id_2020)->title,
113 'related creation 2',
114 '2020 CD created correctly'
117 # try a default year from a specific rel
118 my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
120 $schema->resultset('CD')->find($id_1984)->title,
121 'related creation 3',
122 '1984 CD created correctly'
125 # try a specific everything via a non-simplified rel
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';
130 # Do a self-join last-entry search
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')
139 my $last_tracks = $schema->resultset('Track')->search (
140 {'next_track.trackid' => undef},
141 { join => 'next_track', order_by => 'me.cd' },
145 [$last_tracks->get_column ('trackid')->all],
146 [ grep { $_ } @last_track_ids ],
147 'last group-entry via self-join works',
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');
154 my @artwork_artists = $artwork->artwork_to_artist->all;
155 foreach (@artwork_artists) {
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';
167 @artists = $artwork->artists_test_m2m2->all;
168 } 'manytomany with extended rels in the has many works';
169 is(scalar @artists, 2, 'two artists');
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');
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');