Another candidate for somethingawful.com (fix left join-ed count)
[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;
11$schema->storage->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);
534ca143 48
49$rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
50while( $artist = $rs->next ) {}
51$artist = $rs->first();
52
d52170d4 53is( $queries, 1, 'revisiting a row does not issue a query when cache => 1' );
534ca143 54
a2287768 55$schema->storage->debug($sdebug);
534ca143 56
f109ee4a 57my @a = $schema->resultset("Artist")->search(
58 { },
59 {
60 join => [ qw/ cds /],
61 prefetch => [qw/ cds /],
62 }
63);
64
65is(scalar @a, 3, 'artist with cds: count parent objects');
66
64acc2bc 67$rs = $schema->resultset("Artist")->search(
68 { 'artistid' => 1 },
69 {
3c3c416e 70 join => [ qw/ cds /],
64acc2bc 71 prefetch => [qw/ cds /],
64acc2bc 72 }
73);
74
f9cc31dd 75use Data::Dumper; $Data::Dumper::Deparse = 1;
64acc2bc 76
77# start test for prefetch SELECT count
d52170d4 78$queries = 0;
79$schema->storage->debug(1);
64acc2bc 80
81$artist = $rs->first;
82$rs->reset();
83
84# make sure artist contains a related resultset for cds
660cf1be 85isa_ok( $artist->{related_resultsets}{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
64acc2bc 86
87# check if $artist->cds->get_cache is populated
88is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
89
90# ensure that $artist->cds returns correct number of objects
91is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
92
93# ensure that $artist->cds->count returns correct value
94is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
95
96# ensure that $artist->count_related('cds') returns correct value
97is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
98
d52170d4 99is($queries, 1, 'only one SQL statement executed');
100
a2287768 101$schema->storage->debug($sdebug);
64acc2bc 102
103# make sure related_resultset is deleted after object is updated
104$artist->set_column('name', 'New Name');
105$artist->update();
106
107is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
108
109# todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
110$rs = $schema->resultset("Artist")->search(
111 { artistid => 1 },
112 {
3c3c416e 113 join => { cds => 'tags' },
64acc2bc 114 prefetch => {
115 cds => 'tags'
116 },
64acc2bc 117 }
118);
62e87ea8 119{
717f3498 120my $artist_count_before = $schema->resultset('Artist')->count;
62e87ea8 121$schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
717f3498 122is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
62e87ea8 123my $artist = $schema->resultset("Artist")->search(
124 { artistid => 4 },{prefetch=>[qw/cds/]}
125)->first;
126
127is($artist->cds, 0, 'No cds for this artist');
128}
64acc2bc 129
f9cc31dd 130# SELECT count for nested has_many prefetch
d52170d4 131$queries = 0;
132$schema->storage->debug(1);
f9cc31dd 133
5a5bec6c 134$artist = ($rs->all)[0];
f9cc31dd 135
d52170d4 136is($queries, 1, 'only one SQL statement executed');
137
a2287768 138$schema->storage->debug($sdebug);
f9cc31dd 139
140my @objs;
5a5bec6c 141#$artist = $rs->find(1);
f9cc31dd 142
d52170d4 143$queries = 0;
144$schema->storage->debug(1);
f9cc31dd 145
146my $cds = $artist->cds;
147my $tags = $cds->next->tags;
148while( my $tag = $tags->next ) {
5a5bec6c 149 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
f9cc31dd 150}
151
5a5bec6c 152is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
f9cc31dd 153
154$tags = $cds->next->tags;
155@objs = ();
156while( my $tag = $tags->next ) {
157 push @objs, $tag->id; #warn "tag: ", $tag->ID;
158}
159
b25e9fa0 160is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
161
162$tags = $cds->next->tags;
163@objs = ();
164while( my $tag = $tags->next ) {
165 push @objs, $tag->id; #warn "tag: ", $tag->ID;
166}
167
168is_deeply( \@objs, [ 2, 5, 8 ], 'third cd has correct tags' );
f9cc31dd 169
d52170d4 170is( $queries, 0, 'no additional SQL statements while checking nested data' );
f9cc31dd 171
d2b3ea14 172# start test for prefetch SELECT count
d52170d4 173$queries = 0;
d2b3ea14 174
175$artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
176
d52170d4 177is( $queries, 1, 'only one select statement on find with inline has_many prefetch' );
d2b3ea14 178
179# start test for prefetch SELECT count
d52170d4 180$queries = 0;
d2b3ea14 181
182$rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
183$artist = $rs->find(1);
184
d52170d4 185is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' );
d2b3ea14 186
a2287768 187$schema->storage->debug($sdebug);
d2b3ea14 188