X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fprefetch%2Fvia_search_related.t;h=846b3338fbb87dcb378e01092dd9f57d0dd52f7a;hb=dc71574729e5f45defc433ca2e11b7c2377eaf95;hp=588b125051776f12cf5f55a15e04ee3ca182976a;hpb=bdb4d9404c947de09d19c6af830518b5c9801094;p=dbsrgits%2FDBIx-Class.git diff --git a/t/prefetch/via_search_related.t b/t/prefetch/via_search_related.t index 588b125..846b333 100644 --- a/t/prefetch/via_search_related.t +++ b/t/prefetch/via_search_related.t @@ -1,18 +1,16 @@ +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; my $schema = DBICTest->init_schema(); -my $queries; -my $debugcb = sub { $queries++; }; -my $orig_debug = $schema->storage->debug; - lives_ok ( sub { my $no_prefetch = $schema->resultset('Track')->search_related(cd => { @@ -73,16 +71,12 @@ lives_ok ( sub { { my $cd = $schema->resultset('CD')->search({}, { prefetch => 'cd_to_producer' })->find(1); - $queries = 0; - $schema->storage->debugcb ($debugcb); - $schema->storage->debug (1); - - is( $cd->cd_to_producer->count, 3 ,'Count of prefetched m2m links via accessor' ); - is( scalar $cd->cd_to_producer->all, 3, 'Amount of prefetched m2m link objects via accessor' ); - is( $cd->search_related('cd_to_producer')->count, 3, 'Count of prefetched m2m links via search_related' ); - is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Amount of prefetched m2m links via search_related' ); - - is($queries, 0, 'No queries ran so far'); + $schema->is_executed_querycount( sub { + is( $cd->cd_to_producer->count, 3 ,'Count of prefetched m2m links via accessor' ); + is( scalar $cd->cd_to_producer->all, 3, 'Amount of prefetched m2m link objects via accessor' ); + is( $cd->search_related('cd_to_producer')->count, 3, 'Count of prefetched m2m links via search_related' ); + is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Amount of prefetched m2m links via search_related' ); + }, 0, 'No queries ran so far'); is( scalar $cd->cd_to_producer->search_related('producer')->all, 3, 'Amount of objects via search_related off prefetched linker' ); @@ -97,16 +91,12 @@ lives_ok ( sub { is( $cd->producers->count, 3, 'Count via m2m accessor' ); - $queries = 0; - - is( $cd->cd_to_producer->count, 3 ,'Review count of prefetched m2m links via accessor' ); - is( scalar $cd->cd_to_producer->all, 3, 'Review amount of prefetched m2m link objects via accessor' ); - is( $cd->search_related('cd_to_producer')->count, 3, 'Review count of prefetched m2m links via search_related' ); - is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Rreview amount of prefetched m2m links via search_related' ); - - is($queries, 0, 'Still no queries on prefetched linker'); - $schema->storage->debugcb (undef); - $schema->storage->debug ($orig_debug); + $schema->is_executed_querycount( sub { + is( $cd->cd_to_producer->count, 3 ,'Review count of prefetched m2m links via accessor' ); + is( scalar $cd->cd_to_producer->all, 3, 'Review amount of prefetched m2m link objects via accessor' ); + is( $cd->search_related('cd_to_producer')->count, 3, 'Review count of prefetched m2m links via search_related' ); + is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Rreview amount of prefetched m2m links via search_related' ); + }, 0, 'Still no queries on prefetched linker'); } # tests with distinct => 1 @@ -169,21 +159,18 @@ lives_ok (sub { is($rs->all, 1, 'distinct with prefetch (objects)'); is($rs->count, 1, 'distinct with prefetch (count)'); - $queries = 0; - $schema->storage->debugcb ($debugcb); - $schema->storage->debug (1); + local $TODO = "This makes another 2 trips to the database, it can't be right"; + $schema->is_executed_querycount( sub { - # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2 - is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)'); - is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)'); + # the is() calls are not todoified + local $TODO; - { - local $TODO = "This makes another 2 trips to the database, it can't be right"; - is ($queries, 0, 'No extra queries fired (prefetch survives search_related)'); - } + # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2 + is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)'); + is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)'); + + }, 0, 'No extra queries fired (prefetch survives search_related)'); - $schema->storage->debugcb (undef); - $schema->storage->debug ($orig_debug); }, 'distinct generally works with prefetch on deep search_related chains'); # pathological "user knows what they're doing" case @@ -200,7 +187,7 @@ lives_ok (sub { }); is_deeply( - $rs->all_hri, + $rs->search({}, { order_by => 'me.title' })->all_hri, [ { title => "Caterwaulin' Blues", max_trk => 3 }, { title => "Come Be Depressed With Us", max_trk => 3 },