Merge 'DBIx-Class-current' into 'trunk'
[dbsrgits/DBIx-Class.git] / t / run / 23cache.tl
1 sub run_tests {
2 my $schema = shift;
3
4 eval "use DBD::SQLite";
5 plan skip_all => 'needs DBD::SQLite for testing' if $@;
6 plan tests => 12;
7
8 my $rs = $schema->resultset("Artist")->search(
9   { artistid => 1 }
10 );
11
12 my $artist = $rs->first;
13
14 is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' );
15
16 $rs = $schema->resultset("Artist")->search(
17   { 'artistid' => 1 },
18   {
19     join => [ qw/ cds /],
20     prefetch => [qw/ cds /],
21     cache => 1,
22   }
23 );
24
25 use Data::Dumper; $Data::Dumper::Deparse = 1;
26
27 # start test for prefetch SELECT count
28 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
29 DBI->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
35 is( 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
38 is( 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
41 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
42
43 # ensure that $artist->cds->count returns correct value
44 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
45
46 # ensure that $artist->count_related('cds') returns correct value
47 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
48
49 # count the SELECTs
50 DBI->trace(0, undef);
51 my $selects = 0;
52 my $trace = IO::File->new('t/var/dbic.trace', '<') 
53     or die "Unable to read trace file";
54 while (<$trace>) {
55     $selects++ if /SELECT/;
56 }
57 $trace->close;
58 unlink 't/var/dbic.trace';
59 is($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
65 is( 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   {
71     join => { cds => 'tags' },
72     prefetch => {
73       cds => 'tags'
74     },
75     cache => 1
76   }
77 );
78
79 # SELECT count for nested has_many prefetch
80 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
81 DBI->trace(1, 't/var/dbic.trace');
82
83 $artist = $rs->first;
84
85 # count the SELECTs
86 DBI->trace(0, undef);
87 $selects = 0;
88 $trace = IO::File->new('t/var/dbic.trace', '<') 
89     or die "Unable to read trace file";
90 while (<$trace>) {
91     $selects++ if /SELECT/;
92 }
93 $trace->close;
94 unlink 't/var/dbic.trace';
95 is($selects, 3, 'one SQL statement for each cached table with nested prefetch');
96
97 my @objs;
98 $artist = $rs->find(1);
99
100 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
101 DBI->trace(1, 't/var/dbic.trace');
102
103 my $cds = $artist->cds;
104 my $tags = $cds->next->tags;
105 while( my $tag = $tags->next ) {
106   push @objs, $tag->tagid; #warn "tag:", $tag->ID;
107 }
108
109 is_deeply( \@objs, [ 1 ], 'first cd has correct tags' );
110
111 $tags = $cds->next->tags;
112 @objs = ();
113 while( my $tag = $tags->next ) {
114   push @objs, $tag->id; #warn "tag: ", $tag->ID;
115 }
116
117 is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
118
119 # count the SELECTs
120 DBI->trace(0, undef);
121 $selects = 0;
122 $trace = IO::File->new('t/var/dbic.trace', '<') 
123     or die "Unable to read trace file";
124 while (<$trace>) {
125     $selects++ if /SELECT/;
126 }
127 $trace->close;
128 unlink 't/var/dbic.trace';
129
130 is( $selects, 0, 'no additional SQL statements while checking nested data' );
131
132 }
133
134 1;