blib/lib/DBIO/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
« blib/lib/DBIO/DuckDB/Util.pm ↑ Summary blib/lib/DBIO/DuckDB/Diff/Rebuild.pm »
1 package DBIO::DuckDB::Test;
2 # ABSTRACT: DuckDB-specific test utilities for DBIO
3  
4 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 29 our $VERSION = '0.900007';
8  
9 29 ▶use DBIO::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use DBIO::Test::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 29 ▶sub import {
Subroutine import: called CC=6 SCAR=29.5
15 29 my $self = shift;
16 29 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 29 29
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 29 ▶ 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 29 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 23 ▶sub _database {
Subroutine _database: called CC=3 SCAR=13.5
48 23 my $self = shift;
49 23 my %args = @_;
50  
51 23 delete @args{@HELPER_ARGS};
52  
53 23 ▶ if ($ENV{DBIO_TEST_DUCKDB_DSN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DBIO_TEST_DUCKDB_DSN"}) 0 23 23
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 23 'dbi:DuckDB:dbname=:memory:',
62 '', '',
63 { AutoCommit => 1, RaiseError => 1, PrintError => 0, %args },
64 );
65 }
66  
67  
68 23 ▶sub init_schema {
Subroutine init_schema: called CC=3 SCAR=11.8
Pod documented
69 23 my $self = shift;
70 23 my %args = @_;
71  
72 23 my ($dsn, $user, $pass, $opts) = $self->_database(%args);
73  
74 23 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 23 $self->_without_unsupported_fk_actions($schema);
84 23 ▶ return $schema if $args{no_deploy};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"no_deploy"} 3 20 23
85  
86 20 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 23 ▶sub _without_unsupported_fk_actions {
Subroutine _without_unsupported_fk_actions: called CC=8 SCAR=20.9
93 23 my ($self, $schema) = @_;
94 23 for my $name ($schema->sources) {
95 1035 my $source = $schema->source($name);
96 1035 my %rels;
97 1035 for my $rel ($source->relationships) {
98 3151 my $info = $source->relationship_info($rel);
99 3151 ▶ my %attrs = %{ $info->{attrs} || {} };
Condition50%1/2
$info->{'attrs'} || {}
l!l
31510
MC/DC0%0/1
$info->{'attrs'} || {}
$info->{'attrs'}
Truth table50%1/2
A
A= $info->{'attrs'}
Aresult
11
00
100 3151 for my $event (qw(on_delete on_update)) {
101 delete $attrs{$event}
102 6302 ▶ 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 230 6072 6302
Condition100%3/3
defined $attrs{$event} and $attrs{$event} =~ /\A\s*(?:CASCADE|SET\s+NULL|SET\s+DEFAULT)\s*\z/i
!ll&&!rl&&r
604923230
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 3151 $rels{$rel} = { %$info, attrs => \%attrs };
105 }
106 1035 $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 23 my (%state, %target);
113 4002 ▶ my $target_of = sub {
Subroutine __ANON__: called CC=6 SCAR=18.7
114 4002 my ($source, $rel) = @_;
115 4002 my $info = $source->relationship_info($rel);
116 4002 ▶ return unless $info && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"}{"is_foreign_key_constraint"} 2622 1380 4002
Condition66%2/3
$info and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
026221380
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 1380 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
138000
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 1380 ▶ return ($fs && !$fs->isa('DBIO::ResultSource::View')) ? $fs->source_name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$fs && !$fs->isa('DBIO::ResultSource::View') ? : 1288 92 1380
Condition66%2/3
$fs and not $fs->isa("DBIO::ResultSource::View")
!ll&&!rl&&r
0921288
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 23 };
121 23 ▶ my $visit; $visit = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
122 1265 my ($name) = @_;
123 1265 $state{$name} = 1;
124 1265 my $source = $schema->source($name);
125 1265 for my $rel (sort $source->relationships) {
126 4002 ▶ my $to = $target_of->($source, $rel) // next;
Condition100%2/2
&$target_of($source, $rel) // (next)
l!l
12882714
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 1288 ▶ next if $to eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $to eq $name 46 1242 1288
128 1242 ▶ if (($state{$to} // 0) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($state{$to} // 0) == 1) 69 1173 1242
Condition100%2/2
$state{$to} // 0
l!l
1012230
MC/DC100%1/1
$state{$to} // 0
$state{$to}
Truth table100%2/2
A
A= $state{$to}
Aresult
11
00
129 69 $source->relationship_info($rel)->{attrs}{is_foreign_key_constraint} = 0;
130 69 next;
131 }
132 1173 ▶ $visit->($to) unless $state{$to};
Branch: 1 branch
BranchTrueFalseTotal
unless $state{$to} 230 943 1173
133 }
134 1265 $state{$name} = 2;
135 23 };
136 23 $visit->($_) for sort $schema->sources;
137 23 return;
138 }
139  
140 29 1;
141  
142 __END__
« blib/lib/DBIO/DuckDB/Util.pm ↑ Summary blib/lib/DBIO/DuckDB/Diff/Rebuild.pm »