Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / 83cache.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
70350518 3use strict;
8273e845 4use warnings;
70350518 5
6use Test::More;
c0329273 7
70350518 8use DBICTest;
9
a47e1233 10my $schema = DBICTest->init_schema();
64acc2bc 11
64acc2bc 12my $rs = $schema->resultset("Artist")->search(
13 { artistid => 1 }
14);
15
16my $artist = $rs->first;
17
0823196c 18ok( !defined($rs->get_cache), 'cache is not populated without cache attribute' );
64acc2bc 19
534ca143 20$rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
21my $artists = [ $rs->all ];
22
23is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
24
25$rs->clear_cache;
26
0823196c 27ok( !defined($rs->get_cache), 'clear_cache is functional' );
534ca143 28
29$rs->next;
30
31is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
32
33pop( @$artists );
34$rs->set_cache( $artists );
35
36is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
37
58d387fe 38my $cd = $schema->resultset('CD')->find(1);
534ca143 39
40$rs->clear_cache;
41
49eeb48d 42$schema->is_executed_querycount( sub {
534ca143 43
49eeb48d 44 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
45 while( $artist = $rs->next ) {}
46 $artist = $rs->first();
47}, 1, 'revisiting a row does not issue a query when cache => 1' );
534ca143 48
f109ee4a 49my @a = $schema->resultset("Artist")->search(
50 { },
51 {
52 join => [ qw/ cds /],
53 prefetch => [qw/ cds /],
54 }
55);
56
57is(scalar @a, 3, 'artist with cds: count parent objects');
58
64acc2bc 59$rs = $schema->resultset("Artist")->search(
60 { 'artistid' => 1 },
61 {
3c3c416e 62 join => [ qw/ cds /],
64acc2bc 63 prefetch => [qw/ cds /],
64acc2bc 64 }
65);
66
49eeb48d 67# prefetch SELECT count
68$schema->is_executed_querycount( sub {
69 $artist = $rs->first;
70 $rs->reset();
64acc2bc 71
49eeb48d 72 # make sure artist contains a related resultset for cds
73 isa_ok( $artist->{related_resultsets}{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
64acc2bc 74
49eeb48d 75 # check if $artist->cds->get_cache is populated
76 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
64acc2bc 77
49eeb48d 78 # ensure that $artist->cds returns correct number of objects
79 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
64acc2bc 80
49eeb48d 81 # ensure that $artist->cds->count returns correct value
82 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
64acc2bc 83
49eeb48d 84 # ensure that $artist->count_related('cds') returns correct value
85 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
64acc2bc 86
49eeb48d 87}, 1, 'only one SQL statement executed');
d52170d4 88
64acc2bc 89
90# make sure related_resultset is deleted after object is updated
91$artist->set_column('name', 'New Name');
92$artist->update();
93
94is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
95
96# todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
97$rs = $schema->resultset("Artist")->search(
98 { artistid => 1 },
99 {
3c3c416e 100 join => { cds => 'tags' },
64acc2bc 101 prefetch => {
102 cds => 'tags'
103 },
fb88ca2c 104 order_by => { -desc => 'cds.cdid' },
64acc2bc 105 }
106);
62e87ea8 107{
717f3498 108my $artist_count_before = $schema->resultset('Artist')->count;
62e87ea8 109$schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
717f3498 110is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
62e87ea8 111my $artist = $schema->resultset("Artist")->search(
112 { artistid => 4 },{prefetch=>[qw/cds/]}
113)->first;
114
115is($artist->cds, 0, 'No cds for this artist');
116}
64acc2bc 117
f9cc31dd 118# SELECT count for nested has_many prefetch
49eeb48d 119$schema->is_executed_querycount( sub {
120 $artist = ($rs->all)[0];
121}, 1, 'only one SQL statement executed');
122
123$schema->is_executed_querycount( sub {
124 my @objs;
125 my $cds = $artist->cds;
126 my $tags = $cds->next->tags;
127 while( my $tag = $tags->next ) {
128 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
129 }
f9cc31dd 130
49eeb48d 131 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
f9cc31dd 132
49eeb48d 133 $tags = $cds->next->tags;
134 @objs = ();
135 while( my $tag = $tags->next ) {
136 push @objs, $tag->id; #warn "tag: ", $tag->ID;
137 }
d2b3ea14 138
49eeb48d 139 is_deeply( [ sort @objs] , [ 2, 5, 8 ], 'third cd has correct tags' );
d2b3ea14 140
49eeb48d 141 $tags = $cds->next->tags;
142 @objs = ();
143 while( my $tag = $tags->next ) {
144 push @objs, $tag->id; #warn "tag: ", $tag->ID;
145 }
d2b3ea14 146
49eeb48d 147 is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
148}, 0, 'no additional SQL statements while checking nested data' );
d2b3ea14 149
49eeb48d 150$schema->is_executed_querycount( sub {
151 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
152}, 1, 'only one select statement on find with inline has_many prefetch' );
d2b3ea14 153
49eeb48d 154$schema->is_executed_querycount( sub {
155 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
156 $artist = $rs->find(1);
157}, 1, 'only one select statement on find with has_many prefetch on resultset' );
d2b3ea14 158
49eeb48d 159done_testing;