From: Andy Grundman Date: Fri, 9 Sep 2005 20:07:36 +0000 (+0000) Subject: Added SELECT count test for prefetch X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=42e449f5e2cf3e96f12067c4088ce68bfc78480b;p=dbsrgits%2FDBIx-Class-Historic.git Added SELECT count test for prefetch --- diff --git a/t/16joins.t b/t/16joins.t index a810024..1e27cea 100644 --- a/t/16joins.t +++ b/t/16joins.t @@ -1,11 +1,12 @@ use strict; use Test::More; +use IO::File; BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 21 ); + : ( tests => 22 ); } use lib qw(t/lib); @@ -103,7 +104,6 @@ DBICTest::Schema::CD->add_relationship( { 'foreign.liner_id' => 'self.cdid' }, { join_type => 'LEFT', accessor => 'single' }); - $rs = DBICTest::CD->search( { 'artist.name' => 'Caterwauler McCrae' }, { prefetch => [ qw/artist liner_notes/ ], @@ -111,6 +111,10 @@ $rs = DBICTest::CD->search( cmp_ok($rs->count, '==', 3, 'Correct number of records returned'); +# start test for prefetch SELECT count +unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; +DBI->trace(1, 't/var/dbic.trace'); + my @cd = $rs->all; is($cd[0]->title, 'Spoonful of bees', 'First record returned ok'); @@ -121,6 +125,18 @@ is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch f is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok'); +# count the SELECTs +DBI->trace(0); +my $selects = 0; +my $trace = IO::File->new('t/var/dbic.trace', '<') + or die "Unable to read trace file"; +while (<$trace>) { + $selects++ if /SELECT/; +} +$trace->close; +unlink 't/var/dbic.trace'; +is($selects, 1, 'prefetch ran only 1 select statement'); + my ($artist) = DBICTest::Artist->search({ 'cds.year' => 2001 }, { order_by => 'artistid DESC', join => 'cds' });