Switch code/documentation/examples/tests to the new single-arg syntax
[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;
b5c8410c 23is(@cds_80s, 6, '6 80s cds found');
24
9aae3566 25my @cds_90s = $artist2->cds_90s;
26is(@cds_90s, 6, '6 90s cds found even with non-optimized search');
b5c8410c 27
9aae3566 28map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
b5c8410c 29
30
9aae3566 31# search for all artists prefetching published cds in the 80s...
32my @all_cds_80s = $schema->resultset("Artist")->search
33 ({ 'cds_80s_noopt.cdid' => { '!=' => undef } }, { join => 'cds_80s_noopt' });
34is(@all_cds_80s, 16, '16 cds found even with the non-optimized search');
b5c8410c 35
36my @last_track_ids;
37for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) {
38 push @last_track_ids, $cd->tracks
39 ->search ({}, { order_by => { -desc => 'position'} })
40 ->get_column ('trackid')
41 ->next;
42}
43
44my $last_tracks = $schema->resultset('Track')->search (
45 {'next_track.trackid' => undef},
46 { join => 'next_track', order_by => 'me.cd' },
47);
48
49is_deeply (
50 [$last_tracks->get_column ('trackid')->all],
cf320fd7 51 [ grep { $_ } @last_track_ids ],
b5c8410c 52 'last group-entry via self-join works',
53);
54
55done_testing;