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 # 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');
20 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
25 $trace = IO::File->new('t/var/dbic.trace', '<')
26 or die "Unable to read trace file";
28 $selects++ if /SELECT/;
31 unlink 't/var/dbic.trace';
33 is( $selects, 1, 'only one select statement on find with has_many prefetch' );
36 $rs = $schema->resultset("Artist")->search(
40 prefetch => [qw/ cds /],
44 use Data::Dumper; $Data::Dumper::Deparse = 1;
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');
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' );
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');
59 # ensure that $artist->cds returns correct number of objects
60 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
62 # ensure that $artist->cds->count returns correct value
63 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
65 # ensure that $artist->count_related('cds') returns correct value
66 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
71 my $trace = IO::File->new('t/var/dbic.trace', '<')
72 or die "Unable to read trace file";
74 $selects++ if /SELECT/;
77 unlink 't/var/dbic.trace';
78 is($selects, 1, 'only one SQL statement executed');
80 # make sure related_resultset is deleted after object is updated
81 $artist->set_column('name', 'New Name');
84 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
86 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
87 $rs = $schema->resultset("Artist")->search(
90 join => { cds => 'tags' },
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');
101 $artist = ($rs->all)[0];
104 DBI->trace(0, undef);
106 $trace = IO::File->new('t/var/dbic.trace', '<')
107 or die "Unable to read trace file";
109 $selects++ if /SELECT/;
112 unlink 't/var/dbic.trace';
113 is($selects, 1, 'only one SQL statement executed');
116 #$artist = $rs->find(1);
118 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
119 DBI->trace(1, 't/var/dbic.trace');
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;
127 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
129 $tags = $cds->next->tags;
131 while( my $tag = $tags->next ) {
132 push @objs, $tag->id; #warn "tag: ", $tag->ID;
135 is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
138 DBI->trace(0, undef);
140 $trace = IO::File->new('t/var/dbic.trace', '<')
141 or die "Unable to read trace file";
143 $selects++ if /SELECT/;
146 unlink 't/var/dbic.trace';
148 is( $selects, 0, 'no additional SQL statements while checking nested data' );