refuse to invoke create_related as class method
[dbsrgits/DBIx-Class.git] / t / relationship / custom.t
CommitLineData
b5c8410c 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
7689b9e5 8use DBIC::SqlMakerTest;
b5c8410c 9
10my $schema = DBICTest->init_schema();
11
e98e6478 12my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson', rank => 20 });
13my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque', rank => 1 }) ;
14my $artist3 = $schema->resultset("Artist")->create({ name => 'Ziraldo', rank => 1 });
15my $artist4 = $schema->resultset("Artist")->create({ name => 'Paulo Caruso', rank => 20 });
16
17my @artworks;
b5c8410c 18
b5c8410c 19foreach my $year (1975..1985) {
e98e6478 20 my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
21 push @artworks, $cd->create_related('artwork', {});
b5c8410c 22}
23
9aae3566 24foreach my $year (1975..1995) {
e98e6478 25 my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
26 push @artworks, $cd->create_related('artwork', {});
27}
28
29foreach my $artwork (@artworks) {
30 $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4);
9aae3566 31}
b5c8410c 32
7689b9e5 33my $cds_80s_rs = $artist->cds_80s;
34is_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 ]);
42my @cds_80s = $cds_80s_rs->all;
d5a14c53 43is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
44map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
b5c8410c 45
7689b9e5 46my $cds_90s_rs = $artist2->cds_90s;
47is_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'.
2235a951 50 ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( me.artistid = ? ))',
7689b9e5 51 [
52 [ 'cds_90s.year' => 2000 ],
53 [ 'cds_90s.year' => 1989 ],
2235a951 54 [ 'me.artistid' => 5 ],
7689b9e5 55 ]);
56
57my @cds_90s = $cds_90s_rs->all;
d5a14c53 58is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
59map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
b5c8410c 60
7689b9e5 61my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 });
62is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search');
63map { ok($_->year == 1995) } @cds_90s_95;
64
d5a14c53 65# search for all artists prefetching published cds in the 80s...
66#####
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)
69#####
3fc16c9a 70lives_ok {
71 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
72 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all;
73
74 is_deeply
75 ([ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
76 [ sort (1980..1989, 1980..1985) ],
77 '16 correct cds found'
78 );
79} 'prefetchy-fetchy-fetch';
b5c8410c 80
d5a14c53 81my @all_artists_with_80_cds = $schema->resultset("Artist")->search
82 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
83
84is_deeply(
85 [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
86 [ sort (1980..1989, 1980..1985) ],
87 '16 correct cds found'
88);
89
90# try to create_related a 80s cd
91throws_ok {
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';
94
95# now supply an explicit arg overwriting the ambiguous cond
96my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
97is(
98 $schema->resultset('CD')->find($id_2020)->title,
99 'related creation 2',
100 '2020 CD created correctly'
101);
102
103# try a default year from a specific rel
104my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
105is(
106 $schema->resultset('CD')->find($id_1984)->title,
107 'related creation 3',
108 '1984 CD created correctly'
109);
110
111# try a specific everything via a non-simplified rel
112throws_ok {
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';
b5c8410c 115
d5a14c53 116# Do a self-join last-entry search
b5c8410c 117my @last_track_ids;
118for 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')
122 ->next;
123}
124
125my $last_tracks = $schema->resultset('Track')->search (
126 {'next_track.trackid' => undef},
127 { join => 'next_track', order_by => 'me.cd' },
128);
129
130is_deeply (
131 [$last_tracks->get_column ('trackid')->all],
cf320fd7 132 [ grep { $_ } @last_track_ids ],
b5c8410c 133 'last group-entry via self-join works',
134);
135
e98e6478 136my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first;
137my @artists = $artwork->artists->all;
138is(scalar @artists, 2, 'the two artists are associated');
139
140my @artwork_artists = $artwork->artwork_to_artist->all;
141foreach (@artwork_artists) {
142 lives_ok {
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';
149}
150
151@artists = ();
152lives_ok {
153 @artists = $artwork->artists_test_m2m2->all;
154} 'manytomany with extended rels in the has many works';
155is(scalar @artists, 2, 'two artists');
156
157@artists = ();
158lives_ok {
159 @artists = $artwork->artists_test_m2m->all;
160} 'can fetch many to many with optimized version';
161is(scalar @artists, 1, 'only one artist is associated');
162
163@artists = ();
164lives_ok {
165 @artists = $artwork->artists_test_m2m_noopt->all;
166} 'can fetch many to many with non-optimized version';
167is(scalar @artists, 1, 'only one artist is associated');
168
169
b5c8410c 170done_testing;