X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frelationship%2Fcustom.t;h=8f7ce8d44943a71ab174c9f9e87e5583a46f3c9b;hb=2235a95179bf17c30a1e5364e78da5bbad23ddd6;hp=369aeb08b3dacb2b879cc277999c927b3963928b;hpb=3fc16c9aedbb4d864b98fbe9ed8c5cfe540f1496;p=dbsrgits%2FDBIx-Class.git diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 369aeb0..8f7ce8d 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -5,28 +5,63 @@ use Test::More; use Test::Exception; use lib qw(t/lib); use DBICTest; +use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); +my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson', rank => 20 }); +my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque', rank => 1 }) ; +my $artist3 = $schema->resultset("Artist")->create({ name => 'Ziraldo', rank => 1 }); +my $artist4 = $schema->resultset("Artist")->create({ name => 'Paulo Caruso', rank => 20 }); + +my @artworks; -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 $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); + push @artworks, $cd->create_related('artwork', {}); } -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 $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); + push @artworks, $cd->create_related('artwork', {}); +} + +foreach my $artwork (@artworks) { + $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4); } -my @cds_80s = $artist->cds_80s; +my $cds_80s_rs = $artist->cds_80s; +is_same_sql_bind($cds_80s_rs->as_query, + '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me'. + ' WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) ))', + [ + [ 'me.artist' => 4 ], + [ 'me.year' => 1990 ], + [ 'me.year' => 1979 ], + ]); +my @cds_80s = $cds_80s_rs->all; is(@cds_80s, 6, '6 80s cds found (1980 - 1985)'); map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; -my @cds_90s = $artist2->cds_90s; +my $cds_90s_rs = $artist2->cds_90s; +is_same_sql_bind($cds_90s_rs->as_query, + '(SELECT cds_90s.cdid, cds_90s.artist, cds_90s.title, cds_90s.year, cds_90s.genreid,'. + 'cds_90s.single_track FROM artist me JOIN cd cds_90s ON ( cds_90s.artist = me.artistid'. + ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( me.artistid = ? ))', + [ + [ 'cds_90s.year' => 2000 ], + [ 'cds_90s.year' => 1989 ], + [ 'me.artistid' => 5 ], + ]); + +my @cds_90s = $cds_90s_rs->all; is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search'); map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s; +my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 }); +is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search'); +map { ok($_->year == 1995) } @cds_90s_95; + # 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 @@ -98,4 +133,38 @@ is_deeply ( 'last group-entry via self-join works', ); +my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first; +my @artists = $artwork->artists->all; +is(scalar @artists, 2, 'the two artists are associated'); + +my @artwork_artists = $artwork->artwork_to_artist->all; +foreach (@artwork_artists) { + lives_ok { + my $artista = $_->artist; + my $artistb = $_->artist_test_m2m; + ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.'); + my $artistc = $_->artist_test_m2m_noopt; + ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.'); + } 'belongs_to works with custom rels'; +} + +@artists = (); +lives_ok { + @artists = $artwork->artists_test_m2m2->all; +} 'manytomany with extended rels in the has many works'; +is(scalar @artists, 2, 'two artists'); + +@artists = (); +lives_ok { + @artists = $artwork->artists_test_m2m->all; +} 'can fetch many to many with optimized version'; +is(scalar @artists, 1, 'only one artist is associated'); + +@artists = (); +lives_ok { + @artists = $artwork->artists_test_m2m_noopt->all; +} 'can fetch many to many with non-optimized version'; +is(scalar @artists, 1, 'only one artist is associated'); + + done_testing;