DB2/Test/Conformance.pm

filter: Statementstmt 20.4% 28 / 137 Branchbran 1.7% 1 / 58 Conditioncond 12.5% 2 / 16 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 43.4% 10 / 23 Podpod 100% 11 / 11 totaltotal 20.6% 52 / 252 CC 43 SCAR 50.1
CC
43
Cov
15%
conformance_fact
420.0
conformance_cleanup
90.0
conformance_namespace
42.0
CRAP 149.4
↑ Summary DB2/Introspect/ForeignKeys.pm »
1 package DBIO::DB2::Test::Conformance;
2 # ABSTRACT: Answers the shared conformance case table for the Db2 driver
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.900007';
8  
9 5 ▶use base 'DBIO::Test::Conformance';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use DBIO::DB2::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use DBIO::DB2::Deploy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 5 my $SEQ = 0;
18 # schema class => the name of its case's schema
19 5 my %NAMESPACE;
20  
21  
22 3 ▶sub conformance_live_available {
Subroutine conformance_live_available: called CC=6 SCAR=30.1
Pod documented
23 return 0 unless length($ENV{DBIO_TEST_DB2_DSN} // '')
24 3 ▶ and length($ENV{DBIO_TEST_DB2_USER} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($ENV{"DBIO_TEST_DB2_DSN"} // "") and length($ENV{"DBIO_TEST_DB2_USER"} // "") 3 0 3
Condition28%2/7
length($ENV{"DBIO_TEST_DB2_DSN"} // "") and length($ENV{"DBIO_TEST_DB2_USER"} // "")
!ll&&!rl&&r
300
$ENV{"DBIO_TEST_DB2_DSN"} // ""
l!l
03
$ENV{"DBIO_TEST_DB2_USER"} // ""
l!l
00
MC/DC0%0/2
length($ENV{"DBIO_TEST_DB2_DSN"} // "") and length($ENV{"DBIO_TEST_DB2_USER"} // "")
$ENV{"DBIO_TEST_DB2_DSN"} $ENV{"DBIO_TEST_DB2_USER"}
Truth table33%1/3
A and B
A= $ENV{"DBIO_TEST_DB2_DSN"}B= $ENV{"DBIO_TEST_DB2_USER"}
ABresult
0X0
100
111
25 0 ▶ return eval { require DBD::DB2; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require DBD::DB2; 1 } } ? : 0 0 0
26 }
27  
28 # The name of the next case's schema. Upper case: Db2 stores an unquoted
29 # schema name that way and the introspector filters on the stored name.
30 0 ▶sub _schema_name { sprintf 'DBIO_CF_%d_%d', $$, ++$SEQ }
Subroutine _schema_name: not called CC=1 SCAR=6.9
31  
32 0 ▶sub _connect_in {
Subroutine _connect_in: not called CC=1 SCAR=6.9
33 0 my ($class, $schema_class, $name) = @_;
34 0 $NAMESPACE{$schema_class} = $name;
35 0 $schema_class->load_components('DB2');
36 return $schema_class->connect(
37 $ENV{DBIO_TEST_DB2_DSN}, $ENV{DBIO_TEST_DB2_USER}, $ENV{DBIO_TEST_DB2_PASS}, {
38 0 AutoCommit => 1, RaiseError => 1, PrintError => 0, PrintWarn => 0,
39 LongReadLen => 1_000_000,
40 on_connect_do => [ "SET CURRENT SCHEMA = '$name'" ],
41 });
42 }
43  
44  
45 0 ▶sub conformance_connect {
Subroutine conformance_connect: not called CC=1 SCAR=6.9
Pod documented
46 0 my ($class, $schema_class) = @_;
47 0 return $class->_connect_in($schema_class, $class->_schema_name);
48 }
49  
50 0 ▶sub _namespace_of {
Subroutine _namespace_of: not called CC=2 SCAR=17.9
51 0 my ($class, $schema) = @_;
52 0 my $name = $NAMESPACE{ ref $schema };
53 0 ▶ DBIO::Exception->throw('conformance: this schema was not connected through '
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
54 . 'conformance_connect') unless defined $name;
55 0 return $name;
56 }
57  
58  
59 0 ▶sub conformance_namespace {
Subroutine conformance_namespace: not called CC=6 SCAR=37.4
Pod documented
60 0 my ($class, $schema, $case) = @_;
61 0 my $name = $class->_namespace_of($schema);
62 0 my $dbh = $schema->storage->dbh;
63  
64 # A source named "other.t" is the table t in the schema other: the install
65 # makes that schema (Db2 does, with the first table of it), so the case drops
66 # it again -- and only a schema the case made: one that is there already makes
67 # the namespace throw, touching nothing.
68 0 my %extra;
69 0 for my $moniker ($schema->sources) {
70 0 my $table = $schema->source($moniker)->name;
71 0 ▶ next if ref $table;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table 0 0 0
72 0 ▶ $extra{$1} = 1 if $table =~ /\A([^."]+)\.[^."]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $table =~ /\A([^."]+)\.[^."]+\z/ 0 0 0
73 }
74 0 my @extra = sort grep { $_ ne $name } keys %extra;
75 0 for my $extra (@extra, $name) {
76 0 my ($there) = $dbh->selectrow_array(
77 'SELECT 1 FROM syscat.schemata WHERE schemaname = ?', undef, $extra);
78 0 ▶ DBIO::Exception->throw("conformance: the schema $extra exists already; "
Branch: 1 branch
BranchTrueFalseTotal
if $there 0 0 0
79 . 'refusing to use or drop a schema the case did not create') if $there;
80 }
81 0 $dbh->do("CREATE SCHEMA $name");
82 0 return { name => $name, created => 1, extra => \@extra };
83 }
84  
85  
86 0 ▶sub conformance_install {
Subroutine conformance_install: not called CC=1 SCAR=6.9
Pod documented
87 0 my ($class, $schema, $case) = @_;
88 0 $schema->deploy;
89 0 return;
90 }
91  
92  
93 0 ▶sub conformance_cleanup {
Subroutine conformance_cleanup: not called CC=9 SCAR=45.0
Pod documented
94 0 my ($class, $schema, $case, $namespace) = @_;
95 0 delete $NAMESPACE{ ref $schema };
96 0 eval { $schema->storage->disconnect; 1 };
97 0 ▶ return unless ref $namespace eq 'HASH' and $namespace->{created};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $namespace eq "HASH" and $namespace->{"created"} 0 0 0
Condition0%0/3
ref $namespace eq "HASH" and $namespace->{"created"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $namespace eq "HASH" and $namespace->{"created"}
ref $namespace eq "HASH" $namespace->{"created"}
Truth table0%0/3
A and B
A= ref $namespace eq "HASH"B= $namespace->{"created"}
ABresult
0X0
100
111
98 0 my $name = $namespace->{name};
99  
100 my $dbh = DBI->connect(
101 $ENV{DBIO_TEST_DB2_DSN}, $ENV{DBIO_TEST_DB2_USER}, $ENV{DBIO_TEST_DB2_PASS},
102 0 ▶ { AutoCommit => 1, RaiseError => 1, PrintError => 0, PrintWarn => 0 })
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect($ENV{"DBIO_TEST_DB2_DSN"}, $ENV{"DBIO_TEST_DB2_USER"}, $ENV{"DBIO_TEST_DB2_PASS"}, {"AutoCommit", 1, "RaiseError", 1, "PrintError", 0, "PrintWarn", 0}) or "DBIO::Exception"->throw("conformance: cleanup of schema $name could not connect") 0 0 0
103 or DBIO::Exception->throw("conformance: cleanup of schema $name could not connect");
104 0 ▶ my $quote = sub { (my $q = $_[0]) =~ s/"/""/g; qq{"$q"} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 my $ok = eval {
106 0 ▶ for my $schema_name ($name, @{ $namespace->{extra} || [] }) {
Condition0%0/2
$namespace->{'extra'} || []
l!l
00
MC/DC0%0/1
$namespace->{'extra'} || []
$namespace->{'extra'}
Truth table0%0/2
A
A= $namespace->{'extra'}
Aresult
11
00
107 0 for my $kind (['V', 'VIEW'], ['T', 'TABLE']) {
108 0 my $names = $dbh->selectcol_arrayref(
109 'SELECT tabname FROM syscat.tables WHERE tabschema = ? AND type = ?',
110 undef, $schema_name, $kind->[0]);
111 0 $dbh->do("DROP $kind->[1] " . $quote->($schema_name) . '.' . $quote->($_)) for @$names;
112 }
113 0 $dbh->do('DROP SCHEMA ' . $quote->($schema_name) . ' RESTRICT');
114 }
115 0 1;
116 };
117 0 my $err = $@;
118 0 $dbh->disconnect;
119 0 ▶ DBIO::Exception->throw("conformance: cleanup of schema $name failed: $err")
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
120 unless $ok;
121 0 return;
122 }
123  
124  
125 0 ▶sub conformance_diff_is_empty {
Subroutine conformance_diff_is_empty: not called CC=2 SCAR=17.9
Pod documented
126 0 my ($class, $schema, $case) = @_;
127 0 my $diff = DBIO::DB2::Deploy->new(schema => $schema)->diff;
128 0 ▶ return $diff->has_changes ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$diff->has_changes ? : 0 0 0
129 }
130  
131  
132 0 ▶sub conformance_upgrade {
Subroutine conformance_upgrade: not called CC=1 SCAR=6.9
Pod documented
133 0 my ($class, $schema, $to_class, $case) = @_;
134 0 my $to = $class->_connect_in($to_class, $class->_namespace_of($schema));
135 0 DBIO::DB2::Deploy->new(schema => $to)->upgrade(allow_destructive => 1);
136 0 return $to;
137 }
138  
139  
140 0 ▶sub conformance_deploy {
Subroutine conformance_deploy: not called CC=1 SCAR=6.9
Pod documented
141 0 my ($class, $schema, $to_class, $case) = @_;
142 0 my $to = $class->_connect_in($to_class, $class->_namespace_of($schema));
143 0 return DBIO::DB2::Deploy->new(schema => $to);
144 }
145  
146  
147 9 ▶sub conformance_answered_through { { DDL => 2, Diff => 4, Bind => 1 } }
Subroutine conformance_answered_through: called CC=1 SCAR=0.0
Pod documented
148  
149  
150 3 ▶sub conformance_refusals {
Subroutine conformance_refusals: called CC=1 SCAR=0.0
Pod documented
151 return {
152 3 'ddl-enum-value-apostrophe' => {
153 kind => 'server',
154 reason => qr/no enum type.*it is refused/s,
155 },
156 'ddl-type-size-bit' => {
157 kind => 'server',
158 reason => qr/no bit type.*it is refused/s,
159 },
160 'ddl-default-digit-string' => {
161 kind => 'deviation',
162 fact => qr/expected '007', read back '007 +'/,
163 reason => 'a CHAR(n) column pads the value with blanks to its length '
164 . '(SQL Reference, CHAR data type); the default is written as the '
165 . 'string it is',
166 },
167  
168 # Bind list
169 'bind-long-string' => {
170 kind => 'server',
171 reason => qr/column "v" is compared with a value of \d+ bytes, and Db2 cannot compare a CLOB with more than 32672 bytes/,
172 },
173 'bind-lob-empty-and-large' => {
174 kind => 'server',
175 reason => qr/column "b" gets an empty string, which DBD::DB2 reads back as NULL from a BLOB column/,
176 },
177  
178 # Diff list
179 'diff-column-added-not-null-populated' => {
180 kind => 'server',
181 reason => qr/column "code" to table "base": it is NOT NULL without a default/,
182 },
183 'diff-not-null-over-nulls' => {
184 kind => 'server',
185 reason => qr/column "c" NOT NULL: table "opt" holds rows where it is NULL/,
186 },
187 };
188 }
189  
190  
191 0 ▶sub conformance_fact {
Subroutine conformance_fact: not called CC=20 SCAR=60.4
Pod documented
192 0 my ($class, $schema, $name, $args) = @_;
193 0 my $dbh = $schema->storage->dbh;
194 0 my $ns = $class->_namespace_of($schema);
195 0 my $table = $args->{table};
196 0 ▶ my @cols = @{ $args->{columns} || [] };
Condition0%0/2
$args->{'columns'} || []
l!l
00
MC/DC0%0/1
$args->{'columns'} || []
$args->{'columns'}
Truth table0%0/2
A
A= $args->{'columns'}
Aresult
11
00
197 0 ▶ my $same = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
198 0 my ($got) = @_;
199 0 ▶ return 0 unless @$got == @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @$got == @cols 0 0 0
200 0 ▶ return (join("\0", map { $_ // '' } @$got) eq join("\0", @cols)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
join("\000", map({$_ // '';} @$got)) eq join("\000", @cols) ? : 0 0 0
Condition0%0/2
$_ // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
201 0 };
202  
203 0 ▶ if ($name eq 'table_in_namespace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "table_in_namespace") 0 0 0
204 my ($n) = $dbh->selectrow_array(
205 'SELECT COUNT(*) FROM syscat.tables WHERE tabschema = ? AND tabname = ?',
206 0 undef, $args->{namespace}, $args->{unqualified});
207 0 ▶ return $n ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n ? : 0 0 0
208 }
209  
210 0 ▶ if ($name eq 'index_count') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "index_count") 0 0 0
211 0 my $rows = $dbh->selectall_arrayref(
212 q{SELECT indschema, indname FROM syscat.indexes
213 WHERE tabschema = ? AND tabname = ? AND uniquerule <> 'P'},
214 undef, $ns, $table);
215 0 my $n = 0;
216 0 for my $r (@$rows) {
217 0 my $got = $dbh->selectcol_arrayref(
218 q{SELECT colname FROM syscat.indexcoluse
219 WHERE indschema = ? AND indname = ? AND colorder <> 'I'
220 ORDER BY colseq}, undef, @$r);
221 0 ▶ $n++ if $same->($got);
Branch: 1 branch
BranchTrueFalseTotal
if &$same($got) 0 0 0
222 }
223 0 return $n;
224 }
225  
226 0 my $intro = DBIO::DB2::Introspect->new(dbh => $dbh, schema => $ns);
227 0 my $model = $intro->model;
228 0 my $entry = $model->{tables}{$table};
229  
230 0 ▶ if ($name eq 'object_kind') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "object_kind") 0 0 0
231 0 ▶ return $entry ? $entry->{kind} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$entry ? : 0 0 0
232 }
233 0 ▶ if ($name eq 'table_name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "table_name") 0 0 0
234 0 ▶ return $entry ? $table : undef;
Branch: 1 branch
BranchTrueFalseTotal
$entry ? : 0 0 0
235 }
236 0 ▶ if ($name eq 'column_names') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "column_names") 0 0 0
237 0 ▶ return $entry ? $intro->table_columns($table) : [];
Branch: 1 branch
BranchTrueFalseTotal
$entry ? : 0 0 0
238 }
239 0 ▶ if ($name eq 'column_size') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "column_size") 0 0 0
240 0 return $intro->table_columns_info($table)->{ $args->{column} }{size};
241 }
242 0 ▶ if ($name eq 'column_auto_increment') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "column_auto_increment") 0 0 0
243 0 ▶ return $intro->table_columns_info($table)->{ $args->{column} }{is_auto_increment} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$intro->table_columns_info($table)->{$args->{'column'}}{'is_auto_increment'} ? : 0 0 0
244 }
245 0 ▶ if ($name eq 'primary_key_columns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "primary_key_columns") 0 0 0
246 0 return $intro->table_pk_info($table);
247 }
248 0 ▶ if ($name eq 'unique_exists') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "unique_exists") 0 0 0
249 0 ▶ return (grep { $same->($_->[1]) } @{ $intro->table_uniq_info($table) }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({&$same($_->[1]);} @{$intro->table_uniq_info($table);}) ? : 0 0 0
250 }
251 0 ▶ if ($name eq 'foreign_key_count') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "foreign_key_count") 0 0 0
252 0 return scalar @{ $intro->table_fk_info($table) };
253 }
254  
255 0 DBIO::Exception->throw("unknown conformance fact '$name'");
256 }
257  
258 5 1;
259  
260 __END__
↑ Summary DB2/Introspect/ForeignKeys.pm »