From: Gerda Shank Date: Mon, 10 Feb 2014 19:22:06 +0000 (-0500) Subject: Add (now passing) tests for set_from_related() via 'foreign_resultobj' X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8c7c83982c9ec8ebf8674c1100f9103f2af06402;p=dbsrgits%2FDBIx-Class-Historic.git Add (now passing) tests for set_from_related() via 'foreign_resultobj' --- diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index e060aff..d1d0740 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -51,6 +51,23 @@ __PACKAGE__->has_many( { order_by => { -asc => 'year'} }, ); +__PACKAGE__->has_many( + cds_cref_cond => 'DBICTest::Schema::CD', + 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}.artist" => { '=' => { -ident => "$args->{self_alias}.artistid"} }, + }, + $args->{self_rowobj} && { + "$args->{foreign_alias}.artist" => $args->{self_rowobj}->artistid, + } + ); + }, +); __PACKAGE__->has_many( cds_80s => 'DBICTest::Schema::CD', diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm index 60bad4e..c09ff1d 100644 --- a/t/lib/DBICTest/Schema/Track.pm +++ b/t/lib/DBICTest/Schema/Track.pm @@ -53,6 +53,29 @@ __PACKAGE__->grouping_column ('cd'); __PACKAGE__->belongs_to( cd => 'DBICTest::Schema::CD', undef, { proxy => { cd_title => 'title' }, }); +# custom condition coderef +__PACKAGE__->belongs_to( cd_cref_cond => 'DBICTest::Schema::CD', +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}.cdid" => { -ident => "$args->{self_alias}.cd" }, + }, + + ( $args->{self_resultobj} ? { + "$args->{foreign_alias}.cdid" => $args->{self_resultobj}->cd + } : () ), + + ( $args->{foreign_resultobj} ? { + "$args->{self_alias}.cd" => $args->{foreign_resultobj}->cdid + } : () ), + ); +} +); __PACKAGE__->belongs_to( disc => 'DBICTest::Schema::CD' => 'cd', { proxy => 'year' }); diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 0cf32b5..a623b4b 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -151,6 +151,10 @@ is_deeply( } 'prefetchy-fetchy-fetch'; +# create_related a plain cd via the equoivalent coderef cond, with no extra conditions +lives_ok { + $artist->create_related('cds_cref_cond', { title => 'related creation via coderef cond', year => '2010' } ); +} 'created_related with simple condition works'; # try to create_related a 80s cd throws_ok { @@ -159,7 +163,8 @@ throws_ok { '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; +my $cd_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' }); +my $id_2020 = $cd_2020->id; is( $schema->resultset('CD')->find($id_2020)->title, 'related creation 2', @@ -268,4 +273,21 @@ is_deeply ( 'Prefetched singles in proper order' ); +# test set_from_related with a belongs_to custom condition +my $cd = $schema->resultset("CD")->find(4); +$artist = $cd->search_related('artist'); +my $track = $schema->resultset("Track")->create( { + trackid => 1, + cd => 3, + position => 99, + title => 'Some Track' +} ); +$track->set_from_related( cd_cref_cond => $cd ); +is ($track->get_column('cd'), 4, 'set from related via coderef cond'); +is_deeply ( + { $track->cd->get_columns }, + { $cd->get_columns }, + 'set from related via coderef cond inflates properly', +); + done_testing;