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 my @a = $schema->resultset("Artist")->search(
20 prefetch => [qw/ cds /],
24 is(scalar @a, 3, 'artist with cds: count parent objects');
26 $rs = $schema->resultset("Artist")->search(
30 prefetch => [qw/ cds /],
34 use Data::Dumper; $Data::Dumper::Deparse = 1;
36 # start test for prefetch SELECT count
37 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
38 DBI->trace(1, 't/var/dbic.trace');
43 # make sure artist contains a related resultset for cds
44 is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
46 # check if $artist->cds->get_cache is populated
47 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
49 # ensure that $artist->cds returns correct number of objects
50 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
52 # ensure that $artist->cds->count returns correct value
53 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
55 # ensure that $artist->count_related('cds') returns correct value
56 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
61 my $trace = IO::File->new('t/var/dbic.trace', '<')
62 or die "Unable to read trace file";
64 $selects++ if /SELECT/;
67 unlink 't/var/dbic.trace';
68 is($selects, 1, 'only one SQL statement executed');
70 # make sure related_resultset is deleted after object is updated
71 $artist->set_column('name', 'New Name');
74 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
76 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
77 $rs = $schema->resultset("Artist")->search(
80 join => { cds => 'tags' },
87 my $artist_count_before = $schema->resultset('Artist')->count;
88 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
89 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
90 my $artist = $schema->resultset("Artist")->search(
91 { artistid => 4 },{prefetch=>[qw/cds/]}
94 is($artist->cds, 0, 'No cds for this artist');
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' );
150 # start test for prefetch SELECT count
151 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
152 DBI->trace(1, 't/var/dbic.trace');
154 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
157 DBI->trace(0, undef);
159 $trace = IO::File->new('t/var/dbic.trace', '<')
160 or die "Unable to read trace file";
162 $selects++ if /SELECT/;
165 unlink 't/var/dbic.trace';
167 is( $selects, 1, 'only one select statement on find with inline has_many prefetch' );
169 # start test for prefetch SELECT count
170 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
171 DBI->trace(1, 't/var/dbic.trace');
173 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
174 $artist = $rs->find(1);
177 DBI->trace(0, undef);
179 $trace = IO::File->new('t/var/dbic.trace', '<')
180 or die "Unable to read trace file";
182 $selects++ if /SELECT/;
185 unlink 't/var/dbic.trace';
187 is( $selects, 1, 'only one select statement on find with has_many prefetch on resultset' );