6 $schema->storage->debugcb( sub{ $queries++ } );
8 eval "use DBD::SQLite";
9 plan skip_all => 'needs DBD::SQLite for testing' if $@;
12 my $rs = $schema->resultset("Artist")->search(
16 my $artist = $rs->first;
18 is( scalar @{ $rs->get_cache('all') }, 0, 'cache is not populated without cache attribute' );
20 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
21 my $artists = [ $rs->all ];
23 is( scalar @{$rs->get_cache('all')}, 3, 'all() populates cache for search with cache attribute' );
27 is( scalar @{$rs->get_cache('all')}, 0, 'clear_cache is functional' );
31 is( scalar @{$rs->get_cache('all')}, 3, 'next() populates cache for search with cache attribute' );
34 $rs->set_cache('all', $artists );
36 is( scalar @{$rs->get_cache('all')}, 2, 'set_cache() is functional' );
38 $cd = $schema->resultset('CD')->find(1);
43 $rs->set_cache('all', [ $cd ] );
46 is( scalar @{$rs->get_cache('all')}, 0, 'set_cache() only accepts objects of correct type for the resultset' );
49 $schema->storage->debug(1);
51 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
52 while( $artist = $rs->next ) {}
53 $artist = $rs->first();
55 is( $queries, 1, 'revisiting a row does not issue a query when cache => 1' );
57 $schema->storage->debug(0);
59 my @a = $schema->resultset("Artist")->search(
63 prefetch => [qw/ cds /],
67 is(scalar @a, 3, 'artist with cds: count parent objects');
69 $rs = $schema->resultset("Artist")->search(
73 prefetch => [qw/ cds /],
77 use Data::Dumper; $Data::Dumper::Deparse = 1;
79 # start test for prefetch SELECT count
81 $schema->storage->debug(1);
86 # make sure artist contains a related resultset for cds
87 is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
89 # check if $artist->cds->get_cache is populated
90 is( scalar @{$artist->cds->get_cache('all')}, 3, 'cache for artist->cds contains correct number of records');
92 # ensure that $artist->cds returns correct number of objects
93 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
95 # ensure that $artist->cds->count returns correct value
96 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
98 # ensure that $artist->count_related('cds') returns correct value
99 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
101 is($queries, 1, 'only one SQL statement executed');
103 $schema->storage->debug(0);
105 # make sure related_resultset is deleted after object is updated
106 $artist->set_column('name', 'New Name');
109 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
111 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
112 $rs = $schema->resultset("Artist")->search(
115 join => { cds => 'tags' },
122 my $artist_count_before = $schema->resultset('Artist')->count;
123 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
124 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
125 my $artist = $schema->resultset("Artist")->search(
126 { artistid => 4 },{prefetch=>[qw/cds/]}
129 is($artist->cds, 0, 'No cds for this artist');
132 # SELECT count for nested has_many prefetch
134 $schema->storage->debug(1);
136 $artist = ($rs->all)[0];
138 is($queries, 1, 'only one SQL statement executed');
140 $schema->storage->debug(0);
143 #$artist = $rs->find(1);
146 $schema->storage->debug(1);
148 my $cds = $artist->cds;
149 my $tags = $cds->next->tags;
150 while( my $tag = $tags->next ) {
151 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
154 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
156 $tags = $cds->next->tags;
158 while( my $tag = $tags->next ) {
159 push @objs, $tag->id; #warn "tag: ", $tag->ID;
162 is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
164 is( $queries, 0, 'no additional SQL statements while checking nested data' );
166 # start test for prefetch SELECT count
169 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
171 is( $queries, 1, 'only one select statement on find with inline has_many prefetch' );
173 # start test for prefetch SELECT count
176 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
177 $artist = $rs->find(1);
179 is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' );
181 $schema->storage->debug(0);
184 # start test for prefetch SELECT count
185 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
186 DBI->trace(1, 't/var/dbic.trace');
188 $rs = $schema->resultset("Artist");
192 $rs->find(1, { cache => 1 });
194 is (scalar keys %{$rs->get_cache('find')}, 1, 'find created one cached value');
196 $rs->find(1, {cache => 1});
199 DBI->trace(0, undef);
201 $trace = IO::File->new('t/var/dbic.trace', '<')
202 or die "Unable to read trace file";
204 $selects++ if /SELECT/;
207 # unlink 't/var/dbic.trace';
209 is( $selects, 1, 'only one select statement on find' );