| 1 | package DBIx::Fast::Test::Mock; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Object::Pad 0.807; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DBI (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use DBIx::Fast (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use DBIx::Fast::Connector (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||
| 13 | # A DBIx::Fast with no database behind it: every statement DBIx::Fast sends | ||||||||||||||||||||||||||||||||||||
| 14 | # is recorded and answered from canned responses. Works below DBIx::Fast (a | ||||||||||||||||||||||||||||||||||||
| 15 | # private DBI driver), so every method - val, hash, insert, upsert, txn... - | ||||||||||||||||||||||||||||||||||||
| 16 | # runs its real code and generates the real SQL of the chosen dialect. | ||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | 1 | my %STATE; # id => shared state (weak; the dbh and the mock hold it) | |||||||||||||||||||||||||||||||||||
| 19 | 1 | my $NEXT_ID = 0; | |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||
| 21 | 1 | my %DSN_FOR = ( | |||||||||||||||||||||||||||||||||||
| 22 | MariaDB => 'mariadb://mock@mock/mock', | ||||||||||||||||||||||||||||||||||||
| 23 | mysql => 'mysql://mock@mock/mock', | ||||||||||||||||||||||||||||||||||||
| 24 | Pg => 'postgres://mock@mock/mock', | ||||||||||||||||||||||||||||||||||||
| 25 | SQLite => 'dbi:SQLite:dbname=mock', | ||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | class DBIx::Fast::Test::Mock { | ||||||||||||||||||||||||||||||||||||
| 29 | 0 | field $dialect :param :reader = 'MariaDB'; | |||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | field $strict :param :reader = 0; # unmatched statement => DB error | ||||||||||||||||||||||||||||||||||
|
Subroutine
dialect: not called CC=1 SCAR=2.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | field $args :param = {}; # extra DBIx::Fast constructor args | ||||||||||||||||||||||||||||||||||
|
Subroutine
strict: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 32 | 1 | field $_state; | |||||||||||||||||||||||||||||||||||
| 33 | 1 | field $_db; | |||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | ADJUST { | ||||||||||||||||||||||||||||||||||||
| 36 | die "DBIx::Fast::Test::Mock: unknown dialect '$dialect' (MariaDB, mysql, Pg, SQLite)\n" | ||||||||||||||||||||||||||||||||||||
| 37 | unless $DSN_FOR{$dialect}; | ||||||||||||||||||||||||||||||||||||
| 38 | die "DBIx::Fast::Test::Mock: args must be a hashref\n" unless ref $args eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | my $id = ++$NEXT_ID; | ||||||||||||||||||||||||||||||||||||
| 41 | # Shared with the driver's handles: plain data, no reference back to the | ||||||||||||||||||||||||||||||||||||
| 42 | # DBIx::Fast object (no cycle). | ||||||||||||||||||||||||||||||||||||
| 43 | $_state = { rules => [], queries => [], strict => $strict, | ||||||||||||||||||||||||||||||||||||
| 44 | dialect => $dialect, last_id => undef, auto_id => 0 }; | ||||||||||||||||||||||||||||||||||||
| 45 | $STATE{$id} = $_state; | ||||||||||||||||||||||||||||||||||||
| 46 | Scalar::Util::weaken($STATE{$id}); | ||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | my $raise = $args->{RaiseError} // 1; | ||||||||||||||||||||||||||||||||||||
| 49 | my $connector = DBIx::Fast::Connector->new( | ||||||||||||||||||||||||||||||||||||
| 50 | dsn => "dbi:DBIxFastMock:id=$id", | ||||||||||||||||||||||||||||||||||||
| 51 | attrs => { RaiseError => $raise, PrintError => 0, AutoCommit => 1 }, | ||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||
| 53 | $_db = DBIx::Fast->new(%$args, dsn => $DSN_FOR{$dialect}, db => $connector); | ||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | # The DBIx::Fast instance to hand to the code under test. | ||||||||||||||||||||||||||||||||||||
| 57 | 17 | ▶ | method db () { $_db } | ||||||||||||||||||||||||||||||||||
|
Subroutine
db: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | # Answer statements matching $match (a regex, or a string compared with | ||||||||||||||||||||||||||||||||||||
| 60 | # whitespace collapsed) with $response. First matching rule wins. | ||||||||||||||||||||||||||||||||||||
| 61 | 16 | ▶ | method on ($match, $response) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
on: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 62 | 16 | ▶ | die "DBIx::Fast::Test::Mock: on() needs a regex or SQL string\n" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $match and length $match
MC/DC50%1/2
defined $match and length $match
defined $match
length $match
Truth table66%2/3
A and B
A= defined $matchB= length $match
| |||||||||||||||||||||||||||||||||||||
| 63 | unless defined $match && length $match; | ||||||||||||||||||||||||||||||||||||
| 64 | 15 | push @{ $_state->{rules} }, [ $match, $response ]; | |||||||||||||||||||||||||||||||||||
| 65 | 15 | return $self; | |||||||||||||||||||||||||||||||||||
| 66 | 1 | } | |||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | # Every statement run, in order: { sql => ..., binds => [...] } | ||||||||||||||||||||||||||||||||||||
| 69 | # (transaction control appears as BEGIN / COMMIT / ROLLBACK). | ||||||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | method queries () { @{ $_state->{queries} } } | ||||||||||||||||||||||||||||||||||
|
Subroutine
queries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | method sql () { map { $_->{sql} } @{ $_state->{queries} } } | ||||||||||||||||||||||||||||||||||
|
Subroutine
sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||
| 74 | 6 | ▶ | method last_query () { $_state->{queries}[-1] } | ||||||||||||||||||||||||||||||||||
|
Subroutine
last_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | # How many recorded statements match $match. | ||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | method called ($match) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
called: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 78 | 2 | return scalar grep { _matches($match, $_->{sql}) } @{ $_state->{queries} }; | |||||||||||||||||||||||||||||||||||
| 79 | 1 | } | |||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | # Forget the recorded statements (rules stay). | ||||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | method reset () { @{ $_state->{queries} } = (); return $self } | ||||||||||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||
| 84 | # Forget the rules. | ||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | method clear_rules () { @{ $_state->{rules} } = (); return $self } | ||||||||||||||||||||||||||||||||||
|
Subroutine
clear_rules: not called CC=1 SCAR=4.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||
| 87 | 10 | ▶ | sub _norm ($sql) { my $s = $sql =~ s/\s+/ /gr; $s =~ s/\A | \z//g; $s } | ||||||||||||||||||||||||||||||||||
|
Subroutine
_norm: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||
| 89 | 52 | ▶ | sub _matches ($match, $sql) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_matches: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 90 | 52 | ▶ | return ref $match eq 'Regexp' ? $sql =~ $match : _norm($match) eq _norm($sql); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | # Response for a statement, normalized to | ||||||||||||||||||||||||||||||||||||
| 95 | # { columns => [...], data => [[...]] } (a result set) or | ||||||||||||||||||||||||||||||||||||
| 96 | # { rows => N, last_id => N } (a write) or { error => ..., err => ..., state => ... }. | ||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub _respond ($state, $sql, $binds) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_respond: called CC=25 SCAR=34.7
| |||||||||||||||||||||||||||||||||||||
| 98 | 32 | my $resp; | |||||||||||||||||||||||||||||||||||
| 99 | my $found; | ||||||||||||||||||||||||||||||||||||
| 100 | 32 | for my $rule (@{ $state->{rules} }) { | |||||||||||||||||||||||||||||||||||
| 101 | 44 | ▶ | next unless DBIx::Fast::Test::Mock::_matches($rule->[0], $sql); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 102 | 17 | ($resp, $found) = ($rule->[1], 1); | |||||||||||||||||||||||||||||||||||
| 103 | 17 | last; | |||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||
| 105 | 32 | ▶ | unless ($found) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 106 | return { error => "DBIx::Fast::Test::Mock: no response for: $sql", | ||||||||||||||||||||||||||||||||||||
| 107 | 15 | ▶ | err => 1, state => 'HY000' } if $state->{strict}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 108 | 13 | ▶ | return $sql =~ /\A\s*(?:SELECT|WITH|SHOW|PRAGMA|DESCRIBE|EXPLAIN|VALUES)\b/i | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 109 | ? { columns => [], data => [] } | ||||||||||||||||||||||||||||||||||||
| 110 | : { rows => 1 }; | ||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||
| 112 | 17 | ▶ | $resp = $resp->($sql, @$binds) if ref $resp eq 'CODE'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | 17 | ▶ | return { columns => ['value'], data => [] } unless defined $resp; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 115 | 17 | ▶ | return { columns => ['value'], data => [ [$resp] ] } unless ref $resp; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | 14 | ▶ | if (ref $resp eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | return { columns => ['value'], data => [] } unless @$resp; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 119 | 5 | ▶ | if (ref $resp->[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 120 | 3 | my @cols = sort keys %{ $resp->[0] }; | |||||||||||||||||||||||||||||||||||
| 121 | 3 | return { columns => \@cols, data => [ map { [ @$_{@cols} ] } @$resp ] }; | |||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||
| 123 | 2 | return { columns => [ map { "col$_" } 1 .. @{ $resp->[0] } ], data => $resp }; | |||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||
| 126 | 9 | ▶ | if (ref $resp eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 127 | return { %$resp, err => $resp->{err} // 1, state => $resp->{state} // 'HY000' } | ||||||||||||||||||||||||||||||||||||
| 128 | 9 | ▶ | if defined $resp->{error}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$resp->{"err"} // 1
$resp->{"state"} // "HY000"
MC/DC0%0/1
$resp->{"err"} // 1
$resp->{"err"}
MC/DC0%0/1
$resp->{"state"} // "HY000"
$resp->{"state"}
Truth table50%1/2
A
A= $resp->{"err"}
Truth table50%1/2
A
A= $resp->{"state"}
| |||||||||||||||||||||||||||||||||||||
| 129 | 8 | ▶ | if ($resp->{columns} || $resp->{data}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$resp->{"columns"} or $resp->{"data"}
MC/DC50%1/2
$resp->{"columns"} or $resp->{"data"}
$resp->{"columns"}
$resp->{"data"}
Truth table66%2/3
A or B
A= $resp->{"columns"}B= $resp->{"data"}
| |||||||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | my $data = $resp->{data} // []; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$resp->{"data"} // []
MC/DC0%0/1
$resp->{"data"} // []
$resp->{"data"}
Truth table50%1/2
A
A= $resp->{"data"}
| |||||||||||||||||||||||||||||||||||||
| 131 | my $cols = $resp->{columns} | ||||||||||||||||||||||||||||||||||||
| 132 | 0 | // (@$data && ref $data->[0] eq 'HASH' ? [ sort keys %{ $data->[0] } ] | |||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | : [ map { "col$_" } 1 .. @{ $data->[0] // [undef] } ]); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition14%1/7
$resp->{"columns"} // (@$data && ref $data->[0] eq "HASH" ? [sort(keys %{$$data[0];})] : [map({"col$_";} 1 .. @{$data->[0] // [undef];})])
@$data and ref $data->[0] eq "HASH"
$data->[0] // [undef]
MC/DC0%0/1
$resp->{"columns"} // (@$data && ref $data->[0] eq "HASH" ? [sort(keys %{$$data[0];})] : [map({"col$_";} 1 .. @{$data->[0] // [undef];})])
$resp->{"columns"}
MC/DC0%0/1
$data->[0] // [undef]
$data->[0]
Truth table50%1/2
A
A= $resp->{"columns"}
Truth table0%0/2
A
A= $data->[0]
| |||||||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | $data = [ map { ref $_ eq 'HASH' ? [ @$_{@$cols} ] : $_ } @$data ]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 135 | 1 | return { columns => $cols, data => $data }; | |||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||
| 137 | 7 | ▶ | return { rows => $resp->{rows} // 1, last_id => $resp->{last_id} }; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$resp->{"rows"} // 1
MC/DC100%1/1
$resp->{"rows"} // 1
$resp->{"rows"}
Truth table100%2/2
A
A= $resp->{"rows"}
| |||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | 0 | die "DBIx::Fast::Test::Mock: unsupported response for '$sql': " . ref($resp) . "\n"; | |||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | # --- The private DBI driver --------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||
| 145 | package # hide from PAUSE | ||||||||||||||||||||||||||||||||||||
| 146 | DBD::DBIxFastMock; | ||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||
| 148 | 1 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||||||||||
| 149 | 1 | our $drh; | |||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||
| 151 | # Defined in this file: tell DBI's install_driver (a require) it is loaded. | ||||||||||||||||||||||||||||||||||||
| 152 | 1 | ▶ | $INC{'DBD/DBIxFastMock.pm'} //= __FILE__; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$INC{"DBD/DBIxFastMock.pm"} //= __FILE__
MC/DC0%0/1
$INC{"DBD/DBIxFastMock.pm"} //= __FILE__
$INC{"DBD/DBIxFastMock.pm"}
Truth table50%1/2
A
A= $INC{"DBD/DBIxFastMock.pm"}
| |||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||
| 154 | 1 | ▶ | sub driver ($class, $attr = {}) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
driver: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 155 | 1 | ▶ | return $drh if $drh; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 156 | # DBIx::Fast reads SQLite's last id through this driver method. | ||||||||||||||||||||||||||||||||||||
| 157 | 1 | ▶ | DBD::DBIxFastMock::db->install_method('sqlite_last_insert_rowid') | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 158 | unless defined &DBI::db::sqlite_last_insert_rowid; | ||||||||||||||||||||||||||||||||||||
| 159 | 1 | $drh = DBI::_new_drh("${class}::dr", { | |||||||||||||||||||||||||||||||||||
| 160 | Name => 'DBIxFastMock', Version => $VERSION, Attribution => 'DBIx::Fast::Test::Mock', | ||||||||||||||||||||||||||||||||||||
| 161 | }); | ||||||||||||||||||||||||||||||||||||
| 162 | 1 | return $drh; | |||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub CLONE { undef $drh } | ||||||||||||||||||||||||||||||||||
|
Subroutine
CLONE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||
| 167 | package # hide from PAUSE | ||||||||||||||||||||||||||||||||||||
| 168 | DBD::DBIxFastMock::dr; | ||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||
| 170 | 1 | our $imp_data_size = 0; | |||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||
| 172 | 11 | ▶ | sub connect ($drh, $dsn, $user = undef, $pass = undef, $attr = {}) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
connect: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||
| 173 | 11 | my ($id) = $dsn =~ /\bid=(\d+)/; | |||||||||||||||||||||||||||||||||||
| 174 | 11 | ▶ | my $state = defined $id ? $STATE{$id} : undef | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 175 | or return $drh->set_err(1, "DBIx::Fast::Test::Mock: no mock for '$dsn' (gone out of scope?)"); | ||||||||||||||||||||||||||||||||||||
| 176 | 11 | my ($outer, $dbh) = DBI::_new_dbh($drh, { Name => $dsn }); | |||||||||||||||||||||||||||||||||||
| 177 | 11 | $dbh->{dbixfastmock} = $state; | |||||||||||||||||||||||||||||||||||
| 178 | 11 | $dbh->STORE(Active => 1); | |||||||||||||||||||||||||||||||||||
| 179 | 11 | return $outer; | |||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub data_sources { () } | ||||||||||||||||||||||||||||||||||
|
Subroutine
data_sources: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 183 | 1 | ▶ | sub disconnect_all { } | ||||||||||||||||||||||||||||||||||
|
Subroutine
disconnect_all: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||
| 185 | package # hide from PAUSE | ||||||||||||||||||||||||||||||||||||
| 186 | DBD::DBIxFastMock::db; | ||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | 1 | our $imp_data_size = 0; | |||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||
| 190 | 27 | ▶ | sub prepare ($dbh, $statement, $attr = {}) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 191 | 27 | my ($outer, $sth) = DBI::_new_sth($dbh, { Statement => $statement }); | |||||||||||||||||||||||||||||||||||
| 192 | 27 | $sth->{dbixfastmock} = $dbh->{dbixfastmock}; | |||||||||||||||||||||||||||||||||||
| 193 | 27 | $sth->STORE(NUM_OF_PARAMS => scalar(() = $statement =~ /\?/g)); | |||||||||||||||||||||||||||||||||||
| 194 | 27 | return $outer; | |||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub ping ($dbh) { $dbh->FETCH('Active') ? 1 : 0 } | ||||||||||||||||||||||||||||||||||
|
Subroutine
ping: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub disconnect ($dbh) { $dbh->STORE(Active => 0); 1 } | ||||||||||||||||||||||||||||||||||
|
Subroutine
disconnect: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||
| 201 | 4 | ▶ | sub _log ($dbh, $sql) { push @{ $dbh->{dbixfastmock}{queries} }, { sql => $sql, binds => [] } } | ||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | sub begin_work ($dbh, @) { _log($dbh, 'BEGIN'); $dbh->SUPER::begin_work } | ||||||||||||||||||||||||||||||||||
|
Subroutine
begin_work: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 204 | 1 | ▶ | sub commit ($dbh) { _log($dbh, 'COMMIT'); 1 } | ||||||||||||||||||||||||||||||||||
|
Subroutine
commit: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 205 | 1 | ▶ | sub rollback ($dbh) { _log($dbh, 'ROLLBACK'); 1 } | ||||||||||||||||||||||||||||||||||
|
Subroutine
rollback: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||
| 207 | 4 | ▶ | sub last_insert_id ($dbh, @) { $dbh->{dbixfastmock}{last_id} } | ||||||||||||||||||||||||||||||||||
|
Subroutine
last_insert_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 208 | 1 | ▶ | sub sqlite_last_insert_rowid ($dbh) { $dbh->{dbixfastmock}{last_id} } | ||||||||||||||||||||||||||||||||||
|
Subroutine
sqlite_last_insert_rowid: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||
| 210 | 21 | ▶ | sub get_info ($dbh, $type) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
get_info: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 211 | 21 | my $d = $dbh->{dbixfastmock}{dialect}; | |||||||||||||||||||||||||||||||||||
| 212 | 21 | ▶ | return { 29 => ($d eq 'MariaDB' || $d eq 'mysql' ? '`' : '"'), 41 => '.' }->{$type}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$d eq "MariaDB" or $d eq "mysql"
MC/DC100%2/2
$d eq "MariaDB" or $d eq "mysql"
$d eq "MariaDB"
$d eq "mysql"
Truth table100%3/3
A or B
A= $d eq "MariaDB"B= $d eq "mysql"
| |||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||
| 215 | 38 | ▶ | sub FETCH ($dbh, $attr) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 216 | 38 | ▶ | return $dbh->{dbixfastmock}{last_id} if $attr eq 'mariadb_insertid' || $attr eq 'mysql_insertid'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$attr eq "mariadb_insertid" or $attr eq "mysql_insertid"
MC/DC100%2/2
$attr eq "mariadb_insertid" or $attr eq "mysql_insertid"
$attr eq "mariadb_insertid"
$attr eq "mysql_insertid"
Truth table100%3/3
A or B
A= $attr eq "mariadb_insertid"B= $attr eq "mysql_insertid"
| |||||||||||||||||||||||||||||||||||||
| 217 | 33 | ▶ | return 80400 if $attr eq 'mysql_serverversion'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 218 | 32 | return $dbh->SUPER::FETCH($attr); | |||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||
| 221 | 129 | ▶ | sub STORE ($dbh, $attr, $value) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 222 | # Tell DBI the driver handled AutoCommit (DBI's magic values). | ||||||||||||||||||||||||||||||||||||
| 223 | 129 | ▶ | $value = $value ? -901 : -900 if $attr eq 'AutoCommit'; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 224 | 129 | return $dbh->SUPER::STORE($attr, $value); | |||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||
| 227 | package # hide from PAUSE | ||||||||||||||||||||||||||||||||||||
| 228 | DBD::DBIxFastMock::st; | ||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||
| 230 | 1 | our $imp_data_size = 0; | |||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||
| 232 | 32 | ▶ | sub execute ($sth, @binds) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||
| 233 | 32 | my $state = $sth->{dbixfastmock}; | |||||||||||||||||||||||||||||||||||
| 234 | 32 | my $sql = $sth->{Statement}; | |||||||||||||||||||||||||||||||||||
| 235 | 32 | push @{ $state->{queries} }, { sql => $sql, binds => [@binds] }; | |||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||
| 237 | 32 | my $r = DBIx::Fast::Test::Mock::_respond($state, $sql, \@binds); | |||||||||||||||||||||||||||||||||||
| 238 | 32 | ▶ | return $sth->set_err($r->{err}, $r->{error}, $r->{state}) if defined $r->{error}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||
| 240 | 29 | ▶ | if ($r->{columns}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 241 | 12 | ▶ | my @cols = @{ $r->{columns} } ? @{ $r->{columns} } : ('value'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 242 | 12 | $sth->{dbixfastmock_rows} = [ @{ $r->{data} } ]; | |||||||||||||||||||||||||||||||||||
| 243 | 12 | $sth->{dbixfastmock_affected} = scalar @{ $r->{data} }; | |||||||||||||||||||||||||||||||||||
| 244 | 12 | $sth->STORE(NUM_OF_FIELDS => scalar @cols); | |||||||||||||||||||||||||||||||||||
| 245 | 12 | $sth->{NAME} = \@cols; | |||||||||||||||||||||||||||||||||||
| 246 | 12 | $sth->STORE(Active => 1); | |||||||||||||||||||||||||||||||||||
| 247 | 12 | ▶ | return @{ $r->{data} } || '0E0'; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
@{$r->{'data'};} || '0E0'
MC/DC100%1/1
@{$r->{'data'};} || '0E0'
@{$r->{'data'};}
Truth table100%2/2
A
A= @{$r->{'data'};}
| |||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||
| 250 | $state->{last_id} = $r->{last_id} // ($sql =~ /\A\s*(?:INSERT|REPLACE)\b/i | ||||||||||||||||||||||||||||||||||||
| 251 | 17 | ▶ | ? ++$state->{auto_id} : $state->{last_id}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r->{"last_id"} // ($sql =~ /\A\s*(?:INSERT|REPLACE)\b/iu ? ++$state->{"auto_id"} : $state->{"last_id"})
MC/DC100%2/2
$r->{"last_id"} // ($sql =~ /\A\s*(?:INSERT|REPLACE)\b/iu ? ++$state->{"auto_id"} : $state->{"last_id"})
$r->{"last_id"}
($sql =~ /\A\s*(?:INSERT|REPLACE)\b/iu ? ++$state->{"auto_id"} : $state->{"last_id"})
Truth table100%3/3
A // B
A= $r->{"last_id"}B= ($sql =~ /\A\s*(?:INSERT|REPLACE)\b/iu ? ++$state->{"auto_id"} : $state->{"last_id"})
| |||||||||||||||||||||||||||||||||||||
| 252 | 17 | $sth->{dbixfastmock_rows} = []; | |||||||||||||||||||||||||||||||||||
| 253 | 17 | $sth->{dbixfastmock_affected} = $r->{rows}; | |||||||||||||||||||||||||||||||||||
| 254 | 17 | ▶ | return $r->{rows} || '0E0'; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$r->{'rows'} || '0E0'
MC/DC0%0/1
$r->{'rows'} || '0E0'
$r->{'rows'}
Truth table50%1/2
A
A= $r->{'rows'}
| |||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||
| 257 | 17 | ▶ | sub fetch ($sth) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 258 | 17 | ▶ | my $row = shift @{ $sth->{dbixfastmock_rows} // [] }; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$sth->{"dbixfastmock_rows"} // []
MC/DC0%0/1
$sth->{"dbixfastmock_rows"} // []
$sth->{"dbixfastmock_rows"}
Truth table50%1/2
A
A= $sth->{"dbixfastmock_rows"}
| |||||||||||||||||||||||||||||||||||||
| 259 | 17 | ▶ | unless ($row) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 260 | 6 | $sth->STORE(Active => 0); | |||||||||||||||||||||||||||||||||||
| 261 | 6 | return undef; | |||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||
| 263 | 11 | return $sth->_set_fbav($row); | |||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||
| 265 | 1 | ▶ | { no warnings 'once'; *fetchrow_arrayref = \&fetch; } | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | sub rows ($sth) { $sth->{dbixfastmock_affected} // -1 } | ||||||||||||||||||||||||||||||||||
|
Subroutine
rows: not called CC=2 SCAR=17.9
Condition0%0/2
$sth->{"dbixfastmock_affected"} // -1
MC/DC0%0/1
$sth->{"dbixfastmock_affected"} // -1
$sth->{"dbixfastmock_affected"}
Truth table0%0/2
A
A= $sth->{"dbixfastmock_affected"}
| |||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||
| 269 | 4 | ▶ | sub FETCH ($sth, $attr) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||
| 270 | 4 | ▶ | return $sth->{NAME} if $attr eq 'NAME'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 271 | 4 | return $sth->SUPER::FETCH($attr); | |||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||
| 274 | 57 | ▶ | sub STORE ($sth, $attr, $value) { $sth->SUPER::STORE($attr, $value) } | ||||||||||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||
| 276 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||
| 278 | __END__ | ||||||||||||||||||||||||||||||||||||