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