5 $schema->storage->debugcb( sub{ $queries++ } );
7 eval "use DBD::SQLite";
8 plan skip_all => 'needs DBD::SQLite for testing' if $@;
11 my $rs = $schema->resultset("Artist")->search(
15 my $artist = $rs->first;
17 ok( !defined($rs->get_cache), 'cache is not populated without cache attribute' );
19 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
20 my $artists = [ $rs->all ];
22 is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
26 ok( !defined($rs->get_cache), 'clear_cache is functional' );
30 is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
33 $rs->set_cache( $artists );
35 is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
37 $cd = $schema->resultset('CD')->find(1);
42 $schema->storage->debug(1);
44 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
45 while( $artist = $rs->next ) {}
46 $artist = $rs->first();
48 is( $queries, 1, 'revisiting a row does not issue a query when cache => 1' );
50 $schema->storage->debug(0);
52 my @a = $schema->resultset("Artist")->search(
56 prefetch => [qw/ cds /],
60 is(scalar @a, 3, 'artist with cds: count parent objects');
62 $rs = $schema->resultset("Artist")->search(
66 prefetch => [qw/ cds /],
70 use Data::Dumper; $Data::Dumper::Deparse = 1;
72 # start test for prefetch SELECT count
74 $schema->storage->debug(1);
79 # make sure artist contains a related resultset for cds
80 is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
82 # check if $artist->cds->get_cache is populated
83 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
85 # ensure that $artist->cds returns correct number of objects
86 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
88 # ensure that $artist->cds->count returns correct value
89 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
91 # ensure that $artist->count_related('cds') returns correct value
92 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
94 is($queries, 1, 'only one SQL statement executed');
96 $schema->storage->debug(0);
98 # make sure related_resultset is deleted after object is updated
99 $artist->set_column('name', 'New Name');
102 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
104 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
105 $rs = $schema->resultset("Artist")->search(
108 join => { cds => 'tags' },
115 my $artist_count_before = $schema->resultset('Artist')->count;
116 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
117 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
118 my $artist = $schema->resultset("Artist")->search(
119 { artistid => 4 },{prefetch=>[qw/cds/]}
122 is($artist->cds, 0, 'No cds for this artist');
125 # SELECT count for nested has_many prefetch
127 $schema->storage->debug(1);
129 $artist = ($rs->all)[0];
131 is($queries, 1, 'only one SQL statement executed');
133 $schema->storage->debug(0);
136 #$artist = $rs->find(1);
139 $schema->storage->debug(1);
141 my $cds = $artist->cds;
142 my $tags = $cds->next->tags;
143 while( my $tag = $tags->next ) {
144 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
147 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
149 $tags = $cds->next->tags;
151 while( my $tag = $tags->next ) {
152 push @objs, $tag->id; #warn "tag: ", $tag->ID;
155 is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
157 is( $queries, 0, 'no additional SQL statements while checking nested data' );
159 # start test for prefetch SELECT count
162 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
164 is( $queries, 1, 'only one select statement on find with inline has_many prefetch' );
166 # start test for prefetch SELECT count
169 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
170 $artist = $rs->find(1);
172 is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' );
174 $schema->storage->debug(0);