8 my $schema = DBICTest->init_schema();
10 my $rs = $schema->resultset("Artist")->search(
14 my $artist = $rs->first;
16 ok( !defined($rs->get_cache), 'cache is not populated without cache attribute' );
18 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
19 my $artists = [ $rs->all ];
21 is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
25 ok( !defined($rs->get_cache), 'clear_cache is functional' );
29 is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
32 $rs->set_cache( $artists );
34 is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
36 my $cd = $schema->resultset('CD')->find(1);
40 $schema->is_executed_querycount( sub {
42 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
43 while( $artist = $rs->next ) {}
44 $artist = $rs->first();
45 }, 1, 'revisiting a row does not issue a query when cache => 1' );
47 my @a = $schema->resultset("Artist")->search(
51 prefetch => [qw/ cds /],
55 is(scalar @a, 3, 'artist with cds: count parent objects');
57 $rs = $schema->resultset("Artist")->search(
61 prefetch => [qw/ cds /],
65 # prefetch SELECT count
66 $schema->is_executed_querycount( sub {
70 # make sure artist contains a related resultset for cds
71 isa_ok( $artist->{related_resultsets}{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
73 # check if $artist->cds->get_cache is populated
74 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
76 # ensure that $artist->cds returns correct number of objects
77 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
79 # ensure that $artist->cds->count returns correct value
80 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
82 # ensure that $artist->count_related('cds') returns correct value
83 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
85 }, 1, 'only one SQL statement executed');
88 # make sure related_resultset is deleted after object is updated
89 $artist->set_column('name', 'New Name');
92 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
94 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
95 $rs = $schema->resultset("Artist")->search(
98 join => { cds => 'tags' },
102 order_by => { -desc => 'cds.cdid' },
106 my $artist_count_before = $schema->resultset('Artist')->count;
107 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
108 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
109 my $artist = $schema->resultset("Artist")->search(
110 { artistid => 4 },{prefetch=>[qw/cds/]}
113 is($artist->cds, 0, 'No cds for this artist');
116 # SELECT count for nested has_many prefetch
117 $schema->is_executed_querycount( sub {
118 $artist = ($rs->all)[0];
119 }, 1, 'only one SQL statement executed');
121 $schema->is_executed_querycount( sub {
123 my $cds = $artist->cds;
124 my $tags = $cds->next->tags;
125 while( my $tag = $tags->next ) {
126 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
129 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
131 $tags = $cds->next->tags;
133 while( my $tag = $tags->next ) {
134 push @objs, $tag->id; #warn "tag: ", $tag->ID;
137 is_deeply( [ sort @objs] , [ 2, 5, 8 ], 'third cd has correct tags' );
139 $tags = $cds->next->tags;
141 while( my $tag = $tags->next ) {
142 push @objs, $tag->id; #warn "tag: ", $tag->ID;
145 is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
146 }, 0, 'no additional SQL statements while checking nested data' );
148 $schema->is_executed_querycount( sub {
149 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
150 }, 1, 'only one select statement on find with inline has_many prefetch' );
152 $schema->is_executed_querycount( sub {
153 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
154 $artist = $rs->find(1);
155 }, 1, 'only one select statement on find with has_many prefetch on resultset' );