24556f93da4214fb26264bf0e7d41181fb1872d2
[dbsrgits/DBIx-Class.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
9 my $schema = DBICTest->init_schema();
10
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 });
15
16 my @artworks;
17
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', {});
21 }
22
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', {});
26 }
27
28 foreach my $artwork (@artworks) {
29   $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
30 }
31
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;
35
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;
39
40 # search for all artists prefetching published cds in the 80s...
41 #####
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)
44 #####
45 lives_ok {
46   my @all_artists_with_80_cds = $schema->resultset("Artist")->search
47     ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all;
48
49   is_deeply
50     ([ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
51      [ sort (1980..1989, 1980..1985) ],
52      '16 correct cds found'
53     );
54 } 'prefetchy-fetchy-fetch';
55
56 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
57   ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
58
59 is_deeply(
60   [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
61   [ sort (1980..1989, 1980..1985) ],
62   '16 correct cds found'
63 );
64
65 # try to create_related a 80s cd
66 throws_ok {
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';
69
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;
72 is(
73   $schema->resultset('CD')->find($id_2020)->title,
74   'related creation 2',
75   '2020 CD created correctly'
76 );
77
78 # try a default year from a specific rel
79 my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
80 is(
81   $schema->resultset('CD')->find($id_1984)->title,
82   'related creation 3',
83   '1984 CD created correctly'
84 );
85
86 # try a specific everything via a non-simplified rel
87 throws_ok {
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';
90
91 # Do a self-join last-entry search
92 my @last_track_ids;
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')
97                                 ->next;
98 }
99
100 my $last_tracks = $schema->resultset('Track')->search (
101   {'next_track.trackid' => undef},
102   { join => 'next_track', order_by => 'me.cd' },
103 );
104
105 is_deeply (
106   [$last_tracks->get_column ('trackid')->all],
107   [ grep { $_ } @last_track_ids ],
108   'last group-entry via self-join works',
109 );
110
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');
114
115 my @artwork_artists = $artwork->artwork_to_artist->all;
116 foreach (@artwork_artists) {
117   lives_ok {
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';
124 }
125
126 @artists = ();
127 lives_ok {
128   @artists = $artwork->artists_test_m2m2->all;
129 } 'manytomany with extended rels in the has many works';
130 is(scalar @artists, 2, 'two artists');
131
132 @artists = ();
133 lives_ok {
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');
137
138 @artists = ();
139 lives_ok {
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');
143
144
145 done_testing;