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 my $cds_90s_rs = $artist2->cds_90s;
47 is_same_sql_bind($cds_90s_rs->as_query,
48 '(SELECT cds_90s.cdid, cds_90s.artist, cds_90s.title, cds_90s.year, cds_90s.genreid,'.
49 'cds_90s.single_track FROM artist me JOIN cd cds_90s ON ( cds_90s.artist = me.artistid'.
50 ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( artistid = ? ))',
52 [ 'cds_90s.year' => 2000 ],
53 [ 'cds_90s.year' => 1989 ],
57 my @cds_90s = $cds_90s_rs->all;
58 is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
59 map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
61 my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 });
62 is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search');
63 map { ok($_->year == 1995) } @cds_90s_95;
65 # search for all artists prefetching published cds in the 80s...
67 # the join must be a prefetch, but it can't work until the collapse rewrite is finished
68 # (right-side vs left-side order)
71 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
72 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all;
75 ([ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
76 [ sort (1980..1989, 1980..1985) ],
77 '16 correct cds found'
79 } 'prefetchy-fetchy-fetch';
81 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
82 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
85 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
86 [ sort (1980..1989, 1980..1985) ],
87 '16 correct cds found'
90 # try to create_related a 80s cd
92 $artist->create_related('cds_80s', { title => 'related creation 1' });
93 } qr/\Qunable to set_from_related via complex 'cds_80s' condition on column(s): 'year'/, 'Create failed - complex cond';
95 # now supply an explicit arg overwriting the ambiguous cond
96 my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
98 $schema->resultset('CD')->find($id_2020)->title,
100 '2020 CD created correctly'
103 # try a default year from a specific rel
104 my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
106 $schema->resultset('CD')->find($id_1984)->title,
107 'related creation 3',
108 '1984 CD created correctly'
111 # try a specific everything via a non-simplified rel
113 $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
114 } qr/\Qunable to set_from_related - no simplified condition available for 'cds_90s'/, 'Create failed - non-simplified rel';
116 # Do a self-join last-entry search
118 for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
119 push @last_track_ids, $cd->tracks
120 ->search ({}, { order_by => { -desc => 'position'} })
121 ->get_column ('trackid')
125 my $last_tracks = $schema->resultset('Track')->search (
126 {'next_track.trackid' => undef},
127 { join => 'next_track', order_by => 'me.cd' },
131 [$last_tracks->get_column ('trackid')->all],
132 [ grep { $_ } @last_track_ids ],
133 'last group-entry via self-join works',
136 my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
137 my @artists = $artwork->artists->all;
138 is(scalar @artists, 2, 'the two artists are associated');
140 my @artwork_artists = $artwork->artwork_to_artist->all;
141 foreach (@artwork_artists) {
143 my $artista = $_->artist;
144 my $artistb = $_->artist_test_m2m;
145 ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.');
146 my $artistc = $_->artist_test_m2m_noopt;
147 ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.');
148 } 'belongs_to works with custom rels';
153 @artists = $artwork->artists_test_m2m2->all;
154 } 'manytomany with extended rels in the has many works';
155 is(scalar @artists, 2, 'two artists');
159 @artists = $artwork->artists_test_m2m->all;
160 } 'can fetch many to many with optimized version';
161 is(scalar @artists, 1, 'only one artist is associated');
165 @artists = $artwork->artists_test_m2m_noopt->all;
166 } 'can fetch many to many with non-optimized version';
167 is(scalar @artists, 1, 'only one artist is associated');