DuckDB/Test.pm

filter: Statementstmt 82.5% 71 / 86 Branchbran 66.6% 16 / 24 Conditioncond 65.0% 13 / 20 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 76.7% 119 / 155 CC 28 SCAR 20.2
CC
28
Cov
77%
import
19.1
_without_unsupported_fk_actions
8.0
__ANON__
7.0
CRAP 7.5
« DuckDB/Util.pm ↑ Summary DuckDB/Diff/Rebuild.pm »
1 package DBIO::DuckDB::Test;
2 # ABSTRACT: DuckDB-specific test utilities for DBIO
3  
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 our $VERSION = '0.900004';
8  
9 28 ▶use DBIO::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use DBIO::Test::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 28 ▶sub import {
Subroutine import: called CC=6 SCAR=29.5
15 28 my $self = shift;
16 28 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 28 28
17 0 my $caller = caller;
18 0 for my $exp (@_) {
19 0 ▶ if ($exp eq ':DiffSQL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp eq ':DiffSQL') { } 0 0 0
elsif ($exp eq ':GlobalLock') { } 0 0 0
20 0 require DBIO::SQLMaker;
21 0 require SQL::Abstract::Test;
22 0 for (qw(is_same_sql_bind is_same_sql is_same_bind)) {
23 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 0 *{"${caller}::$_"} = \&{"SQL::Abstract::Test::$_"};
25 }
26 }
27 elsif ($exp eq ':GlobalLock') {
28 # no-op, parallel to DBIO::SQLite::Test
29 }
30 else {
31 0 croak "Unknown export $exp requested from $self";
32 }
33 }
34 }
35 }
36  
37  
38 # Arguments of init_schema that steer the helper itself. They are not
39 # connect attributes and must never reach connect(): the storage rejects a
40 # connect attribute it does not know (karr k40).
41 28 my @HELPER_ARGS = qw(
42 dsn connect_info user pass connect_opts
43 no_deploy no_populate no_connect deploy_args
44 storage_type replicated replicant_connect_info compose_connection
45 );
46  
47 22 ▶sub _database {
Subroutine _database: called CC=3 SCAR=13.5
48 22 my $self = shift;
49 22 my %args = @_;
50  
51 22 delete @args{@HELPER_ARGS};
52  
53 22 ▶ if ($ENV{DBIO_TEST_DUCKDB_DSN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DBIO_TEST_DUCKDB_DSN"}) 0 22 22
54 return (
55 0 ▶ (map { $ENV{"DBIO_TEST_DUCKDB_${_}"} // '' } qw/DSN DBUSER DBPASS/),
Condition0%0/2
$ENV{"DBIO_TEST_DUCKDB_$_"} // ""
l!l
00
MC/DC0%0/1
$ENV{"DBIO_TEST_DUCKDB_$_"} // ""
$ENV{"DBIO_TEST_DUCKDB_$_"}
Truth table0%0/2
A
A= $ENV{"DBIO_TEST_DUCKDB_$_"}
Aresult
11
00
56 { AutoCommit => 1, RaiseError => 1, PrintError => 0, %args },
57 );
58 }
59  
60 return (
61 22 'dbi:DuckDB:dbname=:memory:',
62 '', '',
63 { AutoCommit => 1, RaiseError => 1, PrintError => 0, %args },
64 );
65 }
66  
67  
68 22 ▶sub init_schema {
Subroutine init_schema: called CC=3 SCAR=11.8
Pod documented
69 22 my $self = shift;
70 22 my %args = @_;
71  
72 22 my ($dsn, $user, $pass, $opts) = $self->_database(%args);
73  
74 22 my $schema = DBIO::Test->init_schema(
75 dsn => $dsn,
76 user => $user,
77 pass => $pass,
78 connect_opts => $opts,
79 storage_type => '+DBIO::DuckDB::Storage',
80 %args,
81 no_deploy => 1,
82 );
83 22 $self->_without_unsupported_fk_actions($schema);
84 22 ▶ return $schema if $args{no_deploy};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"no_deploy"} 3 19 22
85  
86 19 DBIO::Test->deploy_schema($schema, $args{deploy_args});
87 0 ▶ DBIO::Test->populate_schema($schema) unless $args{no_populate};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_populate"} 0 0 0
88 0 return $schema;
89 }
90  
91  
92 22 ▶sub _without_unsupported_fk_actions {
Subroutine _without_unsupported_fk_actions: called CC=8 SCAR=20.9
93 22 my ($self, $schema) = @_;
94 22 for my $name ($schema->sources) {
95 990 my $source = $schema->source($name);
96 990 my %rels;
97 990 for my $rel ($source->relationships) {
98 3014 my $info = $source->relationship_info($rel);
99 3014 ▶ my %attrs = %{ $info->{attrs} || {} };
Condition50%1/2
$info->{'attrs'} || {}
l!l
30140
MC/DC0%0/1
$info->{'attrs'} || {}
$info->{'attrs'}
Truth table50%1/2
A
A= $info->{'attrs'}
Aresult
11
00
100 3014 for my $event (qw(on_delete on_update)) {
101 delete $attrs{$event}
102 6028 ▶ if defined $attrs{$event} && $attrs{$event} =~ /\A\s*(?:CASCADE|SET\s+NULL|SET\s+DEFAULT)\s*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs{$event} and $attrs{$event} =~ /\A\s*(?:CASCADE|SET\s+NULL|SET\s+DEFAULT)\s*\z/i 220 5808 6028
Condition100%3/3
defined $attrs{$event} and $attrs{$event} =~ /\A\s*(?:CASCADE|SET\s+NULL|SET\s+DEFAULT)\s*\z/i
!ll&&!rl&&r
578622220
MC/DC100%2/2
defined $attrs{$event} and $attrs{$event} =~ /\A\s*(?:CASCADE|SET\s+NULL|SET\s+DEFAULT)\s*\z/i
defined $attrs{$event} $attrs{$event} =~ /\A\s*(?:CASCADE|SET\s+NULL|SET\s+DEFAULT)\s*\z/i
Truth table100%3/3
A and B
A= defined $attrs{$event}B= $attrs{$event} =~ /\A\s*(?:CASCADE|SET\s+NULL|SET\s+DEFAULT)\s*\z/i
ABresult
0X0
100
111
103 }
104 3014 $rels{$rel} = { %$info, attrs => \%attrs };
105 }
106 990 $source->_relationships(\%rels);
107 }
108  
109 # Two tables that point at each other cannot both be created with their
110 # key (DuckDB adds no key to a table that exists): the key that closes
111 # the circle is taken off, as the driver asks (DBIO::DuckDB::DDL).
112 22 my (%state, %target);
113 3828 ▶ my $target_of = sub {
Subroutine __ANON__: called CC=6 SCAR=18.7
114 3828 my ($source, $rel) = @_;
115 3828 my $info = $source->relationship_info($rel);
116 3828 ▶ return unless $info && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"}{"is_foreign_key_constraint"} 2508 1320 3828
Condition66%2/3
$info and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
025081320
MC/DC50%1/2
$info and $info->{"attrs"}{"is_foreign_key_constraint"}
$info $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table66%2/3
A and B
A= $infoB= $info->{"attrs"}{"is_foreign_key_constraint"}
ABresult
0X0
100
111
117 1320 my $class = $info->{class};
118 0 ▶ my $fs = eval { $schema->source($class) } // eval { $schema->source($class =~ s/.*:://r) };
Condition33%1/3
eval { do { $schema->source($class) } } // eval { do { $schema->source($class =~ s/.*:://r) } }
l!l&&r!l&&!r
132000
MC/DC0%0/2
eval { do { $schema->source($class) } } // eval { do { $schema->source($class =~ s/.*:://r) } }
eval { do { $schema->source($class) } } eval { do { $schema->source($class =~ s/.*:://r) } }
Truth table33%1/3
A // B
A= eval { do { $schema->source($class) } }B= eval { do { $schema->source($class =~ s/.*:://r) } }
ABresult
1X1
011
000
119 1320 ▶ return ($fs && !$fs->isa('DBIO::ResultSource::View')) ? $fs->source_name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$fs && !$fs->isa('DBIO::ResultSource::View') ? : 1232 88 1320
Condition66%2/3
$fs and not $fs->isa("DBIO::ResultSource::View")
!ll&&!rl&&r
0881232
MC/DC50%1/2
$fs and not $fs->isa("DBIO::ResultSource::View")
$fs not $fs->isa("DBIO::ResultSource::View")
Truth table66%2/3
A and not(B)
A= $fsB= not $fs->isa("DBIO::ResultSource::View")
ABresult
0X0
100
111
120 22 };
121 22 ▶ my $visit; $visit = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
122 1210 my ($name) = @_;
123 1210 $state{$name} = 1;
124 1210 my $source = $schema->source($name);
125 1210 for my $rel (sort $source->relationships) {
126 3828 ▶ my $to = $target_of->($source, $rel) // next;
Condition100%2/2
&$target_of($source, $rel) // (next)
l!l
12322596
MC/DC100%1/1
&$target_of($source, $rel) // (next)
&$target_of($source, $rel)
Truth table100%2/2
A
A= &$target_of($source, $rel)
Aresult
11
00
127 1232 ▶ next if $to eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $to eq $name 44 1188 1232
128 1188 ▶ if (($state{$to} // 0) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($state{$to} // 0) == 1) 66 1122 1188
Condition100%2/2
$state{$to} // 0
l!l
968220
MC/DC100%1/1
$state{$to} // 0
$state{$to}
Truth table100%2/2
A
A= $state{$to}
Aresult
11
00
129 66 $source->relationship_info($rel)->{attrs}{is_foreign_key_constraint} = 0;
130 66 next;
131 }
132 1122 ▶ $visit->($to) unless $state{$to};
Branch: 1 branch
BranchTrueFalseTotal
unless $state{$to} 220 902 1122
133 }
134 1210 $state{$name} = 2;
135 22 };
136 22 $visit->($_) for sort $schema->sources;
137 22 return;
138 }
139  
140 28 1;
141  
142 __END__
« DuckDB/Util.pm ↑ Summary DuckDB/Diff/Rebuild.pm »