X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frun%2F06relationship.tl;h=a66211e1bd326d9cba26ff880a68604b0794b6b3;hb=0068f3eecc52a2a45fafc598d398ac864bd427ce;hp=b0f8256a58d02bb4b8de0f0e60a4fa0bd4a71094;hpb=f9db552739e7c980acb4280cb2b6c739a769da5a;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/run/06relationship.tl b/t/run/06relationship.tl index b0f8256..a66211e 100644 --- a/t/run/06relationship.tl +++ b/t/run/06relationship.tl @@ -3,7 +3,7 @@ my $schema = shift; use strict; use warnings; -plan tests => 17; +plan tests => 32; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -38,6 +38,12 @@ if ($INC{'DBICTest/HelperRels.pm'}) { is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' ); +my( $rs_from_list ) = $artist->search_related_rs('cds'); +is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' ); + +( $rs_from_list ) = $artist->cds_rs(); +is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' ); + # count_related is( $artist->count_related('cds'), 4, 'count_related ok' ); @@ -50,12 +56,17 @@ my $track = $schema->resultset("Track")->create( { } ); $track->set_from_related( cd => $cd ); -if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object +if ($INC{'DBICTest/HelperRels.pm'}) { # expect inflated object is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' ); } else { is( $track->cd, 4, 'set_from_related ok' ); } +$track->set_from_related( cd => undef ); + +ok( !defined($track->cd), 'set_from_related with undef ok'); + + # update_from_related, the same as set_from_related, but it calls update afterwards $track = $schema->resultset("Track")->create( { trackid => 2, @@ -89,6 +100,19 @@ is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create $artist->delete_related( cds => { title => 'Greatest Hits' }); cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' ); +# find_or_new_related with an existing record +$cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } ); +is( $cd->year, 2005, 'find_or_new_related on existing record ok' ); +ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' ); + +# find_or_new_related instantiating a new record +$cd = $artist->find_or_new_related( 'cds', { + title => 'Greatest Hits 2: Louder Than Ever', + year => 2007, +} ); +is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' ); +ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' ); + SKIP: { skip "relationship checking needs fixing", 1; # try to add a bogus relationship using the wrong cols @@ -111,6 +135,8 @@ like($@, qr/join condition/, 'failed when creating a rel without join condition, $cd = $schema->resultset("CD")->find(1); my @producers = $cd->producers(); is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' ); +is( $cd->producers_sorted->next->name, 'Bob The Builder', 'sorted many_to_many ok' ); +is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', 'sorted many_to_many with search condition ok' ); # test undirected many-to-many relationship (e.g. "related artists") my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps; @@ -119,10 +145,31 @@ is($undir_maps->count, 1, 'found 1 undirected map for artist 1'); $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps; is($undir_maps->count, 1, 'found 1 undirected map for artist 2'); -my @art = $undir_maps->search_related('mapped_artists')->all; +my $mapped_rs = $undir_maps->search_related('mapped_artists'); + +my @art = $mapped_rs->all; cmp_ok(@art, '==', 2, "Both artist returned from map"); +my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}}); + +cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition"); + +# check join through cascaded has_many relationships +$artist = $schema->resultset("Artist")->find(1); +my $trackset = $artist->cds->search_related('tracks'); +# LEFT join means we also see the trackless additional album... +cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist"); + +# now see about updating eveything that belongs to artist 2 to artist 3 +$artist = $schema->resultset("Artist")->find(2); +my $nartist = $schema->resultset("Artist")->find(3); +cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist"); +cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist"); +$artist->cds->update({artist => $nartist->id}); +cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist"); +cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist"); + } 1;