blib/lib/DBIO/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
« blib/lib/DBIO/SQLite/Deploy.pm ↑ Summary blib/lib/DBIO/SQLite/Util.pm »
1 package DBIO::SQLite::Test;
2 # ABSTRACT: SQLite-specific test utilities for DBIO
3  
4 165 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 165 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 165 our $VERSION = '0.900007';
8  
9 165 ▶use DBIO::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 165 ▶use DBIO::Test::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 165 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 165 ▶use DBIO::Util qw(dir_path parent_dir mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 165 ▶sub import {
Subroutine import: called CC=6 SCAR=18.3
16 165 my $self = shift;
17  
18 # Delegate :DiffSQL and other exports to DBIO::Test
19 165 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 29 136 165
20 29 my $caller = caller;
21 # Manually re-export into caller's namespace
22 29 for my $exp (@_) {
23 29 ▶ if ($exp eq ':DiffSQL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp eq ':DiffSQL') { } 29 0 29
elsif ($exp eq ':GlobalLock') { } 0 0 0
24 29 require DBIO::SQLMaker;
25 29 require SQL::Abstract::Test;
26 29 for (qw(is_same_sql_bind is_same_sql is_same_bind)) {
27 165 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 87 *{"${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 165 my $dir;
45 233 ▶ sub _vardir {
Subroutine _vardir: called CC=3 SCAR=11.0
46 233 ▶ return $dir if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 68 165 233
47 165 $dir = dir_path(
48 parent_dir(parent_dir(parent_dir(parent_dir(__FILE__)))),
49 't', 'var',
50 );
51 165 ▶ mkpath($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 164 165
52 165 return $dir;
53 }
54 }
55  
56  
57 165 ▶sub _sqlite_dbfilename {
Subroutine _sqlite_dbfilename: called CC=3 SCAR=11.0
58 233 my $self = shift;
59 233 ▶ my $holder = $ENV{DBIO_TEST_LOCK_HOLDER} || $$;
Condition66%2/3
$ENV{'DBIO_TEST_LOCK_HOLDER'} || $$
l!l&&r!l&&!r
32300
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 233 ▶ $holder = $$ if $holder == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $holder == -1 3 230 233
61 233 return _vardir() . "/DBIOTest-$holder.db";
62 }
63  
64  
65 412 ▶sub _sqlite_dbname {
Subroutine _sqlite_dbname: called CC=3 SCAR=11.0
66 412 my $self = shift;
67 412 my %args = @_;
68 return $self->_sqlite_dbfilename if (
69 412 ▶ 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 346 412
defined $args{'sqlite_use_file'} ? : 67 345 412
70 );
71 346 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 165 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 411 ▶sub _database {
Subroutine _database: called CC=6 SCAR=19.9
86 411 my $self = shift;
87 411 my %args = @_;
88  
89 411 my %connect_attrs = %args;
90 411 delete @connect_attrs{@HELPER_ARGS};
91  
92 411 ▶ if ($ENV{DBIO_TEST_DSN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DBIO_TEST_DSN"}) 0 411 411
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 411 my $db_file = $self->_sqlite_dbname(%args);
100  
101 411 for ($db_file, "${db_file}-journal") {
102 822 ▶ next unless -e $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $_ 790 32 822
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 421 ▶ on_connect_do => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
112 421 my $storage = shift;
113 421 my $dbh = $storage->_get_dbh;
114 421 $dbh->do('PRAGMA synchronous = OFF');
115  
116 420 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DBIO_TEST_SQLITE_REVERSE_DEFAULT_ORDER"} and $storage->_server_info->{"normalized_dbms_version"} >= "3.007009") 0 420 420
Condition33%1/3
$ENV{"DBIO_TEST_SQLITE_REVERSE_DEFAULT_ORDER"} and $storage->_server_info->{"normalized_dbms_version"} >= "3.007009"
!ll&&!rl&&r
42000
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 411 %connect_attrs,
125 });
126 }
127  
128  
129 398 ▶sub init_schema {
Subroutine init_schema: called CC=9 SCAR=22.1
Pod documented
130 398 my $self = shift;
131 398 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 398 ▶ $args{storage_type} //= 'DBIO::SQLite::Storage';
Condition100%2/2
$args{"storage_type"} //= "DBIO::SQLite::Storage"
l!l
6392
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 398 %args = %{ DBIO::Test->normalize_init_schema_args(\%args) };
139 398 $args{storage_type} = $self->_backend_storage_class($args{storage_type});
140  
141 398 my $schema;
142  
143 398 ▶ if ($args{compose_connection}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'compose_connection'}) { } 0 398 398
144 0 $schema = DBIO::Test::Schema->compose_connection(
145 'DBIO::Test', $self->_database(%args)
146 );
147 } else {
148 398 $schema = DBIO::Test::Schema->compose_namespace('DBIO::Test');
149 }
150  
151 398 ▶ if ($args{storage_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"storage_type"}) 398 0 398
152 398 $schema->storage_type($args{storage_type});
153 }
154  
155 398 ▶ if (!$args{no_connect}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_connect"}) 397 1 398
156 397 $schema = $schema->connect($self->_database(%args));
157  
158 397 ▶ 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 395 397
Condition66%2/3
$args{"replicant_connect_info"} and $schema->storage->isa("DBIO::Replicated::Storage")
!ll&&!rl&&r
39502
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 398 ▶ if (!$args{no_deploy}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_deploy"}) 341 57 398
164 341 DBIO::Test->deploy_schema($schema, $args{deploy_args});
165 DBIO::Test->populate_schema($schema)
166 341 ▶ unless $args{no_populate};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_populate"} 316 25 341
167 }
168  
169 398 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 398 ▶sub _backend_storage_class {
Subroutine _backend_storage_class: called CC=6 SCAR=18.3
178 398 my ($self, $storage_type) = @_;
179  
180 398 ▶ return $storage_type unless ref $storage_type eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $storage_type eq "HASH" 394 4 398
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 165 ▶sub _cleanup_dbfile {
Subroutine _cleanup_dbfile: called CC=5 SCAR=17.8
194 165 my $self = shift;
195 165 ▶ 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"} == $$) 165 0 165
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
16500
not $ENV{"DBIO_TEST_LOCK_HOLDER"} or $ENV{"DBIO_TEST_LOCK_HOLDER"} == -1
l!l&&r!l&&!r
16410
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 165 my $db_file = $self->_sqlite_dbfilename;
203 165 unlink $_ for ($db_file, "${db_file}-journal");
204 }
205 }
206  
207 164 ▶END {
Subroutine END: called CC=1 SCAR=0.0
208 164 __PACKAGE__->_cleanup_dbfile;
209 }
210  
211 165 1;
212  
213 __END__
« blib/lib/DBIO/SQLite/Deploy.pm ↑ Summary blib/lib/DBIO/SQLite/Util.pm »