support for prefetch from resultsource using extended_rels
[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;
8
9my $schema = DBICTest->init_schema();
10
11
12my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson' });
13foreach my $year (1975..1985) {
14 $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
15}
16
9aae3566 17my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque' }) ;
18foreach my $year (1975..1995) {
19 $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
20}
b5c8410c 21
9aae3566 22my @cds_80s = $artist->cds_80s;
d5a14c53 23is(@cds_80s, 6, '6 80s cds found (1980 - 1985)');
24map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
b5c8410c 25
9aae3566 26my @cds_90s = $artist2->cds_90s;
d5a14c53 27is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search');
28map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s;
b5c8410c 29
d5a14c53 30# search for all artists prefetching published cds in the 80s...
31#####
32# the join must be a prefetch, but it can't work until the collapse rewrite is finished
33# (right-side vs left-side order)
34#####
3fc16c9a 35lives_ok {
36 my @all_artists_with_80_cds = $schema->resultset("Artist")->search
37 ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all;
38
39 is_deeply
40 ([ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ],
41 [ sort (1980..1989, 1980..1985) ],
42 '16 correct cds found'
43 );
44} 'prefetchy-fetchy-fetch';
b5c8410c 45
d5a14c53 46my @all_artists_with_80_cds = $schema->resultset("Artist")->search
47 ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 });
48
49is_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
55# try to create_related a 80s cd
56throws_ok {
57 $artist->create_related('cds_80s', { title => 'related creation 1' });
58} qr/\Qunable to set_from_related via complex 'cds_80s' condition on column(s): 'year'/, 'Create failed - complex cond';
59
60# now supply an explicit arg overwriting the ambiguous cond
61my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id;
62is(
63 $schema->resultset('CD')->find($id_2020)->title,
64 'related creation 2',
65 '2020 CD created correctly'
66);
67
68# try a default year from a specific rel
69my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id;
70is(
71 $schema->resultset('CD')->find($id_1984)->title,
72 'related creation 3',
73 '1984 CD created correctly'
74);
75
76# try a specific everything via a non-simplified rel
77throws_ok {
78 $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' });
79} qr/\Qunable to set_from_related - no simplified condition available for 'cds_90s'/, 'Create failed - non-simplified rel';
b5c8410c 80
d5a14c53 81# Do a self-join last-entry search
b5c8410c 82my @last_track_ids;
83for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
84 push @last_track_ids, $cd->tracks
85 ->search ({}, { order_by => { -desc => 'position'} })
86 ->get_column ('trackid')
87 ->next;
88}
89
90my $last_tracks = $schema->resultset('Track')->search (
91 {'next_track.trackid' => undef},
92 { join => 'next_track', order_by => 'me.cd' },
93);
94
95is_deeply (
96 [$last_tracks->get_column ('trackid')->all],
cf320fd7 97 [ grep { $_ } @last_track_ids ],
b5c8410c 98 'last group-entry via self-join works',
99);
100
101done_testing;