blib/lib/DBIO/Test/Storage.pm

filter: Statementstmt 91.6% 220 / 240 Branchbran 75.8% 44 / 58 Conditioncond 50.0% 20 / 40 MC/DCmcdc 18.1% 4 / 22 Subroutinesub 76.2% 61 / 80 Podpod 100% 23 / 23 totaltotal 80.3% 372 / 463 CC 51 SCAR 10.3
CC
51
Cov
84%
_insert_bulk
8.1
is_exhausted
6.0
position
6.0
CRAP 2.8
« blib/lib/DBIO/DeploymentHandler/VersionStorage/Standard/Component.pm ↑ Summary blib/lib/DBIO/Generate/Util.pm »
1 package DBIO::Test::Storage;
2 # ABSTRACT: Fake storage for testing SQL generation without a database
3  
4 277 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 277 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 277 our $VERSION = '0.900007';
8  
9 277 ▶use base qw(DBIO::Storage::DBI);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 277 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 277 ▶use DBIO::Storage::Statistics;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 277 __PACKAGE__->datetime_parser_type('DBIO::Test::DateTimeParser');
16  
17 # ADR 0037: the fake storage renders the base CASE form of _exists_value_sql,
18 # which is the standard spelling -- so it declares the capability, the way a
19 # driver whose engine takes the base rendering does. Without the declaration
20 # every core test touching results_exist would be testing the refusal.
21 277 __PACKAGE__->_use_supports_exists_query(1);
22  
23 277 __PACKAGE__->mk_group_accessors(simple => qw(
24 _captured_queries
25 _fake_connected
26 _mock_results
27 _last_insert_ids
28 _auto_increment
29 _mock_affected_rows
30 _mock_funcs
31 ));
32  
33 2903 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
34 2903 my $self = shift->next::method(@_);
35 2903 $self->_captured_queries([]);
36 2903 $self->_fake_connected(1);
37 2903 $self->{_fake_dbh} = DBIO::Test::Storage::FakeDbh->new;
38 2903 $self->_mock_results([]);
39 2903 $self->_last_insert_ids({});
40 2903 $self->_auto_increment({});
41 2903 $self->_mock_funcs({});
42 2903 ▶ $self->{_sql_maker_opts} ||= {};
Condition50%1/2
$self->{"_sql_maker_opts"} ||= {}
l!l
29030
MC/DC0%0/1
$self->{"_sql_maker_opts"} ||= {}
$self->{"_sql_maker_opts"}
Truth table50%1/2
A
A= $self->{"_sql_maker_opts"}
Aresult
11
00
43 2903 $self->{_driver_determined} = 1;
44 # ADR 0030: the mock storage defaults to the 'immediate' async mode so mock
45 # tests exercise *_async with no event loop (degrade to an immediately
46 # resolved DBIO::Future::Immediate), instead of croaking as a sync instance would.
47 2903 $self->_async_mode('immediate');
48 2903 $self;
49 }
50  
51  
52 49 ▶sub connected { $_[0]->_fake_connected }
Subroutine connected: called CC=1 SCAR=0.0
Pod documented
53  
54  
55 24 ▶sub ensure_connected { 1 }
Subroutine ensure_connected: called CC=1 SCAR=0.0
Pod documented
56  
57 0 ▶sub _populate_dbh { }
Subroutine _populate_dbh: not called CC=1 SCAR=0.0
58  
59  
60 86 ▶sub disconnect {
Subroutine disconnect: called CC=1 SCAR=0.0
Pod documented
61 86 my $self = shift;
62 86 $self->_fake_connected(0);
63 86 $self->next::method;
64 86 1;
65 }
66  
67 # We have no real dbh, but we do hand out a stand-in for the one thing that
68 # reads _dbh without ever talking to a database: L<DBIO::Storage::TxnScopeGuard>
69 # weakens a ref to it on construction and *declines to roll back* when that ref
70 # is empty. With no handle at all every implicit rollback on the fake storage
71 # was a silent no-op, so a test asserting "nothing was written" passed for the
72 # wrong reason -- the fake storage was never going to write. The handle makes
73 # the guard engage, and the ROLLBACK shows up in L</captured_queries> like any
74 # other statement.
75 #
76 # Deliberately not a setter: DBIO::Storage::DBI clears _dbh in disconnect() /
77 # DESTROY() / CLONE(), and the fake storage stays usable across all of those.
78 # _get_dbh remains undef -- code that wants a *working* handle must still not
79 # get one here.
80 9522 ▶sub _dbh { $_[0]->{_fake_dbh} }
Subroutine _dbh: called CC=1 SCAR=0.0
81 1034 ▶sub _get_dbh { undef }
Subroutine _get_dbh: called CC=1 SCAR=0.0
82 0 ▶sub _server_info { { dbms_version => 0, normalized_dbms_version => 0 } }
Subroutine _server_info: not called CC=1 SCAR=6.9
83  
84 600 ▶sub _determine_driver {
Subroutine _determine_driver: called CC=1 SCAR=0.0
85 600 $_[0]->{_driver_determined} = 1;
86 600 return '';
87 }
88 0 ▶sub _init {}
Subroutine _init: not called CC=1 SCAR=0.0
89 991 ▶sub _seems_connected { $_[0]->_fake_connected }
Subroutine _seems_connected: called CC=1 SCAR=0.0
90 1424 ▶sub _dbh_autocommit { 1 }
Subroutine _dbh_autocommit: called CC=1 SCAR=0.0
91  
92 # Override txn_begin/commit/rollback to skip real dbh checks
93 930 ▶sub txn_begin {
Subroutine txn_begin: called CC=1 SCAR=0.0
Pod documented
94 930 my $self = shift;
95 930 $self->next::method(@_);
96 }
97  
98 837 ▶sub txn_commit {
Subroutine txn_commit: called CC=2 SCAR=7.1
Pod documented
99 837 my $self = shift;
100 837 ▶ $self->throw_exception("Unable to txn_commit() on a disconnected storage")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_fake_connected 0 837 837
101 unless $self->_fake_connected;
102 837 $self->next::method(@_);
103 }
104  
105 81 ▶sub txn_rollback {
Subroutine txn_rollback: called CC=2 SCAR=7.1
Pod documented
106 81 my $self = shift;
107 81 ▶ $self->throw_exception("Unable to txn_rollback() on a disconnected storage")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_fake_connected 0 81 81
108 unless $self->_fake_connected;
109 81 $self->next::method(@_);
110 }
111  
112  
113 5755 ▶sub _execute {
Subroutine _execute: called CC=4 SCAR=14.3
114 5755 my ($self, $op, $ident, @args) = @_;
115  
116 5755 my ($sql, $bind) = $self->_prep_for_execute($op, $ident, \@args);
117  
118 # Track inserts for last_insert_id
119 5752 ▶ if ($op eq 'insert') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "insert") 3240 2512 5752
120 3240 ▶ my $source_name = ref $ident ? $ident->name : ($ident || '');
Branch: 1 branch
BranchTrueFalseTotal
ref $ident ? : 3240 0 3240
Condition0%0/2
$ident or ""
l!l
00
MC/DC0%0/1
$ident or ""
$ident
Truth table0%0/2
A
A= $ident
Aresult
11
00
121 3240 my $id = $self->_next_auto_id($source_name);
122 3240 $self->_last_insert_ids->{$source_name} = $id;
123 }
124  
125 5752 return $self->_capture_statement($op, $sql, $bind);
126 }
127  
128  
129 37 ▶sub _execute_rendered {
Subroutine _execute_rendered: called CC=1 SCAR=0.0
130 37 my ($self, $sql, $bind) = @_;
131  
132 # Same reshaping a real storage applies on this path -- a hybrid fake
133 # storage over DBIO::Storage::DBI::NoBindVars must capture the statement
134 # with its bind values interpolated, because that is what its engine would
135 # have received.
136 37 ($sql, $bind) = $self->_prep_rendered_for_execute($sql, $bind);
137  
138 37 return $self->_capture_statement('select', $sql, $bind);
139 }
140  
141  
142 14 ▶sub _prepare_rendered {
Subroutine _prepare_rendered: called CC=1 SCAR=0.0
143 14 my ($self, $sql, $bind) = @_;
144  
145 # Same reshaping the real seam applies (NoBindVars interpolation), for the
146 # same reason it applies it in _execute_rendered.
147 14 ($sql, $bind) = $self->_prep_rendered_for_execute($sql, $bind);
148  
149 14 return $self->_capture_sth('prepare', $sql, $bind);
150 }
151  
152 # Shared tail of _execute / _execute_rendered: log the statement, run the
153 # tracing hooks (so redact_bind_value is exercised on the fake storage too),
154 # and answer from the registered mocks.
155 5789 ▶sub _capture_statement {
Subroutine _capture_statement: called CC=5 SCAR=16.1
156 5789 my ($self, $op, $sql, $bind) = @_;
157  
158 5789 my $fake_sth = $self->_capture_sth($op, $sql, $bind);
159  
160 # The server-message seam. The real _dbh_execute routes the
161 # execute through _execute_sth so a driver can collect the messages a
162 # statement produced on the connection; the fake storage does the same, so a
163 # driver-shaped override is exercised here exactly as it would be against a
164 # real handle. The base _execute_sth answers (undef) -- no collection.
165 5787 my (undef, $messages) = $self->_execute_sth($self->_dbh, $fake_sth);
166 5787 $self->_server_messages($messages);
167  
168 # For DML operations (insert/update/delete), return 1 (row affected)
169 # For select, return '0E0' (zero but true)
170 5787 ▶ my $rv = ($op =~ /^(?:insert|update|delete)$/)
Branch: 2 branches
BranchTrueFalseTotal
$op =~ /^(?:insert|update|delete)$/ ? : 4609 1178 5787
defined $self->_mock_affected_rows ? : 1 4608 4609
171 ? ( defined $self->_mock_affected_rows ? $self->_mock_affected_rows : 1 )
172 : '0E0';
173  
174 5787 ▶ return (wantarray ? ($rv, $fake_sth, @{$bind||[]}) : $rv);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4412 1375 5787
Condition50%1/2
$bind || []
l!l
44120
MC/DC0%0/1
$bind || []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
175 }
176  
177 # The part _capture_statement and _prepare_rendered share: record the statement
178 # in the query log, run the tracing hooks (so redact_bind_value is exercised on
179 # the fake storage too) and build the handle from the registered mocks. It is
180 # the ONLY point at which a statement is seen at all on this storage, which is
181 # what makes a driver bypassing the seam loud here (ADR 0040 section 3).
182 5801 ▶sub _capture_sth {
Subroutine _capture_sth: called CC=2 SCAR=6.9
183 5801 my ($self, $op, $sql, $bind) = @_;
184  
185 5801 push @{ $self->_captured_queries }, {
186 op => $op,
187 sql => $sql,
188 bind => $bind,
189 };
190  
191 5801 $self->_query_start($sql, $bind);
192 5801 $self->_query_end($sql, $bind);
193  
194 5801 my $mock = $self->_find_mock($sql);
195  
196 return DBIO::Test::Storage::FakeSth->new(
197 $mock ? $mock->{rows} : undef,
198 5801 ▶ $self->_mock_funcs,
Branch: 1 branch
BranchTrueFalseTotal
$mock ? : 265 5536 5801
199 $sql,
200 );
201 }
202  
203  
204 1 ▶sub mock_affected_rows { shift->_mock_affected_rows(@_) }
Subroutine mock_affected_rows: called CC=1 SCAR=0.0
Pod documented
205  
206  
207 549 ▶sub captured_queries {
Subroutine captured_queries: called CC=2 SCAR=7.2
Pod documented
208 549 ▶ @{ $_[0]->_captured_queries || [] }
Condition50%1/2
$_[0]->_captured_queries || []
l!l
5490
MC/DC0%0/1
$_[0]->_captured_queries || []
$_[0]->_captured_queries
Truth table50%1/2
A
A= $_[0]->_captured_queries
Aresult
11
00
209 }
210  
211  
212 28 ▶sub captured_sql_bind {
Subroutine captured_sql_bind: called CC=3 SCAR=11.4
Pod documented
213 28 ▶ map { [ $_->{sql}, @{$_->{bind}||[]} ] } @{ $_[0]->_captured_queries || [] }
Condition50%2/4
$_->{'bind'} || []
l!l
190
$_[0]->_captured_queries || []
l!l
280
MC/DC0%0/1
$_->{'bind'} || []
$_->{'bind'}
MC/DC0%0/1
$_[0]->_captured_queries || []
$_[0]->_captured_queries
Truth table50%1/2
A
A= $_->{'bind'}
Aresult
11
00
Truth table50%1/2
A
A= $_[0]->_captured_queries
Aresult
11
00
214 }
215  
216  
217 786 ▶sub reset_captured {
Subroutine reset_captured: called CC=1 SCAR=0.0
Pod documented
218 786 $_[0]->_captured_queries([]);
219 }
220  
221  
222 996 ▶sub select {
Subroutine select: called CC=1 SCAR=0.0
Pod documented
223 996 my $self = shift;
224 996 my ($ident, $select, $condition, $attrs) = @_;
225 996 return DBIO::Test::Storage::FakeCursor->new($self, \@_, $attrs);
226 }
227  
228 1139 ▶sub select_single {
Subroutine select_single: called CC=1 SCAR=0.0
Pod documented
229 1139 my $self = shift;
230 1139 my ($rv, $sth, @bind) = $self->_execute('select', @_);
231 1139 my @row = $sth->fetchrow_array;
232 1139 $sth->finish;
233 1139 return @row;
234 }
235  
236 # Transaction tracking
237 225 ▶sub _exec_txn_begin {
Subroutine _exec_txn_begin: called CC=1 SCAR=0.0
238 225 push @{ $_[0]->_captured_queries }, { op => 'txn_begin', sql => 'BEGIN', bind => [] };
239 }
240  
241 139 ▶sub _exec_txn_commit {
Subroutine _exec_txn_commit: called CC=1 SCAR=0.0
242 139 push @{ $_[0]->_captured_queries }, { op => 'txn_commit', sql => 'COMMIT', bind => [] };
243 }
244  
245 68 ▶sub _exec_txn_rollback {
Subroutine _exec_txn_rollback: called CC=1 SCAR=0.0
246 68 push @{ $_[0]->_captured_queries }, { op => 'txn_rollback', sql => 'ROLLBACK', bind => [] };
247 }
248  
249 1 ▶sub _exec_svp_begin {
Subroutine _exec_svp_begin: called CC=1 SCAR=0.0
250 1 my ($self, $name) = @_;
251 1 push @{ $self->_captured_queries }, { op => 'svp_begin', sql => "SAVEPOINT $name", bind => [] };
252 }
253  
254 1 ▶sub _exec_svp_release {
Subroutine _exec_svp_release: called CC=1 SCAR=0.0
255 1 my ($self, $name) = @_;
256 1 push @{ $self->_captured_queries }, { op => 'svp_release', sql => "RELEASE SAVEPOINT $name", bind => [] };
257 }
258  
259 0 ▶sub _exec_svp_rollback {
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
260 0 my ($self, $name) = @_;
261 0 push @{ $self->_captured_queries }, { op => 'svp_rollback', sql => "ROLLBACK TO SAVEPOINT $name", bind => [] };
262 }
263  
264 # Deploy is a no-op
265 0 ▶sub deploy { }
Subroutine deploy: not called CC=1 SCAR=0.0
Pod documented
266  
267 8 ▶sub sqlt_type { 'NULL' }
Subroutine sqlt_type: called CC=1 SCAR=0.0
Pod documented
268  
269 # Override _insert_bulk to avoid needing a real dbh
270 8 ▶sub _insert_bulk {
Subroutine _insert_bulk: called CC=8 SCAR=20.9
271 8 my ($self, $source, $cols, $data, @rest) = @_;
272  
273 # Same column check as DBIO::Storage::DBI::_insert_bulk (unknown columns
274 # throw, naming the column), and the INSERT is rendered by the SQLMaker,
275 # so quoting is the live quoting (cursor invariant, karr k55 / k394).
276 $self->throw_exception('Calling _insert_bulk without a dataset to process makes no sense')
277 8 ▶ unless @{$data||[]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$data or [];} 0 8 8
Condition50%1/2
$data || []
l!l
80
MC/DC0%0/1
$data || []
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
278 8 $source->columns_info($cols);
279  
280 6 my %proto;
281 6 for my $idx (0 .. $#$cols) {
282 13 my $v = $data->[0][$idx];
283 13 ▶ $proto{ $cols->[$idx] } =
Branch: 2 branches
BranchTrueFalseTotal
ref $v eq 'SCALAR' ? : 2 11 13
ref $v eq 'REF' && ref $$v eq 'ARRAY' ? : 2 9 11
Condition66%2/3
ref $v eq "REF" and ref $$v eq "ARRAY"
!ll&&!rl&&r
902
MC/DC50%1/2
ref $v eq "REF" and ref $$v eq "ARRAY"
ref $v eq "REF" ref $$v eq "ARRAY"
Truth table66%2/3
A and B
A= ref $v eq "REF"B= ref $$v eq "ARRAY"
ABresult
0X0
100
111
284 ref $v eq 'SCALAR' ? $v
285 : (ref $v eq 'REF' and ref $$v eq 'ARRAY') ? \[ @$$v ]
286 : \'?';
287 }
288 6 my ($sql) = $self->sql_maker->insert($source->from, \%proto);
289 6 push @{ $self->_captured_queries }, {
290 op => 'insert',
291 sql => $sql,
292 bind => [],
293 };
294  
295 # Track auto-increment
296 6 my $source_name = $source->name;
297 6 for (1 .. scalar @$data) {
298 10 my $id = $self->_next_auto_id($source_name);
299 10 $self->_last_insert_ids->{$source_name} = $id;
300 }
301  
302 6 return;
303 }
304  
305 # _prepare_sth without a real dbh
306 1 ▶sub _prepare_sth {
Subroutine _prepare_sth: called CC=1 SCAR=0.0
307 1 return DBIO::Test::Storage::FakeSth->new;
308 }
309  
310 # --- Mock result system ---
311  
312  
313 166 ▶sub mock {
Subroutine mock: called CC=3 SCAR=11.4
Pod documented
314 166 my ($self, $pattern, $rows, $cols) = @_;
315 166 ▶ $pattern = qr/\Q$pattern\E/ unless ref $pattern eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pattern eq "Regexp" 0 166 166
316 166 ▶ push @{ $self->_mock_results }, {
Condition50%1/2
$rows || []
l!l
1660
MC/DC0%0/1
$rows || []
$rows
Truth table50%1/2
A
A= $rows
Aresult
11
00
317 pattern => $pattern,
318 rows => $rows || [],
319 columns => $cols,
320 persistent => 0,
321 };
322 }
323  
324  
325 149 ▶sub mock_persistent {
Subroutine mock_persistent: called CC=3 SCAR=11.4
Pod documented
326 149 my ($self, $pattern, $rows, $cols) = @_;
327 149 ▶ $pattern = qr/\Q$pattern\E/ unless ref $pattern eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pattern eq "Regexp" 0 149 149
328 149 ▶ push @{ $self->_mock_results }, {
Condition50%1/2
$rows || []
l!l
1490
MC/DC0%0/1
$rows || []
$rows
Truth table50%1/2
A
A= $rows
Aresult
11
00
329 pattern => $pattern,
330 rows => $rows || [],
331 columns => $cols,
332 persistent => 1,
333 };
334 }
335  
336  
337 133 ▶sub clear_mocks {
Subroutine clear_mocks: called CC=1 SCAR=0.0
Pod documented
338 133 $_[0]->_mock_results([]);
339 133 %{$_[0]->_mock_funcs} = ();
340 }
341  
342  
343 8 ▶sub mock_func {
Subroutine mock_func: called CC=1 SCAR=0.0
Pod documented
344 8 my ($self, $name, $answer) = @_;
345 8 $self->_mock_funcs->{$name} = $answer;
346 8 return $self;
347 }
348  
349 2692 ▶sub _find_mock {
Subroutine _find_mock: called CC=4 SCAR=13.9
350 2692 my ($self, $sql) = @_;
351 2692 my $mocks = $self->_mock_results;
352  
353 # LIFO search -- last registered mock wins
354 2692 for my $i (reverse 0 .. $#$mocks) {
355 578 ▶ if ($sql =~ $mocks->[$i]{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /$mocks->[$i]{"pattern"}/) 405 173 578
356 405 my $mock = $mocks->[$i];
357 405 ▶ splice @$mocks, $i, 1 unless $mock->{persistent};
Branch: 1 branch
BranchTrueFalseTotal
unless $mock->{"persistent"} 161 244 405
358 405 return $mock;
359 }
360 }
361 2287 return undef;
362 }
363  
364 # Track auto-increment per source for last_insert_id
365 3250 ▶sub _next_auto_id {
Subroutine _next_auto_id: called CC=2 SCAR=6.9
366 3250 my ($self, $source_name) = @_;
367 3250 ▶ $self->_auto_increment->{$source_name} ||= 0;
Condition100%2/2
$self->_auto_increment->{$source_name} ||= 0
l!l
2463787
MC/DC100%1/1
$self->_auto_increment->{$source_name} ||= 0
$self->_auto_increment->{$source_name}
Truth table100%2/2
A
A= $self->_auto_increment->{$source_name}
Aresult
11
00
368 3250 return ++$self->_auto_increment->{$source_name};
369 }
370  
371  
372 22 ▶sub set_auto_increment {
Subroutine set_auto_increment: called CC=1 SCAR=0.0
Pod documented
373 22 my ($self, $source_name, $val) = @_;
374 22 $self->_auto_increment->{$source_name} = $val - 1;
375 }
376  
377 2326 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=3 SCAR=13.1
Pod documented
378 2326 my ($self, $source, @cols) = @_;
379 2326 ▶ my $source_name = ref $source ? $source->name : ($source || '');
Branch: 1 branch
BranchTrueFalseTotal
ref $source ? : 2326 0 2326
Condition0%0/2
$source or ""
l!l
00
MC/DC0%0/1
$source or ""
$source
Truth table0%0/2
A
A= $source
Aresult
11
00
380 2326 return $self->_last_insert_ids->{$source_name};
381 }
382 0 ▶sub _dbh_last_insert_id { $_[0]->last_insert_id }
Subroutine _dbh_last_insert_id: not called CC=1 SCAR=6.9
383  
384 # No DBI bind attrs needed
385 3 ▶sub _dbi_attrs_for_bind { [] }
Subroutine _dbi_attrs_for_bind: called CC=1 SCAR=0.0
386  
387 # columns_info_for comes from the Result class definitions, no DB introspection
388 0 ▶sub columns_info_for { {} }
Subroutine columns_info_for: not called CC=1 SCAR=6.9
Pod documented
389  
390 # dbh_do without a real dbh - just run the coderef with undef dbh
391 1 ▶sub dbh_do {
Subroutine dbh_do: called CC=2 SCAR=7.4
Pod documented
392 1 my $self = shift;
393 1 my $run_target = shift;
394  
395 1 ▶ if (not ref $run_target) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $run_target) { } 0 1 1
396 0 $self->$run_target(undef, @_);
397 }
398 else {
399 1 $run_target->($self, undef, @_);
400 }
401 }
402  
403 # ---- Fake database handle ----
404 #
405 # Only what the storage layer touches on a handle it never executes anything
406 # against: attribute access (both hash-style and FETCH/STORE), the connectivity
407 # probes, and the transaction verbs. The transaction verbs are dead ends here --
408 # DBIO::Test::Storage overrides _exec_txn_* to capture BEGIN/COMMIT/ROLLBACK
409 # instead of routing them to the handle -- they exist so an unexpected route to
410 # the handle is a no-op rather than a crash.
411 #
412 # Note the absence of column_info: DBIO::Storage::DBI::_dbh_columns_info_for
413 # branches on $dbh->can('column_info'), and the fake storage must keep taking
414 # the "no introspection" branch.
415  
416 {
417 package DBIO::Test::Storage::FakeDbh;
418  
419 2907 ▶ sub new {
Subroutine new: called CC=2 SCAR=8.1
420 2907 my $class = shift;
421 2907 ▶ bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
029070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
422 Active => 1,
423 AutoCommit => 1,
424 RaiseError => 0,
425 PrintError => 0,
426 CachedKids => {},
427 Driver => { Name => 'Test' },
428 }, ref $class || $class;
429 }
430  
431 5 ▶ sub FETCH { $_[0]->{ $_[1] } }
Subroutine FETCH: called CC=1 SCAR=0.0
432 0 ▶ sub STORE { $_[0]->{ $_[1] } = $_[2] }
Subroutine STORE: not called CC=1 SCAR=6.9
433  
434 0 ▶ sub ping { 1 }
Subroutine ping: not called CC=1 SCAR=6.9
435 101 ▶ sub disconnect { $_[0]->{disconnected}++; 1 }
Subroutine disconnect: called CC=1 SCAR=0.0
436  
437 0 ▶ sub begin_work { 1 }
Subroutine begin_work: not called CC=1 SCAR=6.9
438 0 ▶ sub commit { 1 }
Subroutine commit: not called CC=1 SCAR=6.9
439 0 ▶ sub rollback { 1 }
Subroutine rollback: not called CC=1 SCAR=6.9
440  
441 2 ▶ sub prepare { DBIO::Test::Storage::FakeSth->new }
Subroutine prepare: called CC=1 SCAR=0.0
442 0 ▶ sub prepare_cached { DBIO::Test::Storage::FakeSth->new }
Subroutine prepare_cached: not called CC=1 SCAR=6.9
443 }
444  
445 # ---- Fake statement handle ----
446  
447 {
448 277 package DBIO::Test::Storage::FakeSth;
449  
450 5805 ▶ sub new {
Subroutine new: called CC=4 SCAR=14.3
451 5805 my ($class, $rows, $funcs, $sql) = @_;
452 5805 ▶ bless {
Condition71%5/7
$rows || []
l!l
2655540
$funcs || {}
l!l
58014
ref $class || $class
l!l&&r!l&&!r
058050
MC/DC100%1/1
$rows || []
$rows
MC/DC100%1/1
$funcs || {}
$funcs
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table100%2/2
A
A= $rows
Aresult
11
00
Truth table100%2/2
A
A= $funcs
Aresult
11
00
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
453 rows => $rows || [],
454 pos => 0,
455 funcs => $funcs || {},
456 sql => $sql,
457 }, ref $class || $class;
458 }
459  
460 1198 ▶ sub fetchrow_array {
Subroutine fetchrow_array: called CC=3 SCAR=11.0
461 1198 my $self = shift;
462 1198 ▶ return () if $self->{pos} >= scalar @{$self->{rows}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pos"} >= scalar @{$self->{"rows"};} 971 227 1198
463 227 my $row = $self->{rows}[$self->{pos}++];
464 227 ▶ return ref $row eq 'ARRAY' ? @$row : @$row;
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'ARRAY' ? : 227 0 227
465 }
466  
467 0 ▶ sub fetchrow_hashref { undef }
Subroutine fetchrow_hashref: not called CC=1 SCAR=6.9
468  
469 27 ▶ sub fetchall_arrayref {
Subroutine fetchall_arrayref: called CC=2 SCAR=6.9
470 27 my $self = shift;
471 27 my @rows;
472 27 ▶ while (my @row = $self->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $self->fetchrow_array) 25 27 52
473 25 push @rows, [@row];
474 }
475 27 return \@rows;
476 }
477  
478 1146 ▶ sub finish { 1 }
Subroutine finish: called CC=1 SCAR=0.0
479 5788 ▶ sub execute { '0E0' }
Subroutine execute: called CC=1 SCAR=0.0
480 0 ▶ sub bind_param { 1 }
Subroutine bind_param: not called CC=1 SCAR=6.9
481 0 ▶ sub rows { scalar @{$_[0]->{rows}} }
Subroutine rows: not called CC=1 SCAR=6.9
482  
483 # DBI's dispatcher for a DBD's private statement-handle functions. Several
484 # engines answer a question about a PREPARED statement here rather than
485 # through an attribute -- Firebird's query plan is func('ib_plan') -- so a
486 # driver-shaped override written against _prepare_rendered can be exercised
487 # in its real form against this handle. There is no engine behind the fake
488 # storage, so the default answer is undef; what a driver test registers for
489 # a name through the storage's mock_func() seam (DBIO::Test::Storage
490 # /mock_func) is what this handle answers, because _capture_sth builds the
491 # handle with the storage's registrations. A handle built without them
492 # (FakeDbh->prepare, _prepare_sth) always answers undef.
493 20 ▶ sub func {
Subroutine func: called CC=4 SCAR=13.9
494 20 my ($self, $name, @args) = @_;
495 20 ▶ my $funcs = $self->{funcs} || {};
Condition50%1/2
$self->{'funcs'} || {}
l!l
200
MC/DC0%0/1
$self->{'funcs'} || {}
$self->{'funcs'}
Truth table50%1/2
A
A= $self->{'funcs'}
Aresult
11
00
496 20 ▶ return undef unless exists $funcs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $funcs->{$name} 11 9 20
497 9 my $answer = $funcs->{$name};
498 9 ▶ return ref $answer eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $answer eq 'CODE' ? : 3 6 9
499 ? $answer->($self, $name, @args)
500 : $answer;
501 }
502 }
503  
504 # ---- Fake cursor ----
505  
506 {
507 277 package DBIO::Test::Storage::FakeCursor;
508  
509 277 ▶ use base 'DBIO::Cursor';
Subroutine BEGIN: called CC=1 SCAR=0.0
510  
511 996 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.0
512 996 my ($class, $storage, $args, $attrs) = @_;
513  
514 # Generate the SQL to check for mocks. Run the raw select arguments through
515 # _select_args first (exactly like the real DBIO::Storage::DBI::Cursor does
516 # via storage->_select), so join pruning / complex-prefetch rewriting is
517 # applied before the SQL is rendered and captured.
518 996 my ($op, $ident, @select_args) = $storage->_select_args(@$args);
519 995 my ($sql, $bind) = $storage->_prep_for_execute($op, $ident, \@select_args);
520  
521 994 push @{ $storage->_captured_queries }, {
522 op => 'select',
523 sql => $sql,
524 bind => $bind,
525 };
526  
527 994 $storage->_query_start($sql, $bind);
528 994 $storage->_query_end($sql, $bind);
529  
530 994 my $mock = $storage->_find_mock($sql);
531  
532 my $self = bless {
533 storage => $storage,
534 args => $args,
535 attrs => $attrs,
536 994 ▶ rows => $mock ? [ @{$mock->{rows}} ] : [],
Branch: 1 branch
BranchTrueFalseTotal
$mock ? : 925 69 994
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
09940
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
537 pos => 0,
538 }, ref $class || $class;
539  
540 994 return $self;
541 }
542  
543 1267 ▶ sub next {
Subroutine next: called CC=4 SCAR=13.9
544 1267 my $self = shift;
545 1267 ▶ return () if $self->{_done};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_done"} 1 1266 1267
546 1266 ▶ if ($self->{pos} >= scalar @{$self->{rows}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pos"} >= scalar @{$self->{"rows"};}) 665 601 1266
547 # mirror the real cursor: exhaustion latches _done so a further
548 # ->next stays empty until an explicit ->reset
549 665 $self->{_done} = 1;
550 665 return ();
551 }
552 601 my $row = $self->{rows}[$self->{pos}++];
553 601 ▶ return ref $row eq 'ARRAY' ? @$row : @$row;
Branch: 1 branch
BranchTrueFalseTotal
ref $row eq 'ARRAY' ? : 601 0 601
554 }
555  
556 644 ▶ sub all {
Subroutine all: called CC=2 SCAR=6.9
557 644 my $self = shift;
558 # mirror the real cursor: a bare ->all re-reads from the top,
559 # independent of any prior iteration or exhaustion state
560 644 $self->reset;
561 644 my @all;
562 644 ▶ while (my @row = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $self->next) 250 644 894
563 250 push @all, \@row;
564 }
565 644 $self->reset;
566 644 return @all;
567 }
568  
569 1931 ▶ sub reset {
Subroutine reset: called CC=1 SCAR=0.0
570 1931 my $self = shift;
571 1931 $self->{pos} = 0;
572 1931 delete $self->{_done};
573 }
574  
575 # Public cursor accessors matching DBIO::Storage::DBI::Cursor.
576 # The mock must track position/exhaustion exactly like the real cursor so
577 # the ResultSet collapse fast-path behaves identically under mock storage.
578 0 ▶ sub is_exhausted { $_[0]->{_done} ? 1 : 0 }
Subroutine is_exhausted: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_done'} ? : 0 0 0
579  
580 0 ▶ sub position { $_[0]->{pos} || 0 }
Subroutine position: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"pos"} or 0 0 0 0
581  
582 0 ▶ sub mark_exhausted { $_[0]->{_done} = 1 }
Subroutine mark_exhausted: not called CC=1 SCAR=6.9
583 }
584  
585 277 1;
586  
587 __END__
« blib/lib/DBIO/DeploymentHandler/VersionStorage/Standard/Component.pm ↑ Summary blib/lib/DBIO/Generate/Util.pm »