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