fix for hm prefetch and test.
[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 $@;
62e87ea8 6plan tests => 15;
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
16$rs = $schema->resultset("Artist")->search(
17 { 'artistid' => 1 },
18 {
3c3c416e 19 join => [ qw/ cds /],
64acc2bc 20 prefetch => [qw/ cds /],
64acc2bc 21 }
22);
23
f9cc31dd 24use Data::Dumper; $Data::Dumper::Deparse = 1;
64acc2bc 25
26# start test for prefetch SELECT count
27unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
28DBI->trace(1, 't/var/dbic.trace');
29
30$artist = $rs->first;
31$rs->reset();
32
33# make sure artist contains a related resultset for cds
34is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
35
36# check if $artist->cds->get_cache is populated
37is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
38
39# ensure that $artist->cds returns correct number of objects
40is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
41
42# ensure that $artist->cds->count returns correct value
43is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
44
45# ensure that $artist->count_related('cds') returns correct value
46is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
47
48# count the SELECTs
49DBI->trace(0, undef);
50my $selects = 0;
51my $trace = IO::File->new('t/var/dbic.trace', '<')
52 or die "Unable to read trace file";
53while (<$trace>) {
54 $selects++ if /SELECT/;
55}
56$trace->close;
57unlink 't/var/dbic.trace';
5a5bec6c 58is($selects, 1, 'only one SQL statement executed');
64acc2bc 59
60# make sure related_resultset is deleted after object is updated
61$artist->set_column('name', 'New Name');
62$artist->update();
63
64is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
65
66# todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
67$rs = $schema->resultset("Artist")->search(
68 { artistid => 1 },
69 {
3c3c416e 70 join => { cds => 'tags' },
64acc2bc 71 prefetch => {
72 cds => 'tags'
73 },
64acc2bc 74 }
75);
62e87ea8 76{
77$schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
78my $artist = $schema->resultset("Artist")->search(
79 { artistid => 4 },{prefetch=>[qw/cds/]}
80)->first;
81
82is($artist->cds, 0, 'No cds for this artist');
83}
64acc2bc 84
f9cc31dd 85# SELECT count for nested has_many prefetch
86unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
87DBI->trace(1, 't/var/dbic.trace');
88
5a5bec6c 89$artist = ($rs->all)[0];
f9cc31dd 90
91# count the SELECTs
92DBI->trace(0, undef);
6e3b590d 93$selects = 0;
94$trace = IO::File->new('t/var/dbic.trace', '<')
f9cc31dd 95 or die "Unable to read trace file";
96while (<$trace>) {
97 $selects++ if /SELECT/;
98}
99$trace->close;
100unlink 't/var/dbic.trace';
5a5bec6c 101is($selects, 1, 'only one SQL statement executed');
f9cc31dd 102
103my @objs;
5a5bec6c 104#$artist = $rs->find(1);
f9cc31dd 105
106unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
107DBI->trace(1, 't/var/dbic.trace');
108
109my $cds = $artist->cds;
110my $tags = $cds->next->tags;
111while( my $tag = $tags->next ) {
5a5bec6c 112 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
f9cc31dd 113}
114
5a5bec6c 115is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
f9cc31dd 116
117$tags = $cds->next->tags;
118@objs = ();
119while( my $tag = $tags->next ) {
120 push @objs, $tag->id; #warn "tag: ", $tag->ID;
121}
122
123is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
124
125# count the SELECTs
126DBI->trace(0, undef);
6e3b590d 127$selects = 0;
128$trace = IO::File->new('t/var/dbic.trace', '<')
f9cc31dd 129 or die "Unable to read trace file";
130while (<$trace>) {
131 $selects++ if /SELECT/;
132}
133$trace->close;
134unlink 't/var/dbic.trace';
135
136is( $selects, 0, 'no additional SQL statements while checking nested data' );
137
d2b3ea14 138# start test for prefetch SELECT count
139unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
140DBI->trace(1, 't/var/dbic.trace');
141
142$artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
143
144# count the SELECTs
145DBI->trace(0, undef);
146$selects = 0;
147$trace = IO::File->new('t/var/dbic.trace', '<')
148 or die "Unable to read trace file";
149while (<$trace>) {
150 $selects++ if /SELECT/;
151}
152$trace->close;
153unlink 't/var/dbic.trace';
154
155is( $selects, 1, 'only one select statement on find with inline has_many prefetch' );
156
157# start test for prefetch SELECT count
158unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
159DBI->trace(1, 't/var/dbic.trace');
160
161$rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
162$artist = $rs->find(1);
163
164# count the SELECTs
165DBI->trace(0, undef);
166$selects = 0;
167$trace = IO::File->new('t/var/dbic.trace', '<')
168 or die "Unable to read trace file";
169while (<$trace>) {
170 $selects++ if /SELECT/;
171}
172$trace->close;
173unlink 't/var/dbic.trace';
174
175is( $selects, 1, 'only one select statement on find with has_many prefetch on resultset' );
176
64acc2bc 177}
178
1791;