4 eval "use DBD::SQLite";
5 plan skip_all => 'needs DBD::SQLite for testing' if $@;
8 my $rs = $schema->resultset("Artist")->search(
12 my $artist = $rs->first;
14 is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' );
16 $rs = $schema->resultset("Artist")->search(
20 prefetch => [qw/ cds /],
25 use Data::Dumper; $Data::Dumper::Deparse = 1;
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');
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' );
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');
40 # ensure that $artist->cds returns correct number of objects
41 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
43 # ensure that $artist->cds->count returns correct value
44 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
46 # ensure that $artist->count_related('cds') returns correct value
47 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
52 my $trace = IO::File->new('t/var/dbic.trace', '<')
53 or die "Unable to read trace file";
55 $selects++ if /SELECT/;
58 unlink 't/var/dbic.trace';
59 is($selects, 2, 'only one SQL statement for each cached table');
61 # make sure related_resultset is deleted after object is updated
62 $artist->set_column('name', 'New Name');
65 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
67 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
68 $rs = $schema->resultset("Artist")->search(
71 join => { cds => 'tags' },
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');
88 $trace = IO::File->new('t/var/dbic.trace', '<')
89 or die "Unable to read trace file";
91 $selects++ if /SELECT/;
94 unlink 't/var/dbic.trace';
95 is($selects, 3, 'one SQL statement for each cached table with nested prefetch');
98 $artist = $rs->find(1);
100 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
101 DBI->trace(1, 't/var/dbic.trace');
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;
109 is_deeply( \@objs, [ 1 ], 'first cd has correct tags' );
111 $tags = $cds->next->tags;
113 while( my $tag = $tags->next ) {
114 push @objs, $tag->id; #warn "tag: ", $tag->ID;
117 is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
120 DBI->trace(0, undef);
122 $trace = IO::File->new('t/var/dbic.trace', '<')
123 or die "Unable to read trace file";
125 $selects++ if /SELECT/;
128 unlink 't/var/dbic.trace';
130 is( $selects, 0, 'no additional SQL statements while checking nested data' );