X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frelationship%2Fcore.t;h=c611142ec2fd9d8e0890a7a4f94144f0559d3c8a;hb=c200d94979bde5ac74070d3e898927433b0e667c;hp=b754f940f1ce0951a772c446c4f9a6bd579e3d4d;hpb=590885aa6934c4eda2a19c08d69394e892ee41a0;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/relationship/core.t b/t/relationship/core.t index b754f94..c611142 100644 --- a/t/relationship/core.t +++ b/t/relationship/core.t @@ -4,57 +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, - } ); -} +$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' ); @@ -147,6 +131,10 @@ lives_ok( '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'; { local $TODO = "relationship checking needs fixing"; @@ -166,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' ); @@ -239,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'});