4 eval "use DBD::SQLite";
5 plan skip_all => 'needs DBD::SQLite for testing' if $@;
9 This test WILL fail. That's because the has_many prefetch code is
10 only half re-written. However, it was utterly borken before, so
11 this is arguably an improvement. If you fancy having a go at making
12 _construct_object in resultset collapse multiple results into
13 appropriate nested structures for inflate_result, be my guest.
18 my $rs = $schema->resultset("Artist")->search(
22 my $artist = $rs->first;
24 is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' );
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, 2, 'only one SQL statement for each cached table');
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 # SELECT count for nested has_many prefetch
88 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
89 DBI->trace(1, 't/var/dbic.trace');
96 $trace = IO::File->new('t/var/dbic.trace', '<')
97 or die "Unable to read trace file";
99 $selects++ if /SELECT/;
102 unlink 't/var/dbic.trace';
103 is($selects, 3, 'one SQL statement for each cached table with nested prefetch');
106 $artist = $rs->find(1);
108 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
109 DBI->trace(1, 't/var/dbic.trace');
111 my $cds = $artist->cds;
112 my $tags = $cds->next->tags;
113 while( my $tag = $tags->next ) {
114 push @objs, $tag->tagid; #warn "tag:", $tag->ID;
117 is_deeply( \@objs, [ 1 ], 'first cd has correct tags' );
119 $tags = $cds->next->tags;
121 while( my $tag = $tags->next ) {
122 push @objs, $tag->id; #warn "tag: ", $tag->ID;
125 is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
128 DBI->trace(0, undef);
130 $trace = IO::File->new('t/var/dbic.trace', '<')
131 or die "Unable to read trace file";
133 $selects++ if /SELECT/;
136 unlink 't/var/dbic.trace';
138 is( $selects, 0, 'no additional SQL statements while checking nested data' );