MSSQL/Test/Conformance.pm

filter: Statementstmt 24.5% 40 / 163 Branchbran 5.8% 4 / 68 Conditioncond 14.2% 5 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 44.4% 12 / 27 Podpod 100% 12 / 12 totaltotal 22.5% 73 / 324 CC 49 SCAR 54.0
CC
49
Cov
18%
conformance_fact
650.0
conformance_cleanup
20.0
__ANON__
20.0
CRAP 222.3
↑ Summary MSSQL/Introspect.pm »
1 package DBIO::MSSQL::Test::Conformance;
2 # ABSTRACT: Answers the shared conformance case table for the SQL Server driver
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = '0.900004';
8  
9 8 ▶use base 'DBIO::Test::Conformance';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use DBIO::MSSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use DBIO::MSSQL::Deploy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 8 my $SEQ = 0;
18 # schema class => name of the database of its case
19 8 my %DATABASE;
20  
21 # the connection details of the access path in use: [ dsn, user, pass ]
22 7 ▶sub _server {
Subroutine _server: called CC=7 SCAR=26.4
23 7 my @odbc = @ENV{qw(DBIO_TEST_MSSQL_ODBC_DSN DBIO_TEST_MSSQL_ODBC_USER DBIO_TEST_MSSQL_ODBC_PASS)};
24 7 my @sybase = @ENV{qw(DBIO_TEST_MSSQL_DSN DBIO_TEST_MSSQL_USER DBIO_TEST_MSSQL_PASS)};
25 0 ▶ return \@odbc if length($odbc[0] // '') and eval { require DBD::ODBC; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if length($odbc[0] // "") and eval { do { require DBD::ODBC; 1 } } 0 7 7
Condition40%2/5
length($odbc[0] // "") and eval { do { require DBD::ODBC; 1 } }
!ll&&!rl&&r
700
$odbc[0] // ""
l!l
07
MC/DC0%0/2
length($odbc[0] // "") and eval { do { require DBD::ODBC; 1 } }
$odbc[0] eval { do { require DBD::ODBC; 1 } }
Truth table33%1/3
A and B
A= $odbc[0]B= eval { do { require DBD::ODBC; 1 } }
ABresult
0X0
100
111
26 0 ▶ return \@sybase if length($sybase[0] // '') and eval { require DBD::Sybase; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if length($sybase[0] // "") and eval { do { require DBD::Sybase; 1 } } 0 7 7
Condition40%2/5
length($sybase[0] // "") and eval { do { require DBD::Sybase; 1 } }
!ll&&!rl&&r
700
$sybase[0] // ""
l!l
07
MC/DC0%0/2
length($sybase[0] // "") and eval { do { require DBD::Sybase; 1 } }
$sybase[0] eval { do { require DBD::Sybase; 1 } }
Truth table33%1/3
A and B
A= $sybase[0]B= eval { do { require DBD::Sybase; 1 } }
ABresult
0X0
100
111
27 7 return undef;
28 }
29  
30 # the DSN with its database swapped, in the spelling of the DBD (as
31 # DBIO::MSSQL::Deploy::_temp_dsn does)
32 0 ▶sub _dsn_for {
Subroutine _dsn_for: not called CC=3 SCAR=24.8
33 0 my ($class, $dsn, $database) = @_;
34 0 ▶ return $dsn if $dsn =~ s/\b((?:Database|dbname)=)[^;]*/$1$database/i;
Branch: 1 branch
BranchTrueFalseTotal
if $dsn =~ s/\b((?:Database|dbname)=)[^;]*/$1$database/i 0 0 0
35 0 ▶ my $key = $dsn =~ /^dbi:Sybase:/i ? 'database' : 'Database';
Branch: 1 branch
BranchTrueFalseTotal
$dsn =~ /^dbi:Sybase:/i ? : 0 0 0
36 0 $dsn =~ s/;\z//;
37 0 return "$dsn;$key=$database";
38 }
39  
40 0 ▶sub _admin_dbh {
Subroutine _admin_dbh: not called CC=1 SCAR=6.9
41 0 my ($class) = @_;
42 0 my ($dsn, $user, $pass) = @{ $class->_server };
43 0 return DBI->connect($dsn, $user, $pass,
44 { RaiseError => 1, PrintError => 0, PrintWarn => 0, AutoCommit => 1 });
45 }
46  
47  
48 4 ▶sub conformance_live_available { $_[0]->_server ? 1 : 0 }
Subroutine conformance_live_available: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_server ? : 0 4 4
49  
50  
51 0 ▶sub conformance_connect {
Subroutine conformance_connect: not called CC=2 SCAR=17.9
Pod documented
52 0 my ($class, $schema_class) = @_;
53 0 ▶ my $server = $class->_server
Branch: 1 branch
BranchTrueFalseTotal
my $server = $class->_server or "DBIO::Exception"->throw("conformance: no SQL Server configured") 0 0 0
54 or DBIO::Exception->throw('conformance: no SQL Server configured');
55 0 my $name = $class->_database_name;
56 0 $DATABASE{$schema_class} = $name;
57  
58 0 $schema_class->load_components('MSSQL');
59 0 my ($dsn, $user, $pass) = @$server;
60 0 return $schema_class->connect($class->_dsn_for($dsn, $name), $user, $pass,
61 { AutoCommit => 1, RaiseError => 1, PrintError => 0, PrintWarn => 0,
62 $class->_long_read_len($dsn) });
63 }
64  
65 # Over DBD::Sybase a value longer than the TEXTSIZE of the session (LongReadLen,
66 # 32768 unless the connect info says otherwise) is refused when it is read, not
67 # cut (karr k47); the Bind list writes and reads 100000 characters, so the
68 # connection names a LongReadLen for them, as the message of that refusal says.
69 # DBD::Sybase also reads a datetime as 'Oct 25 2026 04:02PM' unless the
70 # connection asks for datetime_setup (ISO). DBD::ODBC has 1 GiB by default and
71 # reads ISO; it is left alone.
72 0 ▶sub _long_read_len {
Subroutine _long_read_len: not called CC=2 SCAR=17.9
73 0 my ($class, $dsn) = @_;
74 0 ▶ return $dsn =~ /^dbi:Sybase:/i
Branch: 1 branch
BranchTrueFalseTotal
$dsn =~ /^dbi:Sybase:/i ? : 0 0 0
75 ? (LongReadLen => 1_000_000, on_connect_call => 'datetime_setup')
76 : ();
77 }
78  
79 # The name of the next case's database. A hook for the test of the refusal
80 # (t/89): a subclass answers a name that is taken already.
81 0 ▶sub _database_name { sprintf 'dbio_cf_%d_%d', $$, ++$SEQ }
Subroutine _database_name: not called CC=1 SCAR=6.9
82  
83 0 ▶sub _database_of {
Subroutine _database_of: not called CC=2 SCAR=17.9
84 0 my ($class, $schema) = @_;
85 0 my $name = $DATABASE{ ref $schema };
86 0 ▶ DBIO::Exception->throw('conformance: this schema was not connected through '
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
87 . 'conformance_connect') unless defined $name;
88 0 return $name;
89 }
90  
91  
92 11 ▶sub conformance_quote_ident {
Subroutine conformance_quote_ident: called CC=1 SCAR=0.0
Pod documented
93 11 my ($class, $schema, $name) = @_;
94 11 (my $inner = $name) =~ s/\]/]]/g;
95 11 return "[$inner]";
96 }
97  
98  
99 0 ▶sub conformance_namespace {
Subroutine conformance_namespace: not called CC=3 SCAR=24.8
Pod documented
100 0 my ($class, $schema, $case) = @_;
101 0 my $name = $class->_database_of($schema);
102 0 my $admin = $class->_admin_dbh;
103 0 my $ok = eval {
104 0 my ($id) = $admin->selectrow_array('SELECT DB_ID(?)', undef, $name);
105 0 ▶ DBIO::Exception->throw("conformance: the database $name exists already; "
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 0 0
106 . 'refusing to use or drop a database the case did not create')
107 if defined $id;
108 0 $admin->do("CREATE DATABASE [$name]");
109 0 1;
110 };
111 0 my $err = $@;
112 0 $admin->disconnect;
113 0 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
114 0 return { name => $name, created => 1 };
115 }
116  
117  
118 0 ▶sub conformance_install {
Subroutine conformance_install: not called CC=1 SCAR=6.9
Pod documented
119 0 my ($class, $schema, $case) = @_;
120 0 $schema->deploy;
121 0 return;
122 }
123  
124  
125 0 ▶sub conformance_cleanup {
Subroutine conformance_cleanup: not called CC=4 SCAR=30.0
Pod documented
126 0 my ($class, $schema, $case, $namespace) = @_;
127 0 delete $DATABASE{ ref $schema };
128 0 eval { $schema->storage->disconnect; 1 };
129 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
130 0 my $name = $namespace->{name};
131  
132 0 my $admin = $class->_admin_dbh;
133 0 my $ok = eval {
134 0 $admin->do("IF DB_ID('$name') IS NOT NULL BEGIN "
135 . "ALTER DATABASE [$name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; "
136 . "DROP DATABASE [$name] END");
137 0 1;
138 };
139 0 my $err = $@;
140 0 $admin->disconnect;
141 0 ▶ DBIO::Exception->throw("conformance: cleanup of database $name failed: $err")
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
142 unless $ok;
143 0 return;
144 }
145  
146  
147 0 ▶sub conformance_diff_is_empty {
Subroutine conformance_diff_is_empty: not called CC=2 SCAR=17.9
Pod documented
148 0 my ($class, $schema, $case) = @_;
149 0 my $diff = DBIO::MSSQL::Deploy->new(schema => $schema)->diff;
150 0 ▶ return $diff->has_changes ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$diff->has_changes ? : 0 0 0
151 }
152  
153  
154 0 ▶sub conformance_upgrade {
Subroutine conformance_upgrade: not called CC=1 SCAR=6.9
Pod documented
155 0 my ($class, $schema, $to_class, $case) = @_;
156 0 my $to = $class->_connect_target($schema, $to_class);
157 0 DBIO::MSSQL::Deploy->new(schema => $to)->upgrade(allow_destructive => 1);
158 0 return $to;
159 }
160  
161  
162 0 ▶sub conformance_deploy {
Subroutine conformance_deploy: not called CC=1 SCAR=6.9
Pod documented
163 0 my ($class, $schema, $to_class, $case) = @_;
164 0 my $to = $class->_connect_target($schema, $to_class);
165 0 return DBIO::MSSQL::Deploy->new(schema => $to);
166 }
167  
168 # The target class, connected to the database of the installed schema.
169 0 ▶sub _connect_target {
Subroutine _connect_target: not called CC=2 SCAR=17.9
170 0 my ($class, $schema, $to_class) = @_;
171 0 ▶ my $server = $class->_server
Branch: 1 branch
BranchTrueFalseTotal
my $server = $class->_server or "DBIO::Exception"->throw("conformance: no SQL Server configured") 0 0 0
172 or DBIO::Exception->throw('conformance: no SQL Server configured');
173 0 my $name = $class->_database_of($schema);
174 0 $to_class->load_components('MSSQL');
175 0 $DATABASE{$to_class} = $name;
176 0 my ($dsn, $user, $pass) = @$server;
177 0 return $to_class->connect($class->_dsn_for($dsn, $name), $user, $pass,
178 { AutoCommit => 1, RaiseError => 1, PrintError => 0, PrintWarn => 0,
179 $class->_long_read_len($dsn) });
180 }
181  
182  
183 6 ▶sub conformance_answered_through { { DDL => 2, Diff => 4, Bind => 1 } }
Subroutine conformance_answered_through: called CC=1 SCAR=0.0
Pod documented
184  
185  
186 3 ▶sub conformance_refusals {
Subroutine conformance_refusals: called CC=3 SCAR=12.3
Pod documented
187 3 my ($class) = @_;
188 3 my $refusals = {
189 'ddl-enum-value-apostrophe' => {
190 kind => 'server',
191 reason => qr/type 'enum'.*is refused: SQL Server has no enum type/s,
192 },
193 'ddl-type-size-bit' => {
194 kind => 'server',
195 reason => qr/size '8' on type 'bit'.*is refused: bit has no length in SQL Server/s,
196 },
197 'ddl-default-digit-string' => {
198 kind => 'deviation',
199 fact => qr/expected '007', read back '007 +'/,
200 reason => 'a char(n) column pads the value with blanks to its length',
201 },
202  
203 # Diff list (karr k81)
204 'diff-column-added-not-null-populated' => {
205 kind => 'server',
206 reason => qr/refuses to add the column base\.code: it is NOT NULL without a default/,
207 },
208 'diff-not-null-over-nulls' => {
209 kind => 'server',
210 reason => qr/refuses to make the column opt\.c NOT NULL: table opt holds rows where it is NULL/,
211 },
212 # Bind list (karr k81)
213 'bind-empty-string' => {
214 kind => 'deviation',
215 fact => qr/the server accepted a row it must reject/,
216 reason => "SQL Server converts the empty string to 0 for an integer column "
217 . "(Transact-SQL data type conversion: '' to int is 0), so n => '' is "
218 . 'stored as 0, not rejected; the empty string itself round-trips '
219 . 'exactly',
220 },
221  
222 };
223  
224 # Over DBD::Sybase a bound string beyond ASCII is refused before it is sent
225 # (karr k49): FreeTDS would send it as a fixed-length CHAR and the server pad
226 # it. The case is a refusal there, a deviation over DBD::ODBC.
227 3 my $server = $class->_server;
228 3 ▶ if ($server and $server->[0] =~ /^dbi:Sybase:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server and $server->[0] =~ /^dbi:Sybase:/i) 0 3 3
Condition33%1/3
$server and $server->[0] =~ /^dbi:Sybase:/i
!ll&&!rl&&r
300
MC/DC0%0/2
$server and $server->[0] =~ /^dbi:Sybase:/i
$server $server->[0] =~ /^dbi:Sybase:/i
Truth table33%1/3
A and B
A= $serverB= $server->[0] =~ /^dbi:Sybase:/i
ABresult
0X0
100
111
229 0 $refusals->{'bind-non-ascii'} = {
230 kind => 'server',
231 reason => qr/has a character beyond ASCII.*Connect through DBD::ODBC/s,
232 };
233 }
234 3 return $refusals;
235 }
236  
237  
238 0 ▶sub conformance_fact {
Subroutine conformance_fact: not called CC=25 SCAR=64.8
Pod documented
239 0 my ($class, $schema, $name, $args) = @_;
240 0 my $dbh = $schema->storage->dbh;
241  
242 0 ▶ if ($name eq 'table_in_namespace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "table_in_namespace") 0 0 0
243 my ($n) = $dbh->selectrow_array(
244 'SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES '
245 . 'WHERE table_schema = ? AND table_name = ?',
246 0 undef, $args->{namespace}, $args->{unqualified});
247 0 ▶ return $n ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n ? : 0 0 0
248 }
249  
250 # a table "schema.table" is read from that schema (karr k72)
251 0 my $table = $args->{table};
252 0 ▶ my @schemas = defined $table && $table =~ /\A([^.]+)\./ ? ($1) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $table && $table =~ /\A([^.]+)\./ ? : 0 0 0
Condition0%0/3
defined $table and $table =~ /\A([^.]+)\./
!ll&&!rl&&r
000
MC/DC0%0/2
defined $table and $table =~ /\A([^.]+)\./
defined $table $table =~ /\A([^.]+)\./
Truth table0%0/3
A and B
A= defined $tableB= $table =~ /\A([^.]+)\./
ABresult
0X0
100
111
253 0 my $model = DBIO::MSSQL::Introspect->new(dbh => $dbh, schemas => \@schemas)->model;
254 0 my $entry = $model->{tables}{$table};
255 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
256 0 ▶ my $same = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
257 0 my ($got) = @_;
258 0 ▶ return 0 unless @$got == @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @$got == @cols 0 0 0
259 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
260 0 };
261  
262 0 ▶ if ($name eq 'object_kind') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "object_kind") 0 0 0
263 0 ▶ return $entry ? $entry->{kind} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$entry ? : 0 0 0
264 }
265 0 ▶ if ($name eq 'table_name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "table_name") 0 0 0
266 0 ▶ return $entry ? $entry->{table_name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$entry ? : 0 0 0
267 }
268  
269 0 ▶ my $columns = $model->{columns}{$table} || [];
Condition0%0/2
$model->{'columns'}{$table} || []
l!l
00
MC/DC0%0/1
$model->{'columns'}{$table} || []
$model->{'columns'}{$table}
Truth table0%0/2
A
A= $model->{'columns'}{$table}
Aresult
11
00
270 0 ▶ if ($name eq 'column_names') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "column_names") 0 0 0
271 0 return [ map { $_->{column_name} } @$columns ];
272 }
273 0 ▶ if ($name eq 'column_size') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "column_size") 0 0 0
274 0 my ($col) = grep { $_->{column_name} eq $args->{column} } @$columns;
275 0 ▶ return $col ? $col->{size} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$col ? : 0 0 0
276 }
277 0 ▶ if ($name eq 'column_auto_increment') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "column_auto_increment") 0 0 0
278 0 my ($col) = grep { $_->{column_name} eq $args->{column} } @$columns;
279 0 ▶ return $col && $col->{is_identity} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$col && $col->{'is_identity'} ? : 0 0 0
Condition0%0/3
$col and $col->{"is_identity"}
!ll&&!rl&&r
000
MC/DC0%0/2
$col and $col->{"is_identity"}
$col $col->{"is_identity"}
Truth table0%0/3
A and B
A= $colB= $col->{"is_identity"}
ABresult
0X0
100
111
280 }
281 0 ▶ if ($name eq 'primary_key_columns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "primary_key_columns") 0 0 0
282 0 return [ map { $_->{column_name} }
283 0 sort { $a->{pk_position} <=> $b->{pk_position} }
284 0 grep { $_->{is_pk} } @$columns ];
285 }
286  
287 0 ▶ my $indexes = $model->{indexes}{$table} || {};
Condition0%0/2
$model->{'indexes'}{$table} || {}
l!l
00
MC/DC0%0/1
$model->{'indexes'}{$table} || {}
$model->{'indexes'}{$table}
Truth table0%0/2
A
A= $model->{'indexes'}{$table}
Aresult
11
00
288 0 ▶ if ($name eq 'unique_exists') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "unique_exists") 0 0 0
289 0 ▶ return (grep { $_->{is_unique} and $same->($_->{columns}) } values %$indexes)
Branch: 1 branch
BranchTrueFalseTotal
grep({$_->{'is_unique'} and &$same($_->{'columns'});} values %$indexes) ? : 0 0 0
Condition0%0/3
$_->{'is_unique'} && &$same($_->{'columns'})
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'is_unique'} && &$same($_->{'columns'})
$_->{'is_unique'} &$same($_->{'columns'})
Truth table0%0/3
A && B
A= $_->{'is_unique'}B= &$same($_->{'columns'})
ABresult
0X0
100
111
290 ? 1 : 0;
291 }
292 0 ▶ if ($name eq 'foreign_key_count') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "foreign_key_count") 0 0 0
293 0 ▶ return scalar @{ $model->{foreign_keys}{$table} || [] };
Condition0%0/2
$model->{'foreign_keys'}{$table} || []
l!l
00
MC/DC0%0/1
$model->{'foreign_keys'}{$table} || []
$model->{'foreign_keys'}{$table}
Truth table0%0/2
A
A= $model->{'foreign_keys'}{$table}
Aresult
11
00
294 }
295 0 ▶ if ($name eq 'index_count') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "index_count") 0 0 0
296 0 return scalar grep { $same->($_->{columns}) } values %$indexes;
297 }
298  
299 0 DBIO::Exception->throw("unknown conformance fact '$name'");
300 }
301  
302 8 1;
303  
304 __END__
↑ Summary MSSQL/Introspect.pm »