8 my $schema = DBICTest->init_schema();
11 $schema->storage->debugcb( sub{ $queries++ } );
13 eval "use DBD::SQLite";
14 plan skip_all => 'needs DBD::SQLite for testing' if $@;
17 my $rs = $schema->resultset("Artist")->search(
21 my $artist = $rs->first;
23 ok( !defined($rs->get_cache), 'cache is not populated without cache attribute' );
25 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
26 my $artists = [ $rs->all ];
28 is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
32 ok( !defined($rs->get_cache), 'clear_cache is functional' );
36 is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
39 $rs->set_cache( $artists );
41 is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
43 my $cd = $schema->resultset('CD')->find(1);
48 $schema->storage->debug(1);
50 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
51 while( $artist = $rs->next ) {}
52 $artist = $rs->first();
54 is( $queries, 1, 'revisiting a row does not issue a query when cache => 1' );
56 $schema->storage->debug(0);
58 my @a = $schema->resultset("Artist")->search(
62 prefetch => [qw/ cds /],
66 is(scalar @a, 3, 'artist with cds: count parent objects');
68 $rs = $schema->resultset("Artist")->search(
72 prefetch => [qw/ cds /],
76 use Data::Dumper; $Data::Dumper::Deparse = 1;
78 # start test for prefetch SELECT count
80 $schema->storage->debug(1);
85 # make sure artist contains a related resultset for cds
86 is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
88 # check if $artist->cds->get_cache is populated
89 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
91 # ensure that $artist->cds returns correct number of objects
92 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
94 # ensure that $artist->cds->count returns correct value
95 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
97 # ensure that $artist->count_related('cds') returns correct value
98 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
100 is($queries, 1, 'only one SQL statement executed');
102 $schema->storage->debug(0);
104 # make sure related_resultset is deleted after object is updated
105 $artist->set_column('name', 'New Name');
108 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
110 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
111 $rs = $schema->resultset("Artist")->search(
114 join => { cds => 'tags' },
121 my $artist_count_before = $schema->resultset('Artist')->count;
122 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
123 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
124 my $artist = $schema->resultset("Artist")->search(
125 { artistid => 4 },{prefetch=>[qw/cds/]}
128 is($artist->cds, 0, 'No cds for this artist');
131 # SELECT count for nested has_many prefetch
133 $schema->storage->debug(1);
135 $artist = ($rs->all)[0];
137 is($queries, 1, 'only one SQL statement executed');
139 $schema->storage->debug(0);
142 #$artist = $rs->find(1);
145 $schema->storage->debug(1);
147 my $cds = $artist->cds;
148 my $tags = $cds->next->tags;
149 while( my $tag = $tags->next ) {
150 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
153 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
155 $tags = $cds->next->tags;
157 while( my $tag = $tags->next ) {
158 push @objs, $tag->id; #warn "tag: ", $tag->ID;
161 is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
163 $tags = $cds->next->tags;
165 while( my $tag = $tags->next ) {
166 push @objs, $tag->id; #warn "tag: ", $tag->ID;
169 is_deeply( \@objs, [ 2, 5, 8 ], 'third cd has correct tags' );
171 is( $queries, 0, 'no additional SQL statements while checking nested data' );
173 # start test for prefetch SELECT count
176 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
178 is( $queries, 1, 'only one select statement on find with inline has_many prefetch' );
180 # start test for prefetch SELECT count
183 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
184 $artist = $rs->find(1);
186 is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' );
188 $schema->storage->debug(0);