X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frelationship%2Fcore.t;h=6ebf94fa446144a117b2b3dc5a309f57e164bc4c;hb=6c5aa1fbffdc9e5679d2f68780b11a9569ec1993;hp=23d829debccdc8b08507563ec065dda6c782f50b;hpb=fb88ca2c952ef2e17f6e8a414079f12f71761ff7;p=dbsrgits%2FDBIx-Class.git diff --git a/t/relationship/core.t b/t/relationship/core.t index 23d829d..6ebf94f 100644 --- a/t/relationship/core.t +++ b/t/relationship/core.t @@ -1,14 +1,15 @@ +BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } + use strict; use warnings; use Test::More; use Test::Exception; -use lib qw(t/lib); -use DBICTest; -use DBIC::SqlMakerTest; +use Test::Warn; + +use DBICTest ':DiffSQL'; my $schema = DBICTest->init_schema(); -my $sdebug = $schema->storage->debug; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -33,17 +34,14 @@ $artist->create_related( 'cds', { 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' ); @@ -136,22 +134,10 @@ lives_ok( 'No back rel' ); - -{ - local $TODO = "relationship checking needs fixing"; - # try to add a bogus relationship using the wrong cols - throws_ok { - DBICTest::Schema::Artist->add_relationship( - tracks => 'DBICTest::Schema::Track', - { 'foreign.cd' => 'self.cdid' } - ); - } qr/Unknown column/, 'failed when creating a rel with invalid key, ok'; -} - -# another bogus relationship using no join condition throws_ok { - DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' ); -} qr/join condition/, 'failed when creating a rel without join condition, 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'; # many_to_many helper tests $cd = $schema->resultset("CD")->find(1); @@ -191,12 +177,26 @@ is( $prod_rs->first->name, 'Testy McProducer', 'many_to_many add_to_$rel($hash) ok' ); $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, - '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]); + +warnings_like { + $cd->set_producers($schema->resultset('Producer')->all); + 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' ); +} [ + ( qr/\QCalling 'set_producers' with a list of items to link to is deprecated, use an arrayref instead/ ) x 2 +], 'Warnings on deprecated invocation of set_* found'; + +warnings_like { + is( $cd->producers( producerid => '666' )->count, 0 ); +} [ + qr/\Qsearch( %condition ) is deprecated/ +], 'Warning properly bubbled from search()'; + +# the undef-attr-arg at the end is deliberate: this is what FormFu does +$cd->set_producers([$schema->resultset('Producer')->all], undef); is( $cd->producers->count(), $prod_before_count+2, 'many_to_many set_$rel(\@objs) count ok' ); $cd->set_producers([$schema->resultset('Producer')->find(1)]); @@ -204,11 +204,11 @@ is( $cd->producers->count(), 1, 'many_to_many set_$rel([$obj]) count ok' ); throws_ok { $cd->remove_from_producers({ fake => 'hash' }) -} qr/needs an object/, 'remove_from_$rel($hash) dies correctly'; +} qr/expects an object/, 'remove_from_$rel($hash) dies correctly'; throws_ok { $cd->add_to_producers() -} qr/needs an object or hashref/, 'add_to_$rel(undef) dies correctly'; +} qr/expects an object or hashref/, 'add_to_$rel(undef) dies correctly'; # many_to_many stresstest my $twokey = $schema->resultset('TwoKeys')->find(1,1); @@ -228,7 +228,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'}); @@ -263,6 +263,26 @@ is_same_sql_bind ( $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); + + throws_ok { + $artist_to_mangle->set_from_related( artist_undirected_maps => { id1 => 42 } ) + } qr/\QUnable to complete value inferrence - relationship 'artist_undirected_maps' on source 'Artist' results in expression(s) instead of definitive values: ( artistid = ? OR artistid IS NULL )/, + 'Expected exception on unresovable set_from_related' + ; + + 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;