update MANIFEST
[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 $@;
f9cc31dd 6plan tests => 12;
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 /],
21 cache => 1,
22 }
23);
24
f9cc31dd 25use Data::Dumper; $Data::Dumper::Deparse = 1;
64acc2bc 26
27# start test for prefetch SELECT count
28unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
29DBI->trace(1, 't/var/dbic.trace');
30
31$artist = $rs->first;
32$rs->reset();
33
34# make sure artist contains a related resultset for cds
35is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
36
37# check if $artist->cds->get_cache is populated
38is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
39
40# ensure that $artist->cds returns correct number of objects
41is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
42
43# ensure that $artist->cds->count returns correct value
44is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
45
46# ensure that $artist->count_related('cds') returns correct value
47is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
48
49# count the SELECTs
50DBI->trace(0, undef);
51my $selects = 0;
52my $trace = IO::File->new('t/var/dbic.trace', '<')
53 or die "Unable to read trace file";
54while (<$trace>) {
55 $selects++ if /SELECT/;
56}
57$trace->close;
58unlink 't/var/dbic.trace';
59is($selects, 2, 'only one SQL statement for each cached table');
60
61# make sure related_resultset is deleted after object is updated
62$artist->set_column('name', 'New Name');
63$artist->update();
64
65is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
66
67# todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
68$rs = $schema->resultset("Artist")->search(
69 { artistid => 1 },
70 {
3c3c416e 71 join => { cds => 'tags' },
64acc2bc 72 prefetch => {
73 cds => 'tags'
74 },
75 cache => 1
76 }
77);
78
f9cc31dd 79# SELECT count for nested has_many prefetch
80unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
81DBI->trace(1, 't/var/dbic.trace');
82
83$artist = $rs->first;
84
85# count the SELECTs
86DBI->trace(0, undef);
6e3b590d 87$selects = 0;
88$trace = IO::File->new('t/var/dbic.trace', '<')
f9cc31dd 89 or die "Unable to read trace file";
90while (<$trace>) {
91 $selects++ if /SELECT/;
92}
93$trace->close;
94unlink 't/var/dbic.trace';
95is($selects, 3, 'one SQL statement for each cached table with nested prefetch');
96
97my @objs;
6e3b590d 98$artist = $rs->find(1);
f9cc31dd 99
100unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
101DBI->trace(1, 't/var/dbic.trace');
102
103my $cds = $artist->cds;
104my $tags = $cds->next->tags;
105while( my $tag = $tags->next ) {
106 push @objs, $tag->tagid; #warn "tag:", $tag->ID;
107}
108
109is_deeply( \@objs, [ 1 ], 'first cd has correct tags' );
110
111$tags = $cds->next->tags;
112@objs = ();
113while( my $tag = $tags->next ) {
114 push @objs, $tag->id; #warn "tag: ", $tag->ID;
115}
116
117is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
118
119# count the SELECTs
120DBI->trace(0, undef);
6e3b590d 121$selects = 0;
122$trace = IO::File->new('t/var/dbic.trace', '<')
f9cc31dd 123 or die "Unable to read trace file";
124while (<$trace>) {
125 $selects++ if /SELECT/;
126}
127$trace->close;
128unlink 't/var/dbic.trace';
129
130is( $selects, 0, 'no additional SQL statements while checking nested data' );
131
64acc2bc 132}
133
1341;