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