Commit | Line | Data |
4386b954 |
1 | $|=1; |
64acc2bc |
2 | sub run_tests { |
3 | my $schema = shift; |
4 | |
d52170d4 |
5 | my $queries; |
6 | $schema->storage->debugcb( sub{ $queries++ } ); |
7 | |
64acc2bc |
8 | eval "use DBD::SQLite"; |
9 | plan skip_all => 'needs DBD::SQLite for testing' if $@; |
4386b954 |
10 | plan tests => 25; |
64acc2bc |
11 | |
12 | my $rs = $schema->resultset("Artist")->search( |
13 | { artistid => 1 } |
14 | ); |
15 | |
16 | my $artist = $rs->first; |
17 | |
4386b954 |
18 | is( scalar @{ $rs->get_cache('all') }, 0, 'cache is not populated without cache attribute' ); |
64acc2bc |
19 | |
534ca143 |
20 | $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } ); |
21 | my $artists = [ $rs->all ]; |
22 | |
4386b954 |
23 | is( scalar @{$rs->get_cache('all')}, 3, 'all() populates cache for search with cache attribute' ); |
534ca143 |
24 | |
25 | $rs->clear_cache; |
26 | |
4386b954 |
27 | is( scalar @{$rs->get_cache('all')}, 0, 'clear_cache is functional' ); |
534ca143 |
28 | |
29 | $rs->next; |
30 | |
4386b954 |
31 | is( scalar @{$rs->get_cache('all')}, 3, 'next() populates cache for search with cache attribute' ); |
534ca143 |
32 | |
33 | pop( @$artists ); |
4386b954 |
34 | $rs->set_cache('all', $artists ); |
534ca143 |
35 | |
4386b954 |
36 | is( scalar @{$rs->get_cache('all')}, 2, 'set_cache() is functional' ); |
534ca143 |
37 | |
38 | $cd = $schema->resultset('CD')->find(1); |
39 | |
40 | $rs->clear_cache; |
41 | |
42 | eval { |
4386b954 |
43 | $rs->set_cache('all', [ $cd ] ); |
534ca143 |
44 | }; |
45 | |
4386b954 |
46 | is( scalar @{$rs->get_cache('all')}, 0, 'set_cache() only accepts objects of correct type for the resultset' ); |
534ca143 |
47 | |
d52170d4 |
48 | $queries = 0; |
49 | $schema->storage->debug(1); |
534ca143 |
50 | |
51 | $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } ); |
52 | while( $artist = $rs->next ) {} |
53 | $artist = $rs->first(); |
54 | |
d52170d4 |
55 | is( $queries, 1, 'revisiting a row does not issue a query when cache => 1' ); |
534ca143 |
56 | |
d52170d4 |
57 | $schema->storage->debug(0); |
534ca143 |
58 | |
f109ee4a |
59 | my @a = $schema->resultset("Artist")->search( |
60 | { }, |
61 | { |
62 | join => [ qw/ cds /], |
63 | prefetch => [qw/ cds /], |
64 | } |
65 | ); |
66 | |
67 | is(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 |
77 | use Data::Dumper; $Data::Dumper::Deparse = 1; |
64acc2bc |
78 | |
79 | # start test for prefetch SELECT count |
d52170d4 |
80 | $queries = 0; |
81 | $schema->storage->debug(1); |
64acc2bc |
82 | |
83 | $artist = $rs->first; |
84 | $rs->reset(); |
85 | |
86 | # make sure artist contains a related resultset for cds |
87 | is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' ); |
88 | |
89 | # check if $artist->cds->get_cache is populated |
4386b954 |
90 | is( scalar @{$artist->cds->get_cache('all')}, 3, 'cache for artist->cds contains correct number of records'); |
64acc2bc |
91 | |
92 | # ensure that $artist->cds returns correct number of objects |
93 | is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' ); |
94 | |
95 | # ensure that $artist->cds->count returns correct value |
96 | is( $artist->cds->count, 3, 'artist->cds->count returns correct value' ); |
97 | |
98 | # ensure that $artist->count_related('cds') returns correct value |
99 | is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' ); |
100 | |
d52170d4 |
101 | is($queries, 1, 'only one SQL statement executed'); |
102 | |
103 | $schema->storage->debug(0); |
64acc2bc |
104 | |
105 | # make sure related_resultset is deleted after object is updated |
106 | $artist->set_column('name', 'New Name'); |
107 | $artist->update(); |
108 | |
109 | is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' ); |
110 | |
111 | # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks |
112 | $rs = $schema->resultset("Artist")->search( |
113 | { artistid => 1 }, |
114 | { |
3c3c416e |
115 | join => { cds => 'tags' }, |
64acc2bc |
116 | prefetch => { |
117 | cds => 'tags' |
118 | }, |
64acc2bc |
119 | } |
120 | ); |
62e87ea8 |
121 | { |
717f3498 |
122 | my $artist_count_before = $schema->resultset('Artist')->count; |
62e87ea8 |
123 | $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}}); |
717f3498 |
124 | is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist'); |
62e87ea8 |
125 | my $artist = $schema->resultset("Artist")->search( |
126 | { artistid => 4 },{prefetch=>[qw/cds/]} |
127 | )->first; |
128 | |
129 | is($artist->cds, 0, 'No cds for this artist'); |
130 | } |
64acc2bc |
131 | |
f9cc31dd |
132 | # SELECT count for nested has_many prefetch |
d52170d4 |
133 | $queries = 0; |
134 | $schema->storage->debug(1); |
f9cc31dd |
135 | |
5a5bec6c |
136 | $artist = ($rs->all)[0]; |
f9cc31dd |
137 | |
d52170d4 |
138 | is($queries, 1, 'only one SQL statement executed'); |
139 | |
140 | $schema->storage->debug(0); |
f9cc31dd |
141 | |
142 | my @objs; |
5a5bec6c |
143 | #$artist = $rs->find(1); |
f9cc31dd |
144 | |
d52170d4 |
145 | $queries = 0; |
146 | $schema->storage->debug(1); |
f9cc31dd |
147 | |
148 | my $cds = $artist->cds; |
149 | my $tags = $cds->next->tags; |
150 | while( my $tag = $tags->next ) { |
5a5bec6c |
151 | push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag; |
f9cc31dd |
152 | } |
153 | |
5a5bec6c |
154 | is_deeply( \@objs, [ 3 ], 'first cd has correct tags' ); |
f9cc31dd |
155 | |
156 | $tags = $cds->next->tags; |
157 | @objs = (); |
158 | while( my $tag = $tags->next ) { |
159 | push @objs, $tag->id; #warn "tag: ", $tag->ID; |
160 | } |
161 | |
162 | is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' ); |
163 | |
d52170d4 |
164 | is( $queries, 0, 'no additional SQL statements while checking nested data' ); |
f9cc31dd |
165 | |
d2b3ea14 |
166 | # start test for prefetch SELECT count |
d52170d4 |
167 | $queries = 0; |
d2b3ea14 |
168 | |
169 | $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] }); |
170 | |
d52170d4 |
171 | is( $queries, 1, 'only one select statement on find with inline has_many prefetch' ); |
d2b3ea14 |
172 | |
173 | # start test for prefetch SELECT count |
d52170d4 |
174 | $queries = 0; |
d2b3ea14 |
175 | |
176 | $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] }); |
177 | $artist = $rs->find(1); |
178 | |
d52170d4 |
179 | is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' ); |
d2b3ea14 |
180 | |
d52170d4 |
181 | $schema->storage->debug(0); |
d2b3ea14 |
182 | |
4386b954 |
183 | |
184 | # start test for prefetch SELECT count |
185 | unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; |
186 | DBI->trace(1, 't/var/dbic.trace'); |
187 | |
188 | $rs = $schema->resultset("Artist"); |
189 | |
190 | $rs->clear_cache; |
191 | |
192 | $rs->find(1, { cache => 1 }); |
193 | |
194 | is (scalar keys %{$rs->get_cache('find')}, 1, 'find created one cached value'); |
195 | |
196 | $rs->find(1, {cache => 1}); |
197 | |
198 | # count the SELECTs |
199 | DBI->trace(0, undef); |
200 | $selects = 0; |
201 | $trace = IO::File->new('t/var/dbic.trace', '<') |
202 | or die "Unable to read trace file"; |
203 | while (<$trace>) { |
204 | $selects++ if /SELECT/; |
64acc2bc |
205 | } |
4386b954 |
206 | $trace->close; |
207 | # unlink 't/var/dbic.trace'; |
64acc2bc |
208 | |
4386b954 |
209 | is( $selects, 1, 'only one select statement on find' ); |
210 | |
211 | } |
64acc2bc |
212 | 1; |