9 my $schema = DBICTest->init_schema();
11 my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson', rank => 20 });
12 my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque', rank => 1 }) ;
13 my $artist3 = $schema->resultset("Artist")->create({ name => 'Ziraldo', rank => 1 });
14 my $artist4 = $schema->resultset("Artist")->create({ name => 'Paulo Caruso', rank => 20 });
18 foreach my $year (1975..1985) {
19 my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
20 push @artworks, $cd->create_related('artwork', {});
23 foreach my $year (1975..1995) {
24 my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
25 push @artworks, $cd->create_related('artwork', {});
28 foreach my $artwork (@artworks) {
29 $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
32 my @cds_80s = $artist->cds_80s;
33 is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
34 map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
36 my @cds_90s = $artist2->cds_90s;
37 is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
38 map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
40 # search for all artists prefetching published cds in the 80s...
42 # the join must be a prefetch, but it can't work until the collapse rewrite is finished
43 # (right-side vs left-side order)
46 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
47 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all;
50 ([ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
51 [ sort (1980..1989, 1980..1985) ],
52 '16 correct cds found'
54 } 'prefetchy-fetchy-fetch';
56 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
57 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
60 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
61 [ sort (1980..1989, 1980..1985) ],
62 '16 correct cds found'
65 # try to create_related a 80s cd
67 $artist->create_related('cds_80s', { title => 'related creation 1' });
68 } qr/\Qunable to set_from_related via complex 'cds_80s' condition on column(s): 'year'/, 'Create failed - complex cond';
70 # now supply an explicit arg overwriting the ambiguous cond
71 my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
73 $schema->resultset('CD')->find($id_2020)->title,
75 '2020 CD created correctly'
78 # try a default year from a specific rel
79 my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
81 $schema->resultset('CD')->find($id_1984)->title,
83 '1984 CD created correctly'
86 # try a specific everything via a non-simplified rel
88 $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
89 } qr/\Qunable to set_from_related - no simplified condition available for 'cds_90s'/, 'Create failed - non-simplified rel';
91 # Do a self-join last-entry search
93 for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
94 push @last_track_ids, $cd->tracks
95 ->search ({}, { order_by => { -desc => 'position'} })
96 ->get_column ('trackid')
100 my $last_tracks = $schema->resultset('Track')->search (
101 {'next_track.trackid' => undef},
102 { join => 'next_track', order_by => 'me.cd' },
106 [$last_tracks->get_column ('trackid')->all],
107 [ grep { $_ } @last_track_ids ],
108 'last group-entry via self-join works',
111 my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
112 my @artists = $artwork->artists->all;
113 is(scalar @artists, 2, 'the two artists are associated');
115 my @artwork_artists = $artwork->artwork_to_artist->all;
116 foreach (@artwork_artists) {
118 my $artista = $_->artist;
119 my $artistb = $_->artist_test_m2m;
120 ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.');
121 my $artistc = $_->artist_test_m2m_noopt;
122 ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.');
123 } 'belongs_to works with custom rels';
128 @artists = $artwork->artists_test_m2m2->all;
129 } 'manytomany with extended rels in the has many works';
130 is(scalar @artists, 2, 'two artists');
134 @artists = $artwork->artists_test_m2m->all;
135 } 'can fetch many to many with optimized version';
136 is(scalar @artists, 1, 'only one artist is associated');
140 @artists = $artwork->artists_test_m2m_noopt->all;
141 } 'can fetch many to many with non-optimized version';
142 is(scalar @artists, 1, 'only one artist is associated');