croak on DBI->connect errors
[dbsrgits/DBIx-Class.git] / t / run / 22cache.tl
CommitLineData
64acc2bc 1sub run_tests {
2my $schema = shift;
3
4eval "use DBD::SQLite";
5plan skip_all => 'needs DBD::SQLite for testing' if $@;
6plan tests => 8;
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 {
19 prefetch => [qw/ cds /],
20 cache => 1,
21 }
22);
23
24# use Data::Dumper; $Data::Dumper::Deparse = 1;
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';
58is($selects, 2, 'only one SQL statement for each cached table');
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 {
70 prefetch => {
71 cds => 'tags'
72 },
73 cache => 1
74 }
75);
76
77}
78
791;