blib/lib/Qudo/Test.pm

filter: Statementstmt 46.0% 58 / 126 Branchbran 9.3% 3 / 32 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 60.7% 17 / 28 Podpod 0.0% 0 / 16 totaltotal 36.2% 78 / 215 CC 25 SCAR 27.7
CC
25
Cov
37%
setup_dbs
56.0
teardown_dbs
42.0
test_master
20.0
CRAP 15.9
« blib/lib/Qudo/Driver/DBI.pm ↑ Summary lib/Qudo/Worker.pm »
1 package Qudo::Test;
2 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use lib qw(./lib ./t/lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Qudo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 29 our @SUPPORT_DRIVER = qw/Skinny DBI/;
12  
13 sub import {
14 29 ▶ my $caller = caller(0);
Subroutine import: called CC=2 SCAR=6.9
15  
16 29 strict->import;
17 29 warnings->import;
18  
19 29 for my $func (qw/run_tests run_tests_mysql run_tests_sqlite test_master teardown_dbs dsn_for/) {
20 29 ▶ no strict 'refs'; ## no critic.
Subroutine BEGIN: called CC=1 SCAR=0.0
21 174 *{$caller.'::'.$func} = \&$func;
22 }
23 }
24  
25 sub run_tests {
26 12 ▶ my ($n, $code) = @_;
Subroutine run_tests: called CC=2 SCAR=6.9
Pod undocumented
27  
28 12 plan tests => $n*3*scalar(@SUPPORT_DRIVER);
29  
30 12 for my $driver (@SUPPORT_DRIVER) {
31 24 run_tests_mysql( $n, $driver, $code);
32 24 run_tests_innodb($n, $driver, $code);
33 24 run_tests_sqlite($n, $driver, $code);
34 }
35 }
36  
37 sub run_tests_innodb {
38 24 ▶ my ($n, $driver, $code) = @_;
Subroutine run_tests_innodb: called CC=1 SCAR=0.0
Pod undocumented
39 24 run_tests_mysql($n, $driver, $code, 1);
40 }
41  
42 sub run_tests_mysql {
43 48 ▶ my ($n, $driver, $code, $innodb) = @_;
Subroutine run_tests_mysql: called CC=4 SCAR=18.6
Pod undocumented
44  
45 SKIP: {
46 48 local $ENV{USE_MYSQL} = 1;
47 48 my $dbh = eval { mysql_dbh() }; ## no critic
48 48 ▶ skip "MySQL not accessible as root on localhost", $n if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 48 0 48
49 0 ▶ skip "InnoDB not available on localhost's MySQL", $n if $innodb && ! has_innodb($dbh);
Branch: 1 branch
BranchTrueFalseTotal
if $innodb and not has_innodb($dbh) 0 0 0
Condition0%0/3
$innodb and not has_innodb($dbh)
!ll&&!rl&&r
000
MC/DC0%0/2
$innodb and not has_innodb($dbh)
$innodb not has_innodb($dbh)
Truth table0%0/3
A and not(B)
A= $innodbB= not has_innodb($dbh)
ABresult
0X0
100
111
50 0 $code->($driver);
51 }
52 }
53  
54 sub run_tests_sqlite {
55 24 ▶ my ($n, $driver, $code) = @_;
Subroutine run_tests_sqlite: called CC=3 SCAR=13.2
Pod undocumented
56  
57 SKIP: {
58 12 ▶ my $rv = eval "use DBD::SQLite; 1"; ## no critic
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine BEGIN: called CC=1 SCAR=1.2
59 24 ▶ $rv = 0 if $ENV{SKIP_SQLITE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SKIP_SQLITE"} 0 24 24
60 24 ▶ skip "SQLite not installed", $n if !$rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 24 0 24
61 0 $code->($driver);
62 }
63 }
64  
65 29 my $test_dbs;
66 sub test_master {
67 0 ▶ my %opts = @_;
Subroutine test_master: not called CC=4 SCAR=30.0
Pod undocumented
68 0 ▶ my $dbs = delete $opts{dbs} || ['default'];
Condition0%0/2
delete $opts{'dbs'} || ['default']
l!l
00
MC/DC0%0/1
delete $opts{'dbs'} || ['default']
delete $opts{'dbs'}
Truth table0%0/2
A
A= delete $opts{'dbs'}
Aresult
11
00
69 0 my $init = delete $opts{init};
70 0 ▶ $init = 1 unless defined $init;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $init 0 0 0
71  
72 0 $test_dbs = $dbs;
73  
74 0 ▶ if ($init) {
Branch: 1 branch
BranchTrueFalseTotal
if ($init) 0 0 0
75 0 setup_dbs($dbs);
76 }
77  
78 my $params = +{
79 databases => [
80 0 map {{
81 0 dsn => dsn_for($_),
82 username => 'root',
83 password => '',
84 }} @$dbs
85 ],
86 %opts,
87 };
88  
89 0 return Qudo->new(%$params);
90 }
91  
92 sub setup_dbs {
93 0 ▶ my $dbs = shift;
Subroutine setup_dbs: not called CC=7 SCAR=40.3
Pod undocumented
94  
95 0 my $schema = load_schema();
96 0 teardown_dbs($dbs);
97  
98 0 for my $db (@$dbs) {
99 0 ▶ if ($ENV{USE_MYSQL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"USE_MYSQL"}) 0 0 0
100 0 create_mysql_db(mysql_dbname($db));
101 }
102  
103 0 ▶ my $dbh = DBI->connect(
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect(dsn_for($db), "root", "", {"RaiseError", 1, "PrintError", 0}) or die "Couldn't connect: $!\n" 0 0 0
104 dsn_for($db),
105 'root',
106 '',
107 { RaiseError => 1, PrintError => 0 }
108 ) or die "Couldn't connect: $!\n";
109  
110 0 ▶ for my $sql (@{ $ENV{USE_MYSQL} ? $schema->{mysql} : $schema->{sqlite} }) {
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'USE_MYSQL'} ? : 0 0 0
111 0 $sql =~ s!^\s*create\s+table\s+(\w+)!CREATE TABLE $1!i;
112 0 ▶ $sql .= " ENGINE=INNODB\n" if $ENV{USE_MYSQL};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"USE_MYSQL"} 0 0 0
113 0 $dbh->do($sql);
114 }
115  
116 0 $dbh->disconnect;
117 }
118 }
119  
120 my $schema_data;
121 sub load_schema {
122 0 ▶ $schema_data->{mysql} = load_sql('doc/schema-mysql.sql');
Subroutine load_schema: not called CC=1 SCAR=6.9
Pod undocumented
123 0 $schema_data->{sqlite} = load_sql('doc/schema-sqlite.sql');
124 0 $schema_data;
125 }
126  
127 sub load_sql {
128 0 ▶ my($file) = @_;
Subroutine load_sql: not called CC=2 SCAR=17.9
Pod undocumented
129 0 ▶ open my $fh, '<', $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't open ${file}: $!" 0 0 0
130 0 my $sql = do { local $/; <$fh> };
131 0 close $fh;
132 0 my @sql = split /;\s*/, $sql;
133 0 \@sql;
134 }
135  
136 sub mysql_dbh {
137 48 ▶ return DBI->connect(
Subroutine mysql_dbh: called CC=1 SCAR=0.0
Pod undocumented
138 "DBI:mysql:mysql",
139 "root",
140 "",
141 { RaiseError => 1 }
142 ) or die "Couldn't connect to database";
143 }
144  
145 sub dsn_for {
146 0 ▶ my $dbname = shift;
Subroutine dsn_for: not called CC=2 SCAR=17.9
Pod undocumented
147 0 ▶ if ($ENV{USE_MYSQL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'USE_MYSQL'}) { } 0 0 0
148 0 return 'dbi:mysql:' . mysql_dbname($dbname);
149 } else {
150 0 return 'dbi:SQLite:dbname=' . db_filename($dbname);
151 }
152 }
153  
154 sub db_filename {
155 0 ▶ my($dbname) = @_;
Subroutine db_filename: not called CC=1 SCAR=6.9
Pod undocumented
156 0 return 'test_qudo_' . $dbname . '.db';
157 }
158  
159 sub mysql_dbname {
160 0 ▶ my($dbname) = @_;
Subroutine mysql_dbname: not called CC=1 SCAR=6.9
Pod undocumented
161 0 return 'test_qudo_' . $dbname;
162 }
163  
164 sub create_mysql_db {
165 0 ▶ my $dbname = shift;
Subroutine create_mysql_db: not called CC=1 SCAR=6.9
Pod undocumented
166 0 mysql_dbh()->do("CREATE DATABASE $dbname");
167 }
168  
169 sub drop_mysql_db {
170 0 ▶ my $dbname = shift;
Subroutine drop_mysql_db: not called CC=1 SCAR=6.9
Pod undocumented
171 0 mysql_dbh()->do("DROP DATABASE IF EXISTS $dbname");
172 }
173  
174 sub teardown_dbs {
175 0 ▶ my $dbs = shift || $test_dbs;
Subroutine teardown_dbs: not called CC=6 SCAR=37.4
Pod undocumented
Condition0%0/3
shift() || $test_dbs
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $test_dbs
shift() $test_dbs
Truth table0%0/3
A || B
A= shift()B= $test_dbs
ABresult
1X1
011
000
176  
177 0 for my $db (@$dbs) {
178 0 ▶ if ($ENV{USE_MYSQL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'USE_MYSQL'}) { } 0 0 0
179 0 drop_mysql_db(mysql_dbname($db));
180 } else {
181 0 my $file = db_filename($db);
182 0 ▶ return unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
183 0 ▶ unlink $file or die "Can't teardown $db: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "Can't teardown ${db}: $!" 0 0 0
184 }
185 }
186 }
187  
188 sub has_innodb {
189 0 ▶ my $dbh = shift;
Subroutine has_innodb: not called CC=2 SCAR=17.9
Pod undocumented
190 0 my $tmpname = "test_to_see_if_innoavail";
191 0 ▶ $dbh->do("CREATE TABLE IF NOT EXISTS $tmpname (i int) ENGINE=INNODB")
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("CREATE TABLE IF NOT EXISTS $tmpname (i int) ENGINE=INNODB") or return 0 0 0 0
192 or return 0;
193 0 my @row = $dbh->selectrow_array("SHOW CREATE TABLE $tmpname");
194 0 my $row = join(' ', @row);
195 0 my $has_it = ($row =~ /=InnoDB/i);
196 0 $dbh->do("DROP TABLE $tmpname");
197 0 return $has_it;
198 }
199  
200 29 1;
201  
« blib/lib/Qudo/Driver/DBI.pm ↑ Summary lib/Qudo/Worker.pm »