4 eval "use DBD::SQLite";
5 plan skip_all => 'needs DBD::SQLite for testing' if $@;
8 my $rs = $schema->resultset("Artist")->search(
12 my $artist = $rs->first;
14 is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' );
16 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
17 my $artists = [ $rs->all ];
19 is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
23 is( scalar @{$rs->get_cache}, 0, 'clear_cache is functional' );
27 is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
30 $rs->set_cache( $artists );
32 is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
34 $cd = $schema->resultset('CD')->find(1);
39 $rs->set_cache( [ $cd ] );
42 is( scalar @{$rs->get_cache}, 0, 'set_cache() only accepts objects of correct type for the resultset' );
44 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
45 DBI->trace(1, 't/var/dbic.trace');
47 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
48 while( $artist = $rs->next ) {}
49 $artist = $rs->first();
54 $trace = IO::File->new('t/var/dbic.trace', '<')
55 or die "Unable to read trace file";
57 $selects++ if /SELECT/;
60 unlink 't/var/dbic.trace';
62 is( $selects, 1, 'revisiting a row does not issue a query when cache => 1' );
64 my @a = $schema->resultset("Artist")->search(
68 prefetch => [qw/ cds /],
72 is(scalar @a, 3, 'artist with cds: count parent objects');
74 $rs = $schema->resultset("Artist")->search(
78 prefetch => [qw/ cds /],
82 use Data::Dumper; $Data::Dumper::Deparse = 1;
84 # start test for prefetch SELECT count
85 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
86 DBI->trace(1, 't/var/dbic.trace');
91 # make sure artist contains a related resultset for cds
92 is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
94 # check if $artist->cds->get_cache is populated
95 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
97 # ensure that $artist->cds returns correct number of objects
98 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
100 # ensure that $artist->cds->count returns correct value
101 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
103 # ensure that $artist->count_related('cds') returns correct value
104 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
107 DBI->trace(0, undef);
109 my $trace = IO::File->new('t/var/dbic.trace', '<')
110 or die "Unable to read trace file";
112 $selects++ if /SELECT/;
115 unlink 't/var/dbic.trace';
116 is($selects, 1, 'only one SQL statement executed');
118 # make sure related_resultset is deleted after object is updated
119 $artist->set_column('name', 'New Name');
122 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
124 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
125 $rs = $schema->resultset("Artist")->search(
128 join => { cds => 'tags' },
135 my $artist_count_before = $schema->resultset('Artist')->count;
136 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
137 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
138 my $artist = $schema->resultset("Artist")->search(
139 { artistid => 4 },{prefetch=>[qw/cds/]}
142 is($artist->cds, 0, 'No cds for this artist');
145 # SELECT count for nested has_many prefetch
146 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
147 DBI->trace(1, 't/var/dbic.trace');
149 $artist = ($rs->all)[0];
152 DBI->trace(0, undef);
154 $trace = IO::File->new('t/var/dbic.trace', '<')
155 or die "Unable to read trace file";
157 $selects++ if /SELECT/;
160 unlink 't/var/dbic.trace';
161 is($selects, 1, 'only one SQL statement executed');
164 #$artist = $rs->find(1);
166 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
167 DBI->trace(1, 't/var/dbic.trace');
169 my $cds = $artist->cds;
170 my $tags = $cds->next->tags;
171 while( my $tag = $tags->next ) {
172 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
175 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
177 $tags = $cds->next->tags;
179 while( my $tag = $tags->next ) {
180 push @objs, $tag->id; #warn "tag: ", $tag->ID;
183 is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
186 DBI->trace(0, undef);
188 $trace = IO::File->new('t/var/dbic.trace', '<')
189 or die "Unable to read trace file";
191 $selects++ if /SELECT/;
194 unlink 't/var/dbic.trace';
196 is( $selects, 0, 'no additional SQL statements while checking nested data' );
198 # start test for prefetch SELECT count
199 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
200 DBI->trace(1, 't/var/dbic.trace');
202 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
205 DBI->trace(0, undef);
207 $trace = IO::File->new('t/var/dbic.trace', '<')
208 or die "Unable to read trace file";
210 $selects++ if /SELECT/;
213 unlink 't/var/dbic.trace';
215 is( $selects, 1, 'only one select statement on find with inline has_many prefetch' );
217 # start test for prefetch SELECT count
218 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
219 DBI->trace(1, 't/var/dbic.trace');
221 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
222 $artist = $rs->find(1);
225 DBI->trace(0, undef);
227 $trace = IO::File->new('t/var/dbic.trace', '<')
228 or die "Unable to read trace file";
230 $selects++ if /SELECT/;
233 unlink 't/var/dbic.trace';
235 is( $selects, 1, 'only one select statement on find with has_many prefetch on resultset' );