Fix default insert on Oracle ( $rs->create({}) with empty hashref )
[dbsrgits/DBIx-Class.git] / t / 83cache.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 my $rs = $schema->resultset("Artist")->search(
11   { artistid => 1 }
12 );
13
14 my $artist = $rs->first;
15
16 ok( !defined($rs->get_cache), 'cache is not populated without cache attribute' );
17
18 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
19 my $artists = [ $rs->all ];
20
21 is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
22
23 $rs->clear_cache;
24
25 ok( !defined($rs->get_cache), 'clear_cache is functional' );
26
27 $rs->next;
28
29 is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
30
31 pop( @$artists );
32 $rs->set_cache( $artists );
33
34 is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
35
36 my $cd = $schema->resultset('CD')->find(1);
37
38 $rs->clear_cache;
39
40 $schema->is_executed_querycount( sub {
41
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' );
46
47 my @a = $schema->resultset("Artist")->search(
48   { },
49   {
50     join => [ qw/ cds /],
51     prefetch => [qw/ cds /],
52   }
53 );
54
55 is(scalar @a, 3, 'artist with cds: count parent objects');
56
57 $rs = $schema->resultset("Artist")->search(
58   { 'artistid' => 1 },
59   {
60     join => [ qw/ cds /],
61     prefetch => [qw/ cds /],
62   }
63 );
64
65 # prefetch SELECT count
66 $schema->is_executed_querycount( sub {
67   $artist = $rs->first;
68   $rs->reset();
69
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' );
72
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');
75
76   # ensure that $artist->cds returns correct number of objects
77   is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
78
79   # ensure that $artist->cds->count returns correct value
80   is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
81
82   # ensure that $artist->count_related('cds') returns correct value
83   is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
84
85 }, 1, 'only one SQL statement executed');
86
87
88 # make sure related_resultset is deleted after object is updated
89 $artist->set_column('name', 'New Name');
90 $artist->update();
91
92 is( 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   {
98     join => { cds => 'tags' },
99     prefetch => {
100       cds => 'tags'
101     },
102     order_by => { -desc => 'cds.cdid' },
103   }
104 );
105 {
106 my $artist_count_before = $schema->resultset('Artist')->count;
107 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
108 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
109 my $artist = $schema->resultset("Artist")->search(
110   { artistid => 4 },{prefetch=>[qw/cds/]}
111 )->first;
112
113 is($artist->cds, 0, 'No cds for this artist');
114 }
115
116 # SELECT count for nested has_many prefetch
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   }
128
129   is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
130
131   $tags = $cds->next->tags;
132   @objs = ();
133   while( my $tag = $tags->next ) {
134     push @objs, $tag->id; #warn "tag: ", $tag->ID;
135   }
136
137   is_deeply( [ sort @objs] , [ 2, 5, 8 ], 'third cd has correct tags' );
138
139   $tags = $cds->next->tags;
140   @objs = ();
141   while( my $tag = $tags->next ) {
142     push @objs, $tag->id; #warn "tag: ", $tag->ID;
143   }
144
145   is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
146 }, 0, 'no additional SQL statements while checking nested data' );
147
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' );
151
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' );
156
157 done_testing;