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