X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frelationship%2Fcore.t;h=e0e243c7c69edd0fb680d7e66a93734bee7e37e6;hb=de54e8bd6ea6d510ce8c2ad323c3066afedad58e;hp=d6cb3a3f7020bfb574aa65fa7ff7dd85b73ace5f;hpb=b5c8410c7296e6d5128292b3d21a6a2c261774cc;p=dbsrgits%2FDBIx-Class.git diff --git a/t/relationship/core.t b/t/relationship/core.t index d6cb3a3..e0e243c 100644 --- a/t/relationship/core.t +++ b/t/relationship/core.t @@ -4,62 +4,41 @@ use warnings; use Test::More; use Test::Exception; use lib qw(t/lib); -use DBICTest; -use DBIC::SqlMakerTest; +use DBICTest ':DiffSQL'; my $schema = DBICTest->init_schema(); -my $sdebug = $schema->storage->debug; # has_a test my $cd = $schema->resultset("CD")->find(4); -my ($artist) = ($INC{'DBICTest/HelperRels'} - ? $cd->artist - : $cd->search_related('artist')); +my ($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 = $schema->resultset("Artist")->find(1); -my @cds = ($INC{'DBICTest/HelperRels'} - ? $artist->cds - : $artist->search_related('cds')); +my @cds = $artist->search_related('cds'); is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' ); # search_related with additional abstract query -@cds = ($INC{'DBICTest/HelperRels'} - ? $artist->cds({ title => { like => '%of%' } }) - : $artist->search_related('cds', { title => { like => '%of%' } } ) - ); +@cds = $artist->search_related('cds', { title => { like => '%of%' } } ); is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' ); # creating a related object -if ($INC{'DBICTest/HelperRels.pm'}) { - $artist->add_to_cds({ title => 'Big Flop', year => 2005 }); -} else { - my $big_flop = $artist->create_related( 'cds', { - title => 'Big Flop', - year => 2005, - } ); - - TODO: { - local $TODO = "Can't fix right now" if $DBIx::Class::VERSION < 0.09; - lives_ok { $big_flop->genre} "Don't throw exception when col is not loaded after insert"; - }; -} +$artist->create_related( 'cds', { + title => 'Big Flop', + year => 2005, +} ); my $big_flop_cd = ($artist->search_related('cds'))[3]; is( $big_flop_cd->title, 'Big Flop', 'create_related ok' ); -{ # make sure we are not making pointless select queries when a FK IS NULL - my $queries = 0; - $schema->storage->debugcb(sub { $queries++; }); - $schema->storage->debug(1); +# make sure we are not making pointless select queries when a FK IS NULL +$schema->is_executed_querycount( sub { $big_flop_cd->genre; #should not trigger a select query - is($queries, 0, 'No SELECT made for belongs_to if key IS NULL'); +}, 0, 'No SELECT made for belongs_to if key IS NULL'); + +$schema->is_executed_querycount( sub { $big_flop_cd->genre_inefficient; #should trigger a select query - is($queries, 1, 'SELECT made for belongs_to if key IS NULL when undef_on_null_fk disabled'); - $schema->storage->debug($sdebug); - $schema->storage->debugcb(undef); -} +}, 1, 'SELECT made for belongs_to if key IS NULL when undef_on_null_fk disabled'); my( $rs_from_list ) = $artist->search_related_rs('cds'); isa_ok( $rs_from_list, 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' ); @@ -91,7 +70,7 @@ ok( !defined($track->cd), 'set_from_related with undef ok'); $track = $schema->resultset("Track")->new( {} ); $track->cd; -$track->set_from_related( cd => $cd ); +$track->set_from_related( cd => $cd ); ok ($track->cd, 'set_from_related ok after using the accessor' ); # update_from_related, the same as set_from_related, but it calls update afterwards @@ -144,16 +123,20 @@ my $newartist = $cd->find_or_new_related( 'artist', { is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id'); is($newartist->id, 200, 'find_or_new_related new artist id set'); -lives_ok( - sub { +lives_ok( + sub { my $new_bookmark = $schema->resultset("Bookmark")->new_result( {} ); my $new_related_link = $new_bookmark->new_related( 'link', {} ); }, 'No back rel' ); +throws_ok { + my $new_bookmark = $schema->resultset("Bookmark")->new_result( {} ); + $new_bookmark->new_related( no_such_rel => {} ); +} qr/No such relationship 'no_such_rel'/, 'creating in uknown rel throws'; -TODO: { +{ local $TODO = "relationship checking needs fixing"; # try to add a bogus relationship using the wrong cols throws_ok { @@ -171,7 +154,7 @@ throws_ok { # many_to_many helper tests $cd = $schema->resultset("CD")->find(1); -my @producers = $cd->producers(); +my @producers = $cd->producers(undef, { order_by => 'producerid'} ); 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' ); @@ -208,12 +191,12 @@ is( $prod_rs->first->name, 'Testy McProducer', $cd->add_to_producers({ name => 'Jack Black' }); is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' ); $cd->set_producers($schema->resultset('Producer')->all); -is( $cd->producers->count(), $prod_before_count+2, +is( $cd->producers->count(), $prod_before_count+2, 'many_to_many set_$rel(@objs) count ok' ); $cd->set_producers($schema->resultset('Producer')->find(1)); is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' ); $cd->set_producers([$schema->resultset('Producer')->all]); -is( $cd->producers->count(), $prod_before_count+2, +is( $cd->producers->count(), $prod_before_count+2, 'many_to_many set_$rel(\@objs) count ok' ); $cd->set_producers([$schema->resultset('Producer')->find(1)]); is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' ); @@ -244,7 +227,7 @@ is( $twokey->fourkeys_to_twokeys->count, 0, my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 }); -is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded'); +ok(! $undef_artist_cd->has_column_loaded('artist'), 'FK not loaded'); is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db'); lives_ok { $undef_artist_cd->related_resultset('artist')->new({name => 'foo'}); @@ -271,13 +254,30 @@ is_same_sql_bind ( ON artist_undirected_maps.id1 = me.artistid OR artist_undirected_maps.id2 = me.artistid WHERE ( artistid = ? ) )', - [[artistid => 1]], + [[ { sqlt_datatype => 'integer', dbic_colname => 'artistid' } + => 1 ]], 'expected join sql produced', ); $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps; is($undir_maps->count, 1, 'found 1 undirected map for artist 2'); +{ + my $artist_to_mangle = $schema->resultset('Artist')->find(2); + + $artist_to_mangle->set_from_related( artist_undirected_maps => { id1 => 42 } ); + + ok( ! $artist_to_mangle->is_changed, 'Unresolvable set_from_related did not alter object' ); + + $artist_to_mangle->set_from_related( artist_undirected_maps => {} ); + ok( $artist_to_mangle->is_changed, 'Definitive set_from_related did alter object' ); + is ( + $artist_to_mangle->id, + undef, + 'Correctly unset id on definitive outcome of OR condition', + ); +} + my $mapped_rs = $undir_maps->search_related('mapped_artists'); my @art = $mapped_rs->all; @@ -312,7 +312,7 @@ my $rs_overridden = $schema->source('ForceForeign'); my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3'); cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr."); -# check that relationships below left join relationships are forced to left joins +# check that relationships below left join relationships are forced to left joins # when traversing multiple belongs_to my $cds = $schema->resultset("CD")->search({ 'me.cdid' => 5 }, { join => { single_track => 'cd' } }); is($cds->count, 1, "subjoins under left joins force_left (string)");