X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frelationship%2Fcustom.t;h=3b58926a2826d75ab419bf9336e164f1097b47bf;hb=d5a14c53a280677a116b2efb393853a783281b2a;hp=6d4e85b27c8e304b1044f2aa0bf7a5dccd515ab2;hpb=cf320fd7d52a1c9d2e7d097e8b69c54db1453bec;p=dbsrgits%2FDBIx-Class.git diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 6d4e85b..3b58926 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -10,20 +10,73 @@ my $schema = DBICTest->init_schema(); my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson' }); - foreach my $year (1975..1985) { $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); } +my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque' }) ; +foreach my $year (1975..1995) { + $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); +} + my @cds_80s = $artist->cds_80s; +is(@cds_80s, 6, '6 80s cds found (1980 - 1985)'); +map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; -is(@cds_80s, 6, '6 80s cds found'); +my @cds_90s = $artist2->cds_90s; +is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search'); +map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s; -map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; +# search for all artists prefetching published cds in the 80s... +##### +# the join must be a prefetch, but it can't work until the collapse rewrite is finished +# (right-side vs left-side order) +##### +TODO: { + local $TODO = "Replace the join below with this when we fix the collapser"; + lives_ok { + my @all_artists_with_80_cds = $schema->resultset("Artist")->search + ({ 'cds_80s.cdid' => { '!=' => undef } }, { prefetch => 'cds_80s' })->all; + } 'prefetchy-fetchy-fetch?'; +} + +my @all_artists_with_80_cds = $schema->resultset("Artist")->search + ({ 'cds_80s.cdid' => { '!=' => undef } }, { join => 'cds_80s', distinct => 1 }); +is_deeply( + [ sort ( map { $_->year } map { $_->cds_80s->all } @all_artists_with_80_cds ) ], + [ sort (1980..1989, 1980..1985) ], + '16 correct cds found' +); + +# try to create_related a 80s cd +throws_ok { + $artist->create_related('cds_80s', { title => 'related creation 1' }); +} qr/\Qunable to set_from_related via complex 'cds_80s' condition on column(s): 'year'/, 'Create failed - complex cond'; + +# now supply an explicit arg overwriting the ambiguous cond +my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id; +is( + $schema->resultset('CD')->find($id_2020)->title, + 'related creation 2', + '2020 CD created correctly' +); + +# try a default year from a specific rel +my $id_1984 = $artist->create_related('cds_84', { title => 'related creation 3' })->id; +is( + $schema->resultset('CD')->find($id_1984)->title, + 'related creation 3', + '1984 CD created correctly' +); +# try a specific everything via a non-simplified rel +throws_ok { + $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' }); +} qr/\Qunable to set_from_related - no simplified condition available for 'cds_90s'/, 'Create failed - non-simplified rel'; +# Do a self-join last-entry search my @last_track_ids; for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) { push @last_track_ids, $cd->tracks