blib/lib/Qudo/Test.pm

filter: Statementstmt 47.1% 57 / 121 Branchbran 9.3% 3 / 32 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 59.2% 16 / 27 Podpod 0.0% 0 / 16 totaltotal 35.0% 76 / 217 CC 27 SCAR 27.7
CC
27
Cov
36%
setup_dbs
56.0
teardown_dbs
42.0
test_master
20.0
CRAP 15.9
« lib/Qudo/Worker.pm ↑ Summary blib/lib/Qudo.pm »
1 package Qudo::Test;
2 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use lib qw(./lib ./t/lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use Qudo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 32 our @SUPPORT_DRIVER = qw/Skinny/;
12  
13 32 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
14 32 my $caller = caller(0);
15  
16 32 strict->import;
17 32 warnings->import;
18  
19 32 for my $func (qw/run_tests run_tests_mysql run_tests_sqlite test_master teardown_dbs dsn_for/) {
20 32 ▶ no strict 'refs'; ## no critic.
Subroutine BEGIN: called CC=1 SCAR=0.0
21 192 *{$caller.'::'.$func} = \&$func;
22 }
23 }
24  
25 32 ▶sub run_tests {
Subroutine run_tests: called CC=2 SCAR=6.9
Pod undocumented
26 32 my ($n, $code) = @_;
27  
28 32 plan tests => $n*3*scalar(@SUPPORT_DRIVER);
29  
30 32 for my $driver (@SUPPORT_DRIVER) {
31 32 run_tests_mysql( $n, $driver, $code);
32 32 run_tests_innodb($n, $driver, $code);
33 32 run_tests_sqlite($n, $driver, $code);
34 }
35 }
36  
37 32 ▶sub run_tests_innodb {
Subroutine run_tests_innodb: called CC=1 SCAR=0.0
Pod undocumented
38 32 my ($n, $driver, $code) = @_;
39 32 run_tests_mysql($n, $driver, $code, 1);
40 }
41  
42 64 ▶sub run_tests_mysql {
Subroutine run_tests_mysql: called CC=4 SCAR=18.6
Pod undocumented
43 64 my ($n, $driver, $code, $innodb) = @_;
44  
45 SKIP: {
46 64 local $ENV{USE_MYSQL} = 1;
47 64 my $dbh = eval { mysql_dbh() }; ## no critic
48 64 ▶ skip "MySQL not accessible as root on localhost", $n if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 64 0 64
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 32 ▶sub run_tests_sqlite {
Subroutine run_tests_sqlite: called CC=3 SCAR=12.6
Pod undocumented
55 32 my ($n, $driver, $code) = @_;
56  
57 SKIP: {
58 0 ▶ my $rv = eval "use DBD::SQLite; 1"; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.6
59 32 ▶ $rv = 0 if $ENV{SKIP_SQLITE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SKIP_SQLITE"} 0 32 32
60 32 ▶ skip "SQLite not installed", $n if !$rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 32 0 32
61 0 $code->($driver);
62 }
63 }
64  
65 32 my $test_dbs;
66 0 ▶sub test_master {
Subroutine test_master: not called CC=4 SCAR=30.0
Pod undocumented
67 0 my %opts = @_;
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 0 ▶sub setup_dbs {
Subroutine setup_dbs: not called CC=7 SCAR=40.3
Pod undocumented
93 0 my $dbs = shift;
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 0 ▶sub load_schema {
Subroutine load_schema: not called CC=2 SCAR=17.9
Pod undocumented
122 0 ▶ $schema_data ||= YAML::Load(join "", <DATA>);
Condition0%0/3
$schema_data ||= YAML::Load(join("", readline DATA))
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema_data ||= YAML::Load(join("", readline DATA))
$schema_data YAML::Load(join("", readline DATA))
Truth table0%0/3
A ||= B
A= $schema_dataB= YAML::Load(join("", readline DATA))
ABresult
1X1
011
000
123 }
124  
125 0 ▶sub load_sql {
Subroutine load_sql: not called CC=2 SCAR=17.9
Pod undocumented
126 0 my($file) = @_;
127 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
128 0 my $sql = do { local $/; <$fh> };
129 0 close $fh;
130 0 my @sql = split /;\s*/, $sql;
131 0 \@sql;
132 }
133  
134 64 ▶sub mysql_dbh {
Subroutine mysql_dbh: called CC=2 SCAR=11.6
Pod undocumented
135 64 ▶ return DBI->connect(
Condition0%0/2
'DBI'->connect('DBI:mysql:mysql', 'root', '', {'RaiseError', 1}) || die(q[Couldn't connect to database])
l!l
00
MC/DC0%0/1
'DBI'->connect('DBI:mysql:mysql', 'root', '', {'RaiseError', 1}) || die(q[Couldn't connect to database])
'DBI'->connect('DBI:mysql:mysql', 'root', '', {'RaiseError', 1})
Truth table0%0/2
A
A= 'DBI'->connect('DBI:mysql:mysql', 'root', '', {'RaiseError', 1})
Aresult
11
00
136 "DBI:mysql:mysql",
137 "root",
138 "",
139 { RaiseError => 1 }
140 ) || die "Couldn't connect to database";
141 }
142  
143 0 ▶sub dsn_for {
Subroutine dsn_for: not called CC=2 SCAR=17.9
Pod undocumented
144 0 my $dbname = shift;
145 0 ▶ if ($ENV{USE_MYSQL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'USE_MYSQL'}) { } 0 0 0
146 0 return 'dbi:mysql:' . mysql_dbname($dbname);
147 } else {
148 0 return 'dbi:SQLite:dbname=' . db_filename($dbname);
149 }
150 }
151  
152 0 ▶sub db_filename {
Subroutine db_filename: not called CC=1 SCAR=6.9
Pod undocumented
153 0 my $dbname = shift;
154 0 'test_qudo_' . $dbname . '.db';
155 }
156  
157 0 ▶sub mysql_dbname {
Subroutine mysql_dbname: not called CC=1 SCAR=6.9
Pod undocumented
158 0 my $dbname = shift;
159 0 'test_qudo_' . $dbname;
160 }
161  
162 0 ▶sub create_mysql_db {
Subroutine create_mysql_db: not called CC=1 SCAR=6.9
Pod undocumented
163 0 my $dbname = shift;
164 0 mysql_dbh()->do("CREATE DATABASE $dbname");
165 }
166  
167 0 ▶sub drop_mysql_db {
Subroutine drop_mysql_db: not called CC=1 SCAR=6.9
Pod undocumented
168 0 my $dbname = shift;
169 0 mysql_dbh()->do("DROP DATABASE IF EXISTS $dbname");
170 }
171  
172 0 ▶sub teardown_dbs {
Subroutine teardown_dbs: not called CC=6 SCAR=37.4
Pod undocumented
173 0 ▶ my $dbs = shift || $test_dbs;
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
174  
175 0 for my $db (@$dbs) {
176 0 ▶ if ($ENV{USE_MYSQL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'USE_MYSQL'}) { } 0 0 0
177 0 drop_mysql_db(mysql_dbname($db));
178 } else {
179 0 my $file = db_filename($db);
180 0 ▶ return unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
181 0 ▶ unlink $file or die "Can't teardown $db: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "Can't teardown ${db}: $!" 0 0 0
182 }
183 }
184 }
185  
186 0 ▶sub has_innodb {
Subroutine has_innodb: not called CC=2 SCAR=17.9
Pod undocumented
187 0 my $dbh = shift;
188 0 my $tmpname = "test_to_see_if_innoavail";
189 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
190 or return 0;
191 0 my @row = $dbh->selectrow_array("SHOW CREATE TABLE $tmpname");
192 0 my $row = join(' ', @row);
193 0 my $has_it = ($row =~ /=InnoDB/i);
194 0 $dbh->do("DROP TABLE $tmpname");
195 0 return $has_it;
196 }
197  
198 32 1;
199  
200 __DATA__
« lib/Qudo/Worker.pm ↑ Summary blib/lib/Qudo.pm »