X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frun%2F06relationship.tl;h=bc84c2e318d6d2dc32d5add7f71ecc9a92dcce7d;hb=d52170d466537df84dc1d86c84d9cd5a8e58407a;hp=57ff1f636fbcbf559ce50324462a04c71f0653b6;hpb=5efe4c7916e1fb2132c1b73ab4ddddbced317adb;p=dbsrgits%2FDBIx-Class.git diff --git a/t/run/06relationship.tl b/t/run/06relationship.tl index 57ff1f6..bc84c2e 100644 --- a/t/run/06relationship.tl +++ b/t/run/06relationship.tl @@ -1,18 +1,19 @@ sub run_tests { +my $schema = shift; use strict; use warnings; -plan tests => 17; +plan tests => 26; # has_a test -my $cd = DBICTest->class("CD")->find(4); +my $cd = $schema->resultset("CD")->find(4); my ($artist) = ($INC{'DBICTest/HelperRels'} ? $cd->artist : $cd->search_related('artist')); is($artist->name, 'Random Boy Band', 'has_a search_related ok'); # has_many test with an order_by clause defined -$artist = DBICTest->class("Artist")->find(1); +$artist = $schema->resultset("Artist")->find(1); my @cds = ($INC{'DBICTest/HelperRels'} ? $artist->cds : $artist->search_related('cds')); @@ -41,7 +42,7 @@ is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' is( $artist->count_related('cds'), 4, 'count_related ok' ); # set_from_related -my $track = DBICTest->class("Track")->create( { +my $track = $schema->resultset("Track")->create( { trackid => 1, cd => 3, position => 98, @@ -49,14 +50,19 @@ my $track = DBICTest->class("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 = DBICTest->class("Track")->create( { +$track = $schema->resultset("Track")->create( { trackid => 2, cd => 3, position => 99, @@ -64,7 +70,7 @@ $track = DBICTest->class("Track")->create( { } ); $track->update_from_related( cd => $cd ); -my $t_cd = (DBICTest->class("Track")->search( cd => 4, position => 99 ))[0]->cd; +my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd; if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object is( $t_cd->cdid, 4, 'update_from_related ok' ); @@ -86,7 +92,7 @@ is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' ); is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' ); $artist->delete_related( cds => { title => 'Greatest Hits' }); -cmp_ok( DBICTest->class("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' ); +cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' ); SKIP: { skip "relationship checking needs fixing", 1; @@ -107,21 +113,44 @@ eval { like($@, qr/join condition/, 'failed when creating a rel without join condition, ok'); # many_to_many helper test -$cd = DBICTest->class("CD")->find(1); +$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 = DBICTest->class("Artist")->find(1)->artist_undirected_maps; +my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps; is($undir_maps->count, 1, 'found 1 undirected map for artist 1'); -$undir_maps = DBICTest->class("Artist")->find(2)->artist_undirected_maps; +$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;