Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 188 lines (132 sloc) 4.991 kB
7035051 Delete basicrels tests. Modify run tests to use new syntax. Remove h…
Aran Deltac authored
1 use strict;
8273e84 @ribasushi Trailing WS crusade - got to save them bits
ribasushi authored
2 use warnings;
7035051 Delete basicrels tests. Modify run tests to use new syntax. Remove h…
Aran Deltac authored
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
a47e123 Changed tests to use DBICTest->init_schema() instead of DBICTest::ini…
Justin Guenther authored
8 my $schema = DBICTest->init_schema();
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
9
d52170d @wdh nuke DBI->trace calls
wdh authored
10 my $queries;
81549fa @ribasushi cleanup
ribasushi authored
11 my $debugcb = sub{ $queries++ };
a228776 @ribasushi Test cleanups
ribasushi authored
12 my $sdebug = $schema->storage->debug;
d52170d @wdh nuke DBI->trace calls
wdh authored
13
b25e9fa @shadowcat-mst collapse result refac hopefully complete
shadowcat-mst authored
14 plan tests => 23;
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
15
16 my $rs = $schema->resultset("Artist")->search(
17 { artistid => 1 }
18 );
19
20 my $artist = $rs->first;
21
0823196 @shadowcat-mst couple bugfixes
shadowcat-mst authored
22 ok( !defined($rs->get_cache), 'cache is not populated without cache attribute' );
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
23
534ca14 @wdh add basic cache tests/docs to trunk
wdh authored
24 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
25 my $artists = [ $rs->all ];
26
27 is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
28
29 $rs->clear_cache;
30
0823196 @shadowcat-mst couple bugfixes
shadowcat-mst authored
31 ok( !defined($rs->get_cache), 'clear_cache is functional' );
534ca14 @wdh add basic cache tests/docs to trunk
wdh authored
32
33 $rs->next;
34
35 is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
36
37 pop( @$artists );
38 $rs->set_cache( $artists );
39
40 is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
41
58d387f Fixes to tests so that they pass.
Aran Deltac authored
42 my $cd = $schema->resultset('CD')->find(1);
534ca14 @wdh add basic cache tests/docs to trunk
wdh authored
43
44 $rs->clear_cache;
45
d52170d @wdh nuke DBI->trace calls
wdh authored
46 $queries = 0;
47 $schema->storage->debug(1);
81549fa @ribasushi cleanup
ribasushi authored
48 $schema->storage->debugcb ($debugcb);
534ca14 @wdh add basic cache tests/docs to trunk
wdh authored
49
50 $rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
51 while( $artist = $rs->next ) {}
52 $artist = $rs->first();
53
d52170d @wdh nuke DBI->trace calls
wdh authored
54 is( $queries, 1, 'revisiting a row does not issue a query when cache => 1' );
534ca14 @wdh add basic cache tests/docs to trunk
wdh authored
55
a228776 @ribasushi Test cleanups
ribasushi authored
56 $schema->storage->debug($sdebug);
81549fa @ribasushi cleanup
ribasushi authored
57 $schema->storage->debugcb (undef);
534ca14 @wdh add basic cache tests/docs to trunk
wdh authored
58
f109ee4 @shadowcat-mst doc tweaks, failing test from jcs
shadowcat-mst authored
59 my @a = $schema->resultset("Artist")->search(
60 { },
61 {
62 join => [ qw/ cds /],
63 prefetch => [qw/ cds /],
64 }
65 );
66
67 is(scalar @a, 3, 'artist with cds: count parent objects');
68
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
69 $rs = $schema->resultset("Artist")->search(
70 { 'artistid' => 1 },
71 {
3c3c416 @shadowcat-mst Added missing join attrs to 23cache
shadowcat-mst authored
72 join => [ qw/ cds /],
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
73 prefetch => [qw/ cds /],
74 }
75 );
76
77 # start test for prefetch SELECT count
d52170d @wdh nuke DBI->trace calls
wdh authored
78 $queries = 0;
79 $schema->storage->debug(1);
81549fa @ribasushi cleanup
ribasushi authored
80 $schema->storage->debugcb ($debugcb);
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
81
82 $artist = $rs->first;
83 $rs->reset();
84
85 # make sure artist contains a related resultset for cds
660cf1b @ribasushi Add default resultclass/resultsetclass to the entire test schema, as …
ribasushi authored
86 isa_ok( $artist->{related_resultsets}{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' );
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
87
88 # check if $artist->cds->get_cache is populated
89 is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records');
90
91 # ensure that $artist->cds returns correct number of objects
92 is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
93
94 # ensure that $artist->cds->count returns correct value
95 is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
96
97 # ensure that $artist->count_related('cds') returns correct value
98 is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
99
d52170d @wdh nuke DBI->trace calls
wdh authored
100 is($queries, 1, 'only one SQL statement executed');
101
a228776 @ribasushi Test cleanups
ribasushi authored
102 $schema->storage->debug($sdebug);
81549fa @ribasushi cleanup
ribasushi authored
103 $schema->storage->debugcb (undef);
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
104
105 # make sure related_resultset is deleted after object is updated
106 $artist->set_column('name', 'New Name');
107 $artist->update();
108
109 is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' );
110
111 # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks
112 $rs = $schema->resultset("Artist")->search(
113 { artistid => 1 },
114 {
3c3c416 @shadowcat-mst Added missing join attrs to 23cache
shadowcat-mst authored
115 join => { cds => 'tags' },
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
116 prefetch => {
117 cds => 'tags'
118 },
fb88ca2 @ribasushi Fix tests failing due to unspecified resultset retrieval order
ribasushi authored
119 order_by => { -desc => 'cds.cdid' },
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
120 }
121 );
62e87ea fix for hm prefetch and test.
Marcus Ramberg authored
122 {
717f349 added a test for count() caching
Justin Guenther authored
123 my $artist_count_before = $schema->resultset('Artist')->count;
62e87ea fix for hm prefetch and test.
Marcus Ramberg authored
124 $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}});
717f349 added a test for count() caching
Justin Guenther authored
125 is($schema->resultset('Artist')->count, $artist_count_before + 1, 'count() reflects new artist');
62e87ea fix for hm prefetch and test.
Marcus Ramberg authored
126 my $artist = $schema->resultset("Artist")->search(
127 { artistid => 4 },{prefetch=>[qw/cds/]}
128 )->first;
129
130 is($artist->cds, 0, 'No cds for this artist');
131 }
64acc2b @wdh introduce row caching using related_resultset; has_many prefetch (sin…
wdh authored
132
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
133 # SELECT count for nested has_many prefetch
d52170d @wdh nuke DBI->trace calls
wdh authored
134 $queries = 0;
135 $schema->storage->debug(1);
81549fa @ribasushi cleanup
ribasushi authored
136 $schema->storage->debugcb ($debugcb);
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
137
5a5bec6 @shadowcat-mst has_many prefetch works. no, seriously
shadowcat-mst authored
138 $artist = ($rs->all)[0];
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
139
d52170d @wdh nuke DBI->trace calls
wdh authored
140 is($queries, 1, 'only one SQL statement executed');
141
142 $queries = 0;
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
143
fb88ca2 @ribasushi Fix tests failing due to unspecified resultset retrieval order
ribasushi authored
144 my @objs;
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
145 my $cds = $artist->cds;
146 my $tags = $cds->next->tags;
147 while( my $tag = $tags->next ) {
5a5bec6 @shadowcat-mst has_many prefetch works. no, seriously
shadowcat-mst authored
148 push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag;
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
149 }
150
5a5bec6 @shadowcat-mst has_many prefetch works. no, seriously
shadowcat-mst authored
151 is_deeply( \@objs, [ 3 ], 'first cd has correct tags' );
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
152
153 $tags = $cds->next->tags;
154 @objs = ();
155 while( my $tag = $tags->next ) {
156 push @objs, $tag->id; #warn "tag: ", $tag->ID;
157 }
158
fb88ca2 @ribasushi Fix tests failing due to unspecified resultset retrieval order
ribasushi authored
159 is_deeply( [ sort @objs] , [ 2, 5, 8 ], 'third cd has correct tags' );
b25e9fa @shadowcat-mst collapse result refac hopefully complete
shadowcat-mst authored
160
161 $tags = $cds->next->tags;
162 @objs = ();
163 while( my $tag = $tags->next ) {
164 push @objs, $tag->id; #warn "tag: ", $tag->ID;
165 }
166
908aa1b @ribasushi Initial full test pass - all fetches are eager for now
ribasushi authored
167 is_deeply( \@objs, [ 1 ], 'second cd has correct tags' );
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
168
d52170d @wdh nuke DBI->trace calls
wdh authored
169 is( $queries, 0, 'no additional SQL statements while checking nested data' );
f9cc31d @wdh nested has_many prefetch + tests
wdh authored
170
d2b3ea1 add second test to 23cache, that doesn't work
David Kamholz authored
171 # start test for prefetch SELECT count
d52170d @wdh nuke DBI->trace calls
wdh authored
172 $queries = 0;
d2b3ea1 add second test to 23cache, that doesn't work
David Kamholz authored
173
174 $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] });
175
d52170d @wdh nuke DBI->trace calls
wdh authored
176 is( $queries, 1, 'only one select statement on find with inline has_many prefetch' );
d2b3ea1 add second test to 23cache, that doesn't work
David Kamholz authored
177
178 # start test for prefetch SELECT count
d52170d @wdh nuke DBI->trace calls
wdh authored
179 $queries = 0;
d2b3ea1 add second test to 23cache, that doesn't work
David Kamholz authored
180
181 $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] });
182 $artist = $rs->find(1);
183
d52170d @wdh nuke DBI->trace calls
wdh authored
184 is( $queries, 1, 'only one select statement on find with has_many prefetch on resultset' );
d2b3ea1 add second test to 23cache, that doesn't work
David Kamholz authored
185
a228776 @ribasushi Test cleanups
ribasushi authored
186 $schema->storage->debug($sdebug);
81549fa @ribasushi cleanup
ribasushi authored
187 $schema->storage->debugcb (undef);
Something went wrong with that request. Please try again.