Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / 83cache.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
11
12 my $rs = $schema->resultset("Artist")->search(
13   { artistid => 1 }
14 );
15
16 my $artist = $rs->first;
17
18 ok( !defined($rs->get_cache), 'cache is not populated without cache attribute' );
19
20 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
21 my $artists = [ $rs->all ];
22
23 is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
24
25 $rs->clear_cache;
26
27 ok( !defined($rs->get_cache), 'clear_cache is functional' );
28
29 $rs->next;
30
31 is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
32
33 pop( @$artists );
34 $rs->set_cache( $artists );
35
36 is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
37
38 my $cd = $schema->resultset('CD')->find(1);
39
40 $rs->clear_cache;
41
42 $schema->is_executed_querycount( sub {
43
44   $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
45   while( $artist = $rs->next ) {}
46   $artist = $rs->first();
47 }, 1, 'revisiting a row does not issue a query when cache => 1' );
48
49 my @a = $schema->resultset("Artist")->search(
50   { },
51   {
52     join => [ qw/ cds /],
53     prefetch => [qw/ cds /],
54   }
55 );
56
57 is(scalar @a, 3, 'artist with cds: count parent objects');
58
59 $rs = $schema->resultset("Artist")->search(
60   { 'artistid' => 1 },
61   {
62     join => [ qw/ cds /],
63     prefetch => [qw/ cds /],
64   }
65 );
66
67 # prefetch SELECT count
68 $schema->is_executed_querycount( sub {
69   $artist = $rs->first;
70   $rs->reset();
71
72   # make sure artist contains a related resultset for cds
73   isa_ok( $artist->{related_resultsets}{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
74
75   # check if $artist->cds->get_cache is populated
76   is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
77
78   # ensure that $artist->cds returns correct number of objects
79   is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
80
81   # ensure that $artist->cds->count returns correct value
82   is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
83
84   # ensure that $artist->count_related('cds') returns correct value
85   is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
86
87 }, 1, 'only one SQL statement executed');
88
89
90 # make sure related_resultset is deleted after object is updated
91 $artist->set_column('name', 'New Name');
92 $artist->update();
93
94 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
95
96 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
97 $rs = $schema->resultset("Artist")->search(
98   { artistid => 1 },
99   {
100     join => { cds => 'tags' },
101     prefetch => {
102       cds => 'tags'
103     },
104     order_by => { -desc => 'cds.cdid' },
105   }
106 );
107 {
108 my $artist_count_before = $schema->resultset('Artist')->count;
109 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
110 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
111 my $artist = $schema->resultset("Artist")->search(
112   { artistid => 4 },{prefetch=>[qw/cds/]}
113 )->first;
114
115 is($artist->cds, 0, 'No cds for this artist');
116 }
117
118 # SELECT count for nested has_many prefetch
119 $schema->is_executed_querycount( sub {
120   $artist = ($rs->all)[0];
121 }, 1, 'only one SQL statement executed');
122
123 $schema->is_executed_querycount( sub {
124   my @objs;
125   my $cds = $artist->cds;
126   my $tags = $cds->next->tags;
127   while( my $tag = $tags->next ) {
128     push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
129   }
130
131   is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
132
133   $tags = $cds->next->tags;
134   @objs = ();
135   while( my $tag = $tags->next ) {
136     push @objs, $tag->id; #warn "tag: ", $tag->ID;
137   }
138
139   is_deeply( [ sort @objs] , [ 2, 5, 8 ], 'third cd has correct tags' );
140
141   $tags = $cds->next->tags;
142   @objs = ();
143   while( my $tag = $tags->next ) {
144     push @objs, $tag->id; #warn "tag: ", $tag->ID;
145   }
146
147   is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
148 }, 0, 'no additional SQL statements while checking nested data' );
149
150 $schema->is_executed_querycount( sub {
151   $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
152 }, 1, 'only one select statement on find with inline has_many prefetch' );
153
154 $schema->is_executed_querycount( sub {
155   $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
156   $artist = $rs->find(1);
157 }, 1, 'only one select statement on find with has_many prefetch on resultset' );
158
159 done_testing;