SQLite/Test.pm

filter: Statementstmt 95.1% 98 / 103 Branchbran 77.2% 34 / 44 Conditioncond 50.0% 12 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 80.7% 164 / 203 CC 36 SCAR 16.7
CC
36
Cov
84%
init_schema
9.1
_database
7.3
import
6.2
CRAP 5.3
« SQLite/Deploy.pm ↑ Summary SQLite/Util.pm »
1 package DBIO::SQLite::Test;
2 # ABSTRACT: SQLite-specific test utilities for DBIO
3  
4 232 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 232 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 232 our $VERSION = '0.900004';
8  
9 232 ▶use DBIO::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 232 ▶use DBIO::Test::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 232 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 232 ▶use DBIO::Util qw(dir_path parent_dir mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 232 ▶sub import {
Subroutine import: called CC=6 SCAR=18.3
16 232 my $self = shift;
17  
18 # Delegate :DiffSQL and other exports to DBIO::Test
19 232 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 43 189 232
20 43 my $caller = caller;
21 # Manually re-export into caller's namespace
22 43 for my $exp (@_) {
23 43 ▶ if ($exp eq ':DiffSQL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp eq ':DiffSQL') { } 43 0 43
elsif ($exp eq ':GlobalLock') { } 0 0 0
24 43 require DBIO::SQLMaker;
25 43 require SQL::Abstract::Test;
26 43 for (qw(is_same_sql_bind is_same_sql is_same_bind)) {
27 232 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 129 *{"${caller}::$_"} = \&{"SQL::Abstract::Test::$_"};
29 }
30 }
31 elsif ($exp eq ':GlobalLock') {
32 # GlobalLock is a no-op in the DBIO test suite — the original
33 # DBIOTest locking mechanism is not needed outside of the old
34 # concurrent test infrastructure.
35 }
36 else {
37 0 croak "Unknown export $exp requested from $self";
38 }
39 }
40 }
41 }
42  
43 {
44 232 my $dir;
45 300 ▶ sub _vardir {
Subroutine _vardir: called CC=3 SCAR=11.0
46 300 ▶ return $dir if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 68 232 300
47 232 $dir = dir_path(
48 parent_dir(parent_dir(parent_dir(parent_dir(__FILE__)))),
49 't', 'var',
50 );
51 232 ▶ mkpath($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 231 232
52 232 return $dir;
53 }
54 }
55  
56  
57 232 ▶sub _sqlite_dbfilename {
Subroutine _sqlite_dbfilename: called CC=3 SCAR=11.0
58 300 my $self = shift;
59 300 ▶ my $holder = $ENV{DBIO_TEST_LOCK_HOLDER} || $$;
Condition66%2/3
$ENV{'DBIO_TEST_LOCK_HOLDER'} || $$
l!l&&r!l&&!r
32970
MC/DC0%0/2
$ENV{'DBIO_TEST_LOCK_HOLDER'} || $$
$ENV{'DBIO_TEST_LOCK_HOLDER'} $$
Truth table66%2/3
A || B
A= $ENV{'DBIO_TEST_LOCK_HOLDER'}B= $$
ABresult
1X1
011
000
60 300 ▶ $holder = $$ if $holder == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $holder == -1 3 297 300
61 300 return _vardir() . "/DBIOTest-$holder.db";
62 }
63  
64  
65 429 ▶sub _sqlite_dbname {
Subroutine _sqlite_dbname: called CC=3 SCAR=11.0
66 429 my $self = shift;
67 429 my %args = @_;
68 return $self->_sqlite_dbfilename if (
69 429 ▶ defined $args{sqlite_use_file} ? $args{sqlite_use_file} : $ENV{'DBIO_TEST_SQLITE_USE_FILE'}
Branch: 2 branches
BranchTrueFalseTotal
if defined $args{"sqlite_use_file"} ? $args{"sqlite_use_file"} : $ENV{"DBIO_TEST_SQLITE_USE_FILE"} 66 363 429
defined $args{'sqlite_use_file'} ? : 67 362 429
70 );
71 363 return ":memory:";
72 }
73  
74  
75 # Arguments of init_schema that steer the helper itself. They are not
76 # connect attributes and must never reach connect(): the storage rejects a
77 # connect attribute it does not know (karr k76).
78 232 my @HELPER_ARGS = qw(
79 dsn connect_info user pass connect_opts
80 no_deploy no_populate no_connect deploy_args
81 storage_type replicated replicant_connect_info compose_connection
82 sqlite_use_file
83 );
84  
85 428 ▶sub _database {
Subroutine _database: called CC=6 SCAR=19.9
86 428 my $self = shift;
87 428 my %args = @_;
88  
89 428 my %connect_attrs = %args;
90 428 delete @connect_attrs{@HELPER_ARGS};
91  
92 428 ▶ if ($ENV{DBIO_TEST_DSN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DBIO_TEST_DSN"}) 0 428 428
93 return (
94 0 ▶ (map { $ENV{"DBIO_TEST_${_}"} // '' } qw/DSN DBUSER DBPASS/),
Condition0%0/2
$ENV{"DBIO_TEST_$_"} // ""
l!l
00
MC/DC0%0/1
$ENV{"DBIO_TEST_$_"} // ""
$ENV{"DBIO_TEST_$_"}
Truth table0%0/2
A
A= $ENV{"DBIO_TEST_$_"}
Aresult
11
00
95 { AutoCommit => 1, %connect_attrs },
96 );
97 }
98  
99 428 my $db_file = $self->_sqlite_dbname(%args);
100  
101 428 for ($db_file, "${db_file}-journal") {
102 856 ▶ next unless -e $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $_ 824 32 856
103 32 ▶ unlink ($_) or carp (
Branch: 1 branch
BranchTrueFalseTotal
unlink $_ or carp("Unable to unlink existing test database file $_ ($!), " . 'creation of fresh database / further tests may fail!') 32 0 32
104 "Unable to unlink existing test database file $_ ($!), "
105 . "creation of fresh database / further tests may fail!"
106 );
107 }
108  
109 return ("dbi:SQLite:${db_file}", '', '', {
110 AutoCommit => 1,
111 434 ▶ on_connect_do => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
112 434 my $storage = shift;
113 434 my $dbh = $storage->_get_dbh;
114 434 $dbh->do('PRAGMA synchronous = OFF');
115  
116 433 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DBIO_TEST_SQLITE_REVERSE_DEFAULT_ORDER"} and $storage->_server_info->{"normalized_dbms_version"} >= "3.007009") 0 433 433
Condition33%1/3
$ENV{"DBIO_TEST_SQLITE_REVERSE_DEFAULT_ORDER"} and $storage->_server_info->{"normalized_dbms_version"} >= "3.007009"
!ll&&!rl&&r
43300
MC/DC0%0/2
$ENV{"DBIO_TEST_SQLITE_REVERSE_DEFAULT_ORDER"} and $storage->_server_info->{"normalized_dbms_version"} >= "3.007009"
$ENV{"DBIO_TEST_SQLITE_REVERSE_DEFAULT_ORDER"} $storage->_server_info->{"normalized_dbms_version"} >= "3.007009"
Truth table33%1/3
A and B
A= $ENV{"DBIO_TEST_SQLITE_REVERSE_DEFAULT_ORDER"}B= $storage->_server_info->{"normalized_dbms_version"} >= "3.007009"
ABresult
0X0
100
111
117 $ENV{DBIO_TEST_SQLITE_REVERSE_DEFAULT_ORDER}
118 and
119 $storage->_server_info->{normalized_dbms_version} >= 3.007009
120 ) {
121 0 $dbh->do('PRAGMA reverse_unordered_selects = ON');
122 }
123 },
124 428 %connect_attrs,
125 });
126 }
127  
128  
129 415 ▶sub init_schema {
Subroutine init_schema: called CC=9 SCAR=22.1
Pod documented
130 415 my $self = shift;
131 415 my %args = @_;
132  
133 # A schema always has a driver (core ADR 0067), and DBIO::Test::Schema
134 # keeps none in its class: this helper declares SQLite unless the caller
135 # names a storage_type. Set before the normalization, so that
136 # replicated => 1 runs its backends on it as well.
137 415 ▶ $args{storage_type} //= 'DBIO::SQLite::Storage';
Condition100%2/2
$args{"storage_type"} //= "DBIO::SQLite::Storage"
l!l
6409
MC/DC100%1/1
$args{"storage_type"} //= "DBIO::SQLite::Storage"
$args{"storage_type"}
Truth table100%2/2
A
A= $args{"storage_type"}
Aresult
11
00
138 415 %args = %{ DBIO::Test->normalize_init_schema_args(\%args) };
139 415 $args{storage_type} = $self->_backend_storage_class($args{storage_type});
140  
141 415 my $schema;
142  
143 415 ▶ if ($args{compose_connection}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'compose_connection'}) { } 0 415 415
144 0 $schema = DBIO::Test::Schema->compose_connection(
145 'DBIO::Test', $self->_database(%args)
146 );
147 } else {
148 415 $schema = DBIO::Test::Schema->compose_namespace('DBIO::Test');
149 }
150  
151 415 ▶ if ($args{storage_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"storage_type"}) 415 0 415
152 415 $schema->storage_type($args{storage_type});
153 }
154  
155 415 ▶ if (!$args{no_connect}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_connect"}) 414 1 415
156 414 $schema = $schema->connect($self->_database(%args));
157  
158 414 ▶ if ($args{replicant_connect_info} && $schema->storage->isa('DBIO::Replicated::Storage')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"replicant_connect_info"} and $schema->storage->isa("DBIO::Replicated::Storage")) 2 412 414
Condition66%2/3
$args{"replicant_connect_info"} and $schema->storage->isa("DBIO::Replicated::Storage")
!ll&&!rl&&r
41202
MC/DC50%1/2
$args{"replicant_connect_info"} and $schema->storage->isa("DBIO::Replicated::Storage")
$args{"replicant_connect_info"} $schema->storage->isa("DBIO::Replicated::Storage")
Truth table66%2/3
A and B
A= $args{"replicant_connect_info"}B= $schema->storage->isa("DBIO::Replicated::Storage")
ABresult
0X0
100
111
159 2 $schema->storage->connect_replicants(@{ $args{replicant_connect_info} });
160 }
161 }
162  
163 415 ▶ if (!$args{no_deploy}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_deploy"}) 354 61 415
164 354 DBIO::Test->deploy_schema($schema, $args{deploy_args});
165 DBIO::Test->populate_schema($schema)
166 354 ▶ unless $args{no_populate};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_populate"} 329 25 354
167 }
168  
169 415 return $schema;
170 }
171  
172 # normalize_init_schema_args records the driver of replicated => 1 as
173 # backend_storage_type; DBIO::Replicated::Storage builds its master and
174 # replicants from backend_storage_class. Hand the driver on under that name,
175 # unless the caller gave backend_storage_class itself. Any other storage_type
176 # is returned as given.
177 415 ▶sub _backend_storage_class {
Subroutine _backend_storage_class: called CC=6 SCAR=18.3
178 415 my ($self, $storage_type) = @_;
179  
180 415 ▶ return $storage_type unless ref $storage_type eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $storage_type eq "HASH" 411 4 415
181  
182 4 my ($storage_class, $args) = %$storage_type;
183 4 (my $plain = $storage_class) =~ s/^\+//;
184 4 ▶ return $storage_type unless $plain eq 'DBIO::Replicated::Storage';
Branch: 1 branch
BranchTrueFalseTotal
unless $plain eq "DBIO::Replicated::Storage" 0 4 4
185  
186 4 ▶ my %replicated_args = %{ $args || {} };
Condition50%1/2
$args || {}
l!l
40
MC/DC0%0/1
$args || {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
187 4 my $backend = delete $replicated_args{backend_storage_type};
188 4 ▶ $replicated_args{backend_storage_class} //= $backend if defined $backend;
Branch: 1 branch
BranchTrueFalseTotal
if defined $backend 3 1 4
Condition33%1/3
$replicated_args{"backend_storage_class"} //= $backend
l!l&&r!l&&!r
030
MC/DC0%0/2
$replicated_args{"backend_storage_class"} //= $backend
$replicated_args{"backend_storage_class"} $backend
Truth table33%1/3
A //= B
A= $replicated_args{"backend_storage_class"}B= $backend
ABresult
1X1
011
000
189  
190 4 return { $storage_class => \%replicated_args };
191 }
192  
193 232 ▶sub _cleanup_dbfile {
Subroutine _cleanup_dbfile: called CC=5 SCAR=17.8
194 232 my $self = shift;
195 232 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{"DBIO_TEST_LOCK_HOLDER"} or $ENV{"DBIO_TEST_LOCK_HOLDER"} == -1 or $ENV{"DBIO_TEST_LOCK_HOLDER"} == $$) 232 0 232
Condition50%3/6
not $ENV{"DBIO_TEST_LOCK_HOLDER"} or $ENV{"DBIO_TEST_LOCK_HOLDER"} == -1 or $ENV{"DBIO_TEST_LOCK_HOLDER"} == $$
l!l&&r!l&&!r
23200
not $ENV{"DBIO_TEST_LOCK_HOLDER"} or $ENV{"DBIO_TEST_LOCK_HOLDER"} == -1
l!l&&r!l&&!r
23110
MC/DC0%0/3
not $ENV{"DBIO_TEST_LOCK_HOLDER"} or $ENV{"DBIO_TEST_LOCK_HOLDER"} == -1 or $ENV{"DBIO_TEST_LOCK_HOLDER"} == $$
not $ENV{"DBIO_TEST_LOCK_HOLDER"} $ENV{"DBIO_TEST_LOCK_HOLDER"} == -1 $ENV{"DBIO_TEST_LOCK_HOLDER"} == $$
Truth table50%2/4
not(A) or B or C
A= not $ENV{"DBIO_TEST_LOCK_HOLDER"}B= $ENV{"DBIO_TEST_LOCK_HOLDER"} == -1C= $ENV{"DBIO_TEST_LOCK_HOLDER"} == $$
ABCresult
1XX1
01X1
0011
0000
196 ! $ENV{DBIO_TEST_LOCK_HOLDER}
197 or
198 $ENV{DBIO_TEST_LOCK_HOLDER} == -1
199 or
200 $ENV{DBIO_TEST_LOCK_HOLDER} == $$
201 ) {
202 232 my $db_file = $self->_sqlite_dbfilename;
203 232 unlink $_ for ($db_file, "${db_file}-journal");
204 }
205 }
206  
207 231 ▶END {
Subroutine END: called CC=1 SCAR=0.0
208 231 __PACKAGE__->_cleanup_dbfile;
209 }
210  
211 232 1;
212  
213 __END__
« SQLite/Deploy.pm ↑ Summary SQLite/Util.pm »