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 /],
24 use Data::Dumper; $Data::Dumper::Deparse = 1;
26 # start test for prefetch SELECT count
27 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
28 DBI->trace(1, 't/var/dbic.trace');
33 # make sure artist contains a related resultset for cds
34 is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
36 # check if $artist->cds->get_cache is populated
37 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
39 # ensure that $artist->cds returns correct number of objects
40 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
42 # ensure that $artist->cds->count returns correct value
43 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
45 # ensure that $artist->count_related('cds') returns correct value
46 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
51 my $trace = IO::File->new('t/var/dbic.trace', '<')
52 or die "Unable to read trace file";
54 $selects++ if /SELECT/;
57 unlink 't/var/dbic.trace';
58 is($selects, 1, 'only one SQL statement executed');
60 # make sure related_resultset is deleted after object is updated
61 $artist->set_column('name', 'New Name');
64 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
66 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
67 $rs = $schema->resultset("Artist")->search(
70 join => { cds => 'tags' },
77 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
78 my $artist = $schema->resultset("Artist")->search(
79 { artistid => 4 },{prefetch=>[qw/cds/]}
82 is($artist->cds, 0, 'No cds for this artist');
85 # SELECT count for nested has_many prefetch
86 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
87 DBI->trace(1, 't/var/dbic.trace');
89 $artist = ($rs->all)[0];
94 $trace = IO::File->new('t/var/dbic.trace', '<')
95 or die "Unable to read trace file";
97 $selects++ if /SELECT/;
100 unlink 't/var/dbic.trace';
101 is($selects, 1, 'only one SQL statement executed');
104 #$artist = $rs->find(1);
106 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
107 DBI->trace(1, 't/var/dbic.trace');
109 my $cds = $artist->cds;
110 my $tags = $cds->next->tags;
111 while( my $tag = $tags->next ) {
112 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
115 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
117 $tags = $cds->next->tags;
119 while( my $tag = $tags->next ) {
120 push @objs, $tag->id; #warn "tag: ", $tag->ID;
123 is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
126 DBI->trace(0, undef);
128 $trace = IO::File->new('t/var/dbic.trace', '<')
129 or die "Unable to read trace file";
131 $selects++ if /SELECT/;
134 unlink 't/var/dbic.trace';
136 is( $selects, 0, 'no additional SQL statements while checking nested data' );
138 # start test for prefetch SELECT count
139 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
140 DBI->trace(1, 't/var/dbic.trace');
142 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
145 DBI->trace(0, undef);
147 $trace = IO::File->new('t/var/dbic.trace', '<')
148 or die "Unable to read trace file";
150 $selects++ if /SELECT/;
153 unlink 't/var/dbic.trace';
155 is( $selects, 1, 'only one select statement on find with inline has_many prefetch' );
157 # start test for prefetch SELECT count
158 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
159 DBI->trace(1, 't/var/dbic.trace');
161 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
162 $artist = $rs->find(1);
165 DBI->trace(0, undef);
167 $trace = IO::File->new('t/var/dbic.trace', '<')
168 or die "Unable to read trace file";
170 $selects++ if /SELECT/;
173 unlink 't/var/dbic.trace';
175 is( $selects, 1, 'only one select statement on find with has_many prefetch on resultset' );