Call on_connect AFTER actual connection, so we have the correct sqlt_type
[dbsrgits/DBIx-Class.git] / t / run / 23cache.tl
1 $|=1;
2 sub run_tests {
3 my $schema = shift;
4
5 my $queries;
6 $schema->storage->debugcb( sub{ $queries++ } );
7
8 eval "use DBD::SQLite";
9 plan skip_all => 'needs DBD::SQLite for testing' if $@;
10 plan tests => 25;
11
12 my $rs = $schema->resultset("Artist")->search(
13   { artistid => 1 }
14 );
15
16 my $artist = $rs->first;
17
18 is( scalar @{ $rs->get_cache('all') }, 0, 'cache is not populated without cache attribute' );
19
20 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
21 my $artists = [ $rs->all ];
22
23 is( scalar @{$rs->get_cache('all')}, 3, 'all() populates cache for search with cache attribute' );
24
25 $rs->clear_cache;
26
27 is( scalar @{$rs->get_cache('all')}, 0, 'clear_cache is functional' );
28
29 $rs->next;
30
31 is( scalar @{$rs->get_cache('all')}, 3, 'next() populates cache for search with cache attribute' );
32
33 pop( @$artists );
34 $rs->set_cache('all', $artists );
35
36 is( scalar @{$rs->get_cache('all')}, 2, 'set_cache() is functional' );
37
38 $cd = $schema->resultset('CD')->find(1);
39
40 $rs->clear_cache;
41
42 eval {
43   $rs->set_cache('all', [ $cd ] );
44 };
45
46 is( scalar @{$rs->get_cache('all')}, 0, 'set_cache() only accepts objects of correct type for the resultset' );
47
48 $queries = 0;
49 $schema->storage->debug(1);
50
51 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
52 while( $artist = $rs->next ) {}
53 $artist = $rs->first();
54
55 is( $queries, 1, 'revisiting a row does not issue a query when cache => 1' );
56
57 $schema->storage->debug(0);
58
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
69 $rs = $schema->resultset("Artist")->search(
70   { 'artistid' => 1 },
71   {
72     join => [ qw/ cds /],
73     prefetch => [qw/ cds /],
74   }
75 );
76
77 use Data::Dumper; $Data::Dumper::Deparse = 1;
78
79 # start test for prefetch SELECT count
80 $queries = 0;
81 $schema->storage->debug(1);
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
90 is( scalar @{$artist->cds->get_cache('all')}, 3, 'cache for artist->cds contains correct number of records');
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
101 is($queries, 1, 'only one SQL statement executed');
102
103 $schema->storage->debug(0);
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   {
115     join => { cds => 'tags' },
116     prefetch => {
117       cds => 'tags'
118     },
119   }
120 );
121 {
122 my $artist_count_before = $schema->resultset('Artist')->count;
123 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
124 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
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 }
131
132 # SELECT count for nested has_many prefetch
133 $queries = 0;
134 $schema->storage->debug(1);
135
136 $artist = ($rs->all)[0];
137
138 is($queries, 1, 'only one SQL statement executed');
139
140 $schema->storage->debug(0);
141
142 my @objs;
143 #$artist = $rs->find(1);
144
145 $queries = 0;
146 $schema->storage->debug(1);
147
148 my $cds = $artist->cds;
149 my $tags = $cds->next->tags;
150 while( my $tag = $tags->next ) {
151   push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
152 }
153
154 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
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
164 is( $queries, 0, 'no additional SQL statements while checking nested data' );
165
166 # start test for prefetch SELECT count
167 $queries = 0;
168
169 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
170
171 is( $queries, 1, 'only one select statement on find with inline has_many prefetch' );
172
173 # start test for prefetch SELECT count
174 $queries = 0;
175
176 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
177 $artist = $rs->find(1);
178
179 is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' );
180
181 $schema->storage->debug(0);
182
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/;
205 }
206 $trace->close;
207 # unlink 't/var/dbic.trace';
208
209 is( $selects, 1, 'only one select statement on find' );
210
211 }
212 1;