8 my $schema = DBICTest->init_schema();
11 my $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);
48 $schema->storage->debugcb ($debugcb);
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($sdebug);
57 $schema->storage->debugcb (undef);
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 # start test for prefetch SELECT count
79 $schema->storage->debug(1);
80 $schema->storage->debugcb ($debugcb);
85 # make sure artist contains a related resultset for cds
86 isa_ok( $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($sdebug);
103 $schema->storage->debugcb (undef);
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' },
119 order_by => { -desc => 'cds.cdid' },
123 my $artist_count_before = $schema->resultset('Artist')->count;
124 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
125 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
126 my $artist = $schema->resultset("Artist")->search(
127 { artistid => 4 },{prefetch=>[qw/cds/]}
130 is($artist->cds, 0, 'No cds for this artist');
133 # SELECT count for nested has_many prefetch
135 $schema->storage->debug(1);
136 $schema->storage->debugcb ($debugcb);
138 $artist = ($rs->all)[0];
140 is($queries, 1, 'only one SQL statement executed');
145 my $cds = $artist->cds;
146 my $tags = $cds->next->tags;
147 while( my $tag = $tags->next ) {
148 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
151 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
153 $tags = $cds->next->tags;
155 while( my $tag = $tags->next ) {
156 push @objs, $tag->id; #warn "tag: ", $tag->ID;
159 is_deeply( [ sort @objs] , [ 2, 5, 8 ], 'third cd has correct tags' );
161 $tags = $cds->next->tags;
163 while( my $tag = $tags->next ) {
164 push @objs, $tag->id; #warn "tag: ", $tag->ID;
167 is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
169 is( $queries, 0, 'no additional SQL statements while checking nested data' );
171 # start test for prefetch SELECT count
174 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
176 is( $queries, 1, 'only one select statement on find with inline has_many prefetch' );
178 # start test for prefetch SELECT count
181 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
182 $artist = $rs->find(1);
184 is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' );
186 $schema->storage->debug($sdebug);
187 $schema->storage->debugcb (undef);