Trivial documentation fixes
[dbsrgits/DBIx-Class.git] / t / run / 23cache.tl
CommitLineData
64acc2bc 1sub run_tests {
2my $schema = shift;
3
4eval "use DBD::SQLite";
5plan skip_all => 'needs DBD::SQLite for testing' if $@;
534ca143 6plan tests => 23;
64acc2bc 7
8my $rs = $schema->resultset("Artist")->search(
9 { artistid => 1 }
10);
11
12my $artist = $rs->first;
13
14is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' );
15
534ca143 16$rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
17my $artists = [ $rs->all ];
18
19is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
20
21$rs->clear_cache;
22
23is( scalar @{$rs->get_cache}, 0, 'clear_cache is functional' );
24
25$rs->next;
26
27is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
28
29pop( @$artists );
30$rs->set_cache( $artists );
31
32is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
33
34$cd = $schema->resultset('CD')->find(1);
35
36$rs->clear_cache;
37
38eval {
39 $rs->set_cache( [ $cd ] );
40};
41
42is( scalar @{$rs->get_cache}, 0, 'set_cache() only accepts objects of correct type for the resultset' );
43
44unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
45DBI->trace(1, 't/var/dbic.trace');
46
47$rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
48while( $artist = $rs->next ) {}
49$artist = $rs->first();
50
51# count the SELECTs
52DBI->trace(0, undef);
53my $selects = 0;
54$trace = IO::File->new('t/var/dbic.trace', '<')
55 or die "Unable to read trace file";
56while (<$trace>) {
57 $selects++ if /SELECT/;
58}
59$trace->close;
60unlink 't/var/dbic.trace';
61
62is( $selects, 1, 'revisiting a row does not issue a query when cache => 1' );
63
f109ee4a 64my @a = $schema->resultset("Artist")->search(
65 { },
66 {
67 join => [ qw/ cds /],
68 prefetch => [qw/ cds /],
69 }
70);
71
72is(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 82use Data::Dumper; $Data::Dumper::Deparse = 1;
64acc2bc 83
84# start test for prefetch SELECT count
85unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
86DBI->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
92is( 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
95is( 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
98is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
99
100# ensure that $artist->cds->count returns correct value
101is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
102
103# ensure that $artist->count_related('cds') returns correct value
104is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
105
106# count the SELECTs
107DBI->trace(0, undef);
534ca143 108$selects = 0;
64acc2bc 109my $trace = IO::File->new('t/var/dbic.trace', '<')
110 or die "Unable to read trace file";
111while (<$trace>) {
112 $selects++ if /SELECT/;
113}
114$trace->close;
115unlink 't/var/dbic.trace';
5a5bec6c 116is($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
122is( 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 135my $artist_count_before = $schema->resultset('Artist')->count;
62e87ea8 136$schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
717f3498 137is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
62e87ea8 138my $artist = $schema->resultset("Artist")->search(
139 { artistid => 4 },{prefetch=>[qw/cds/]}
140)->first;
141
142is($artist->cds, 0, 'No cds for this artist');
143}
64acc2bc 144
f9cc31dd 145# SELECT count for nested has_many prefetch
146unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
147DBI->trace(1, 't/var/dbic.trace');
148
5a5bec6c 149$artist = ($rs->all)[0];
f9cc31dd 150
151# count the SELECTs
152DBI->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";
156while (<$trace>) {
157 $selects++ if /SELECT/;
158}
159$trace->close;
160unlink 't/var/dbic.trace';
5a5bec6c 161is($selects, 1, 'only one SQL statement executed');
f9cc31dd 162
163my @objs;
5a5bec6c 164#$artist = $rs->find(1);
f9cc31dd 165
166unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
167DBI->trace(1, 't/var/dbic.trace');
168
169my $cds = $artist->cds;
170my $tags = $cds->next->tags;
171while( my $tag = $tags->next ) {
5a5bec6c 172 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
f9cc31dd 173}
174
5a5bec6c 175is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
f9cc31dd 176
177$tags = $cds->next->tags;
178@objs = ();
179while( my $tag = $tags->next ) {
180 push @objs, $tag->id; #warn "tag: ", $tag->ID;
181}
182
183is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
184
185# count the SELECTs
186DBI->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";
190while (<$trace>) {
191 $selects++ if /SELECT/;
192}
193$trace->close;
194unlink 't/var/dbic.trace';
195
196is( $selects, 0, 'no additional SQL statements while checking nested data' );
197
d2b3ea14 198# start test for prefetch SELECT count
199unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
200DBI->trace(1, 't/var/dbic.trace');
201
202$artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
203
204# count the SELECTs
205DBI->trace(0, undef);
206$selects = 0;
207$trace = IO::File->new('t/var/dbic.trace', '<')
208 or die "Unable to read trace file";
209while (<$trace>) {
210 $selects++ if /SELECT/;
211}
212$trace->close;
213unlink 't/var/dbic.trace';
214
215is( $selects, 1, 'only one select statement on find with inline has_many prefetch' );
216
217# start test for prefetch SELECT count
218unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
219DBI->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
225DBI->trace(0, undef);
226$selects = 0;
227$trace = IO::File->new('t/var/dbic.trace', '<')
228 or die "Unable to read trace file";
229while (<$trace>) {
230 $selects++ if /SELECT/;
231}
232$trace->close;
233unlink 't/var/dbic.trace';
234
235is( $selects, 1, 'only one select statement on find with has_many prefetch on resultset' );
236
64acc2bc 237}
238
2391;