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