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