Populate caches for related result sets even if they're empty
[dbsrgits/DBIx-Class.git] / t / prefetch / empty_cache.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my $schema = DBICTest->init_schema();
10
11 my $no_albums_artist = { name => 'We Have No Albums' };
12 $schema->resultset('Artist')->create($no_albums_artist);
13
14 foreach (
15   [empty => \'0 = 1', 0],
16   [nonempty => $no_albums_artist, 1],
17 ) {
18   my ($desc, $cond, $count) = @$_;
19
20   my $artists_rs = $schema->resultset('Artist')
21     ->search($cond, { prefetch => 'cds', cache => 1 });
22
23   $schema->is_executed_querycount( sub {
24     my @artists = $artists_rs->all;
25     is( 0+@{$artists_rs->get_cache}, $count, "$desc cache on original resultset" );
26     is( 0+@artists, $count, "$desc original resultset" );
27   }, 1, "->all on $desc original resultset hit db" );
28
29   $schema->is_executed_querycount( sub {
30     my $cds_rs = $artists_rs->related_resultset('cds');
31     is_deeply( $cds_rs->get_cache, [], 'empty cache on related resultset' );
32
33     my @cds = $cds_rs->all;
34     is( 0+@cds, 0, 'empty related resultset' );
35   }, 0, '->all on empty related resultest didn\'t hit db' );
36 }
37
38
39 done_testing;