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