From: Peter Rabbitson Date: Sun, 5 Jul 2015 10:16:46 +0000 (+0200) Subject: Expand (and complicate) m2m test from e98e6478 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b79eae3bd2b5abeb6789cf5e35f592cdaba92482;p=dbsrgits%2FDBIx-Class-Historic.git Expand (and complicate) m2m test from e98e6478 There are no logical changes, the only real addition is the extra opaque relcond in the middle --- diff --git a/t/lib/DBICTest/Schema/Artwork.pm b/t/lib/DBICTest/Schema/Artwork.pm index a167475..d3f56b7 100644 --- a/t/lib/DBICTest/Schema/Artwork.pm +++ b/t/lib/DBICTest/Schema/Artwork.pm @@ -21,12 +21,10 @@ __PACKAGE__->has_many('images', 'DBICTest::Schema::Image', 'artwork_id'); __PACKAGE__->has_many('artwork_to_artist', 'DBICTest::Schema::Artwork_to_Artist', 'artwork_cd_id'); __PACKAGE__->many_to_many('artists', 'artwork_to_artist', 'artist'); -# both to test manytomany with custom rel -# (deliberate misnamed accessor clash) -__PACKAGE__->many_to_many('artist_limited_rank', 'artwork_to_artist', 'artist_limited_rank'); -__PACKAGE__->many_to_many('artist_limited_rank_opaque', 'artwork_to_artist', 'artist_limited_rank_opaque'); +# both to test manytomany via double custom rel (deliberate misnamed accessor clash) +__PACKAGE__->many_to_many('artist_limited_rank', 'artwork_to_artist_via_customcond', 'artist_limited_rank'); +__PACKAGE__->many_to_many('artist_limited_rank_opaque', 'artwork_to_artist_via_opaque_customcond', 'artist_limited_rank_opaque'); -# other test to manytomany __PACKAGE__->has_many('artwork_to_artist_via_customcond', 'DBICTest::Schema::Artwork_to_Artist', sub { # This is for test purposes only. A regular user does not @@ -43,7 +41,21 @@ __PACKAGE__->has_many('artwork_to_artist_via_customcond', 'DBICTest::Schema::Art ); } ); -__PACKAGE__->many_to_many('artists_via_customcond', 'artwork_to_artist_via_customcond', 'artist'); + +__PACKAGE__->has_many('artwork_to_artist_via_opaque_customcond', 'DBICTest::Schema::Artwork_to_Artist', + sub { + # This is for test purposes only. A regular user does not + # need to sanity check the passed-in arguments, this is what + # the tests are for :) + my $args = &check_customcond_args; + + return ( + { "$args->{foreign_alias}.artwork_cd_id" => { -ident => "$args->{self_alias}.cd_id" } }, + ); + } +); + +__PACKAGE__->many_to_many('all_artists_via_opaque_customcond', 'artwork_to_artist_via_opaque_customcond', 'artist'); 1; diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 564142b..4d3052b 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -217,6 +217,7 @@ is_deeply ( 'last_track via insane subquery condition works, even without prefetch', ); + 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'); @@ -232,23 +233,39 @@ foreach (@artwork_artists) { } 'belongs_to works with custom rels'; } -@artists = (); -lives_ok { - @artists = $artwork->artists_via_customcond->all; -} 'manytomany with extended rels in the has many works'; -is(scalar @artists, 2, 'two artists'); +is( + $schema->resultset('Artwork') + ->related_resultset( 'artwork_to_artist_via_opaque_customcond' ) + ->related_resultset( 'artist' ) + ->search({}, { collapse => 1 }) + ->count, + 2, + 'Custom rel works correctly', +); -@artists = (); -lives_ok { - @artists = $artwork->artist_limited_rank->all; -} 'can fetch many to many with optimized version'; -is(scalar @artists, 1, 'only one artist is associated'); +is ( + scalar $artwork->all_artists_via_opaque_customcond->all, + 2, + 'Expected two m2m associated artist objects via opaque costom cond' +); -@artists = (); -lives_ok { - @artists = $artwork->artist_limited_rank_opaque->all; -} 'can fetch many to many with non-optimized version'; -is(scalar @artists, 1, 'only one artist is associated'); +for (qw( artist_limited_rank artist_limited_rank_opaque )) { + is( + $schema->resultset('Artwork') + ->related_resultset( 'artwork_to_artist_via_opaque_customcond' ) + ->related_resultset( $_ ) + ->search({}, { collapse => 1 }) + ->count, + 1, + 'Condition over double custom rel works correctly', + ); + + is ( + scalar $artwork->$_->all, + 1, + 'Expected one m2m associated artist object via opaque custom cond + conditional far cond' + ); +} # Make a single for each last_track