X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frun%2F23cache.tl;h=4be8fbd81e69276f800acbd3a76ab5fe46c355b4;hb=bab77431c4723e1625740c08c9c53742b689cbdb;hp=1f85dcbcc02610cbccee4420e4c6e77b6d55d88e;hpb=f109ee4aa6c7f7cc4eaf6cd15b6d67c2ebcf8702;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/run/23cache.tl b/t/run/23cache.tl index 1f85dcb..4be8fbd 100644 --- a/t/run/23cache.tl +++ b/t/run/23cache.tl @@ -3,7 +3,7 @@ my $schema = shift; eval "use DBD::SQLite"; plan skip_all => 'needs DBD::SQLite for testing' if $@; -plan tests => 15; +plan tests => 23; my $rs = $schema->resultset("Artist")->search( { artistid => 1 } @@ -13,6 +13,54 @@ my $artist = $rs->first; is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' ); +$rs = $schema->resultset('Artist')->search( undef, { cache => 1 } ); +my $artists = [ $rs->all ]; + +is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' ); + +$rs->clear_cache; + +is( scalar @{$rs->get_cache}, 0, 'clear_cache is functional' ); + +$rs->next; + +is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' ); + +pop( @$artists ); +$rs->set_cache( $artists ); + +is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' ); + +$cd = $schema->resultset('CD')->find(1); + +$rs->clear_cache; + +eval { + $rs->set_cache( [ $cd ] ); +}; + +is( scalar @{$rs->get_cache}, 0, 'set_cache() only accepts objects of correct type for the resultset' ); + +unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; +DBI->trace(1, 't/var/dbic.trace'); + +$rs = $schema->resultset('Artist')->search( undef, { cache => 1 } ); +while( $artist = $rs->next ) {} +$artist = $rs->first(); + +# count the SELECTs +DBI->trace(0, undef); +my $selects = 0; +$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, 'revisiting a row does not issue a query when cache => 1' ); + my @a = $schema->resultset("Artist")->search( { }, { @@ -57,7 +105,7 @@ is( $artist->count_related('cds'), 3, 'artist->count_related returns correct val # count the SELECTs DBI->trace(0, undef); -my $selects = 0; +$selects = 0; my $trace = IO::File->new('t/var/dbic.trace', '<') or die "Unable to read trace file"; while (<$trace>) { @@ -84,7 +132,9 @@ $rs = $schema->resultset("Artist")->search( } ); { +my $artist_count_before = $schema->resultset('Artist')->count; $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}}); +is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist'); my $artist = $schema->resultset("Artist")->search( { artistid => 4 },{prefetch=>[qw/cds/]} )->first;