8 my $schema = DBICTest->init_schema();
11 $schema->storage->debugcb( sub{ $queries++ } );
12 my $sdebug = $schema->storage->debug;
16 my $rs = $schema->resultset("Artist")->search(
20 my $artist = $rs->first;
22 ok( !defined($rs->get_cache), 'cache is not populated without cache attribute' );
24 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
25 my $artists = [ $rs->all ];
27 is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
31 ok( !defined($rs->get_cache), 'clear_cache is functional' );
35 is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
38 $rs->set_cache( $artists );
40 is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
42 my $cd = $schema->resultset('CD')->find(1);
47 $schema->storage->debug(1);
49 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
50 while( $artist = $rs->next ) {}
51 $artist = $rs->first();
53 is( $queries, 1, 'revisiting a row does not issue a query when cache => 1' );
55 $schema->storage->debug($sdebug);
57 my @a = $schema->resultset("Artist")->search(
61 prefetch => [qw/ cds /],
65 is(scalar @a, 3, 'artist with cds: count parent objects');
67 $rs = $schema->resultset("Artist")->search(
71 prefetch => [qw/ cds /],
75 use Data::Dumper; $Data::Dumper::Deparse = 1;
77 # start test for prefetch SELECT count
79 $schema->storage->debug(1);
84 # make sure artist contains a related resultset for cds
85 isa_ok( $artist->{related_resultsets}{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
87 # check if $artist->cds->get_cache is populated
88 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
90 # ensure that $artist->cds returns correct number of objects
91 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
93 # ensure that $artist->cds->count returns correct value
94 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
96 # ensure that $artist->count_related('cds') returns correct value
97 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
99 is($queries, 1, 'only one SQL statement executed');
101 $schema->storage->debug($sdebug);
103 # make sure related_resultset is deleted after object is updated
104 $artist->set_column('name', 'New Name');
107 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
109 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
110 $rs = $schema->resultset("Artist")->search(
113 join => { cds => 'tags' },
120 my $artist_count_before = $schema->resultset('Artist')->count;
121 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
122 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
123 my $artist = $schema->resultset("Artist")->search(
124 { artistid => 4 },{prefetch=>[qw/cds/]}
127 is($artist->cds, 0, 'No cds for this artist');
130 # SELECT count for nested has_many prefetch
132 $schema->storage->debug(1);
134 $artist = ($rs->all)[0];
136 is($queries, 1, 'only one SQL statement executed');
138 $schema->storage->debug($sdebug);
141 #$artist = $rs->find(1);
144 $schema->storage->debug(1);
146 my $cds = $artist->cds;
147 my $tags = $cds->next->tags;
148 while( my $tag = $tags->next ) {
149 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
152 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
154 $tags = $cds->next->tags;
156 while( my $tag = $tags->next ) {
157 push @objs, $tag->id; #warn "tag: ", $tag->ID;
160 is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
162 $tags = $cds->next->tags;
164 while( my $tag = $tags->next ) {
165 push @objs, $tag->id; #warn "tag: ", $tag->ID;
168 is_deeply( \@objs, [ 2, 5, 8 ], 'third cd has correct tags' );
170 is( $queries, 0, 'no additional SQL statements while checking nested data' );
172 # start test for prefetch SELECT count
175 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
177 is( $queries, 1, 'only one select statement on find with inline has_many prefetch' );
179 # start test for prefetch SELECT count
182 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
183 $artist = $rs->find(1);
185 is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' );
187 $schema->storage->debug($sdebug);