| 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
| |||||||||||||||||||||||||||||
| 17 | 0 | my $caller = caller; | |||||||||||||||||||||||||||
| 18 | 0 | for my $exp (@_) { | |||||||||||||||||||||||||||
| 19 | 0 | ▶ | if ($exp eq ':DiffSQL') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 54 | return ( | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | (map { $ENV{"DBIO_TEST_DUCKDB_${_}"} // '' } qw/DSN DBUSER DBPASS/), | ||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{"DBIO_TEST_DUCKDB_$_"} // ""
MC/DC0%0/1
$ENV{"DBIO_TEST_DUCKDB_$_"} // ""
$ENV{"DBIO_TEST_DUCKDB_$_"}
Truth table0%0/2
A
A= $ENV{"DBIO_TEST_DUCKDB_$_"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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'} || {}
MC/DC0%0/1
$info->{'attrs'} || {}
$info->{'attrs'}
Truth table50%1/2
A
A= $info->{'attrs'}
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $attrs{$event} and $attrs{$event} =~ /\A\s*(?:CASCADE|SET\s+NULL|SET\s+DEFAULT)\s*\z/i
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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$info and $info->{"attrs"}{"is_foreign_key_constraint"}
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"}
| |||||||||||||||||||||||||||||
| 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)
}
}
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)
}
}
| |||||||||||||||||||||||||||||
| 119 | 1380 | ▶ | return ($fs && !$fs->isa('DBIO::ResultSource::View')) ? $fs->source_name : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$fs and not $fs->isa("DBIO::ResultSource::View")
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")
| |||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
&$target_of($source, $rel) // (next)
&$target_of($source, $rel)
Truth table100%2/2
A
A= &$target_of($source, $rel)
| |||||||||||||||||||||||||||||
| 127 | 1288 | ▶ | next if $to eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1242 | ▶ | if (($state{$to} // 0) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$state{$to} // 0
MC/DC100%1/1
$state{$to} // 0
$state{$to}
Truth table100%2/2
A
A= $state{$to}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||