« blib/lib/DBIO/DeploymentHandler/DeployMethod/Native.pm
↑ Summary
blib/lib/DBIO/ResultSetColumn.pm »
| 1 | package DBIO::Test::ExtensionSet; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Commutativity test for a set of database extension classes (ADR 0066) | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use DBIO::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Test::More (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use DBIO::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use DBIO::Extension; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use DBIO::Extension::Protocol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 18 | extension_set_ok | ||||||||||||||||||||||||||||
| 19 | extension_set_violations | ||||||||||||||||||||||||||||
| 20 | extension_set_orders | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 22 | ▶ | sub extension_set_orders { | ||||||||||||||||||||||||||
|
Subroutine
extension_set_orders: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 22 | my (@names) = @_; | |||||||||||||||||||||||||||
| 26 | 22 | ▶ | return ([]) unless @names; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 19 | ▶ | if (@names <= 5) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 18 | my @out; | |||||||||||||||||||||||||||
| 30 | 404 | ▶ | my $permute; $permute = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 31 | 404 | my ($done, $rest) = @_; | |||||||||||||||||||||||||||
| 32 | 404 | ▶ | return push @out, $done unless @$rest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 252 | for my $i (0 .. $#$rest) { | |||||||||||||||||||||||||||
| 34 | 386 | my @rest = @$rest; | |||||||||||||||||||||||||||
| 35 | 386 | my ($pick) = splice @rest, $i, 1; | |||||||||||||||||||||||||||
| 36 | 386 | $permute->([ @$done, $pick ], \@rest); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 18 | }; | |||||||||||||||||||||||||||
| 39 | 18 | $permute->([], \@names); | |||||||||||||||||||||||||||
| 40 | 18 | undef $permute; | |||||||||||||||||||||||||||
| 41 | 18 | return @out; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | my (%seen, @out); | |||||||||||||||||||||||||||
| 45 | 1 | for my $base (\@names, [ reverse @names ]) { | |||||||||||||||||||||||||||
| 46 | 2 | for my $shift (0 .. $#$base) { | |||||||||||||||||||||||||||
| 47 | 12 | my @order = (@$base[ $shift .. $#$base ], @$base[ 0 .. $shift - 1 ]); | |||||||||||||||||||||||||||
| 48 | 12 | ▶ | push @out, \@order unless $seen{ join "\0", @order }++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 1 | return @out; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 22 | ▶ | sub _dump { require Data::Dumper; local $Data::Dumper::Sortkeys = 1; local $Data::Dumper::Indent = 0; local $Data::Dumper::Terse = 1; Data::Dumper::Dumper($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
_dump: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # The driver is handed over as a class name and loaded here; an object is used | ||||||||||||||||||||||||||||
| 57 | # as it is. A class that cannot be loaded is an exception, not a violation. | ||||||||||||||||||||||||||||
| 58 | 22 | ▶ | sub _load_driver { | ||||||||||||||||||||||||||
|
Subroutine
_load_driver: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 59 | 22 | my ($driver) = @_; | |||||||||||||||||||||||||||
| 60 | 22 | ▶ | return if ref $driver; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 21 | ▶ | return if $driver->can('new'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | (my $file = "$driver.pm") =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 64 | 3 | ▶ | return if $INC{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 3 | ▶ | return if eval { require $file; 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | my $error = $@; | |||||||||||||||||||||||||||
| 68 | 2 | ▶ | DBIO::Exception->throw( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | $error =~ /^Can't locate \Q$file\E in \@INC/ | ||||||||||||||||||||||||||||
| 70 | ? "extension_set: the driver class $driver is not installed" | ||||||||||||||||||||||||||||
| 71 | : "extension_set: the driver class $driver does not load: $error" | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 20 | ▶ | sub _entries { | ||||||||||||||||||||||||||
|
Subroutine
_entries: called CC=11 SCAR=25.2
| |||||||||||||||||||||||||||||
| 76 | 20 | my ($args) = @_; | |||||||||||||||||||||||||||
| 77 | 20 | my (@entries, @violations); | |||||||||||||||||||||||||||
| 78 | 20 | ▶ | for my $item (@{ $args->{extensions} || [] }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'extensions'} || []
MC/DC0%0/1
$args->{'extensions'} || []
$args->{'extensions'}
Truth table50%1/2
A
A= $args->{'extensions'}
| |||||||||||||||||||||||||||||
| 79 | 27 | ▶ | my $entry = ref $item eq 'HASH' ? { %$item } : { class => $item }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 27 | my $class = $entry->{class}; | |||||||||||||||||||||||||||
| 81 | 27 | ▶ | $entry->{name} //= eval { $class->name }; | ||||||||||||||||||||||||||
|
Condition66%2/3
$entry->{"name"} //= eval {
do {
$class->name
}
}
MC/DC50%1/2
$entry->{"name"} //= eval {
do {
$class->name
}
}
$entry->{"name"}
eval {
do {
$class->name
}
}
Truth table66%2/3
A //= B
A= $entry->{"name"}B= eval {
do {
$class->name
}
}
| |||||||||||||||||||||||||||||
| 82 | 27 | ▶ | unless (defined $entry->{name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | push @violations, "Extension class $class states no name; it cannot be put into a set"; | |||||||||||||||||||||||||||
| 84 | 1 | next; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 26 | ▶ | $entry->{options} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"options"} ||= {}
MC/DC0%0/1
$entry->{"options"} ||= {}
$entry->{"options"}
Truth table50%1/2
A
A= $entry->{"options"}
| |||||||||||||||||||||||||||||
| 87 | 26 | ▶ | if (my $state = ($args->{installed} || {})->{ $entry->{name} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$args->{'installed'} || {}
MC/DC0%0/1
$args->{'installed'} || {}
$args->{'installed'}
Truth table50%1/2
A
A= $args->{'installed'}
| |||||||||||||||||||||||||||||
| 88 | 0 | @{$entry}{keys %$state} = values %$state; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | 26 | ▶ | $entry->{installed} //= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"installed"} //= 1
MC/DC0%0/1
$entry->{"installed"} //= 1
$entry->{"installed"}
Truth table50%1/2
A
A= $entry->{"installed"}
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 26 | ▶ | eval { DBIO::Extension->check_interface($class); 1 } or push @violations, "$@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 26 | push @entries, $entry; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 20 | return (\@entries, \@violations); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 56 | ▶ | sub _run { | ||||||||||||||||||||||||||
|
Subroutine
_run: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 100 | 56 | my ($args, $entries, $order) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 56 | my $driver = $args->{driver}; | |||||||||||||||||||||||||||
| 103 | 56 | ▶ | my $deploy = ref $driver ? $driver : $driver->new; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 56 | my $protocol = DBIO::Extension::Protocol->new( | |||||||||||||||||||||||||||
| 105 | entries => $entries, | ||||||||||||||||||||||||||||
| 106 | driver => $deploy->_extension_driver_helpers, | ||||||||||||||||||||||||||||
| 107 | connection_schema => $deploy->_extension_connection_schema, | ||||||||||||||||||||||||||||
| 108 | restricted => $deploy->_extension_restricted, | ||||||||||||||||||||||||||||
| 109 | run_order => $order, | ||||||||||||||||||||||||||||
| 110 | verify_input => 1, | ||||||||||||||||||||||||||||
| 111 | trace => 1, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 56 | my %out; | |||||||||||||||||||||||||||
| 115 | 56 | $out{states} = $protocol->inspect($args->{dbh}); | |||||||||||||||||||||||||||
| 116 | 56 | my ($source, $target) = $protocol->extend_models($deploy, $args->{source}, $args->{target}); | |||||||||||||||||||||||||||
| 117 | 42 | $out{source} = $source; | |||||||||||||||||||||||||||
| 118 | 42 | $out{target} = $target; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 42 | my $statements; | |||||||||||||||||||||||||||
| 121 | 42 | ▶ | if (my $diff = $args->{diff}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 122 | 12 | $statements = $diff->($source, $target, $protocol); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 30 | elsif (my $class = eval { $deploy->_diff_class }) { | |||||||||||||||||||||||||||
| 125 | 30 | my @sql = $class->new(source => $source, target => $target, extensions => $protocol)->as_sql; | |||||||||||||||||||||||||||
| 126 | 30 | ▶ | $statements = @sql == 1 && ref $sql[0] eq 'ARRAY' ? $sql[0] : \@sql; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@sql == 1 and ref $sql[0] eq "ARRAY"
MC/DC0%0/2
@sql == 1 and ref $sql[0] eq "ARRAY"
@sql == 1
ref $sql[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @sql == 1B= ref $sql[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 42 | $out{statements} = $statements; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 42 | ▶ | $out{verdicts} = [ map { $protocol->verdict($_) } @{ $args->{changes} || [] } ]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'changes'} || []
MC/DC100%1/1
$args->{'changes'} || []
$args->{'changes'}
Truth table100%2/2
A
A= $args->{'changes'}
| |||||||||||||||||||||||||||||
| 131 | 42 | $out{refusals} = [ $protocol->refusals ]; | |||||||||||||||||||||||||||
| 132 | 42 | ▶ | if (my @conflicts = $protocol->conflicts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 4 | DBIO::Exception->throw("The extensions contradict each other: " . join('; ', @conflicts)); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 38 | $out{activation} = $protocol->activation; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 38 | my $trace = $protocol->trace; | |||||||||||||||||||||||||||
| 138 | 38 | $out{"extension '$_'"} = $trace->{$_} for sort keys %$trace; | |||||||||||||||||||||||||||
| 139 | 38 | return \%out; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 22 | ▶ | sub extension_set_violations { | ||||||||||||||||||||||||||
|
Subroutine
extension_set_violations: called CC=14 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 22 | my (%args) = @_; | |||||||||||||||||||||||||||
| 145 | 22 | ▶ | DBIO::Exception->throw('extension_set: driver is required') unless $args{driver}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | DBIO::Exception->throw('extension_set: source and target models are required') | ||||||||||||||||||||||||||||
| 147 | 22 | ▶ | unless $args{source} && $args{target}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args{"source"} and $args{"target"}
MC/DC0%0/2
$args{"source"} and $args{"target"}
$args{"source"}
$args{"target"}
Truth table33%1/3
A and B
A= $args{"source"}B= $args{"target"}
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 22 | _load_driver($args{driver}); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 20 | my ($entries, $violations) = _entries(\%args); | |||||||||||||||||||||||||||
| 152 | 20 | ▶ | return @$violations if @$violations; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 18 | my @names = sort map { $_->{name} } @$entries; | |||||||||||||||||||||||||||
| 155 | 18 | my ($reference, $reference_label); | |||||||||||||||||||||||||||
| 156 | 18 | my @out; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 18 | for my $order (extension_set_orders(@names)) { | |||||||||||||||||||||||||||
| 159 | 28 | ▶ | my $label = @$order ? join(' > ', @$order) : '(no extensions)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 28 | for my $run (1, 2) { | |||||||||||||||||||||||||||
| 161 | 56 | ▶ | my $here = "order [$label]" . ($run == 2 ? ', repeated' : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 56 | ▶ | my $result = eval { _run(\%args, $entries, @$order ? $order : undef) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 56 | ▶ | if (!$result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 18 | my $error = "$@"; | |||||||||||||||||||||||||||
| 165 | 18 | $error =~ s/\s+$//; | |||||||||||||||||||||||||||
| 166 | 18 | push @out, "$here: $error"; | |||||||||||||||||||||||||||
| 167 | 18 | next; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 38 | ▶ | if (!$reference) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 12 | ($reference, $reference_label) = ($result, $here); | |||||||||||||||||||||||||||
| 171 | 12 | next; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 26 | for my $part (sort keys %$result) { | |||||||||||||||||||||||||||
| 174 | 226 | ▶ | next if DBIO::Util::canonical_text($result->{$part}) eq DBIO::Util::canonical_text($reference->{$part}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | push @out, "$here differs from $reference_label in $part: " | ||||||||||||||||||||||||||||
| 176 | 11 | . _dump($result->{$part}) . ' versus ' . _dump($reference->{$part}); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 18 | return @out; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 2 | ▶ | sub extension_set_ok { | ||||||||||||||||||||||||||
|
Subroutine
extension_set_ok: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | 2 | my (%args) = @_; | |||||||||||||||||||||||||||
| 186 | 2 | ▶ | my $tb = delete $args{builder} || Test::More->builder; | ||||||||||||||||||||||||||
|
Condition66%2/3
delete $args{'builder'} || 'Test::More'->builder
MC/DC0%0/2
delete $args{'builder'} || 'Test::More'->builder
delete $args{'builder'}
'Test::More'->builder
Truth table66%2/3
A || B
A= delete $args{'builder'}B= 'Test::More'->builder
| |||||||||||||||||||||||||||||
| 187 | 2 | ▶ | my $name = delete $args{name} // 'the extension set commutes'; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"name"} // "the extension set commutes"
MC/DC0%0/1
delete $args{"name"} // "the extension set commutes"
delete $args{"name"}
Truth table50%1/2
A
A= delete $args{"name"}
| |||||||||||||||||||||||||||||
| 188 | 2 | my @violations = extension_set_violations(%args); | |||||||||||||||||||||||||||
| 189 | 2 | my $ok = $tb->ok(!@violations, $name); | |||||||||||||||||||||||||||
| 190 | 2 | $tb->diag(" $_") for @violations; | |||||||||||||||||||||||||||
| 191 | 2 | return $ok; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 4 | 1; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | __END__ | ||||||||||||||||||||||||||||