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