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