blib/lib/DBIO/Test/StorageLayer.pm

filter: Statementstmt 90.5% 105 / 116 Branchbran 75.0% 27 / 36 Conditioncond 64.2% 9 / 14 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 90.0% 18 / 20 Podpod 100% 2 / 2 totaltotal 84.1% 164 / 195 CC 28 SCAR 15.2
CC
28
Cov
85%
storage_layer_contract_violations
9.1
_storage_type_pin_violations
6.3
storage_layer_contract_ok
5.5
CRAP 4.6
« blib/lib/DBIO/Relationship/Codegen.pm ↑ Summary blib/lib/DBIO/Introspect/Base.pm »
1 package DBIO::Test::StorageLayer;
2 # ABSTRACT: Structural contract check for DBIO storage-extension components and layers
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.900007';
8  
9 3 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use DBIO::Componentised ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Test::More ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use DBIO::Storage::Composed ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 our @EXPORT_OK = qw(
18 storage_layer_contract_ok
19 storage_layer_contract_violations
20 );
21  
22  
23 4 ▶sub storage_layer_contract_ok {
Subroutine storage_layer_contract_ok: called CC=5 SCAR=17.0
Pod documented
24 4 my ($component, $layer, $name) = @_;
25  
26 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
27  
28 4 ▶ $name = defined $name ? $name
Branch: 2 branches
BranchTrueFalseTotal
defined $name ? : 3 1 4
defined $layer ? : 0 1 1
29 : defined $layer
30 ? "$component / $layer honour the ADR 0032 section 7 storage-layer contract"
31 : "$component honours the ADR 0032 section 7 storage-layer contract";
32  
33 4 my @violations = storage_layer_contract_violations($component, $layer);
34  
35 4 my $ok = Test::More::ok(!@violations, $name);
36  
37 4 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 4 4
38 0 Test::More::diag($_) for @violations;
39 0 Test::More::diag(
40 'ADR 0032 section 7 (hard cut): a DBIO storage extension registers a '
41 . 'storage LAYER via DBIO::Schema->register_storage_layer; the schema '
42 . 'declares the driver, by its shortcut or component (ADR 0067). The extension '
43 . 'never picks the storage class itself -- not via storage_type, not via '
44 . 'inheritance. See docs/adr/'
45 . '0032-storage-layer-composition-and-async-transport-contract.md in the '
46 . 'DBIO core distribution, and DBIO::Storage::Composed "Layer rules".'
47 );
48 }
49  
50 4 return $ok;
51 }
52  
53  
54 12 ▶sub storage_layer_contract_violations {
Subroutine storage_layer_contract_violations: called CC=9 SCAR=22.1
Pod documented
55 12 my ($component, $layer) = @_;
56  
57 12 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $component and length $component and not ref $component 1 11 12
Condition66%4/6
defined $component and length $component and not ref $component
!ll&&!rl&&r
1011
defined $component and length $component
!ll&&!rl&&r
1011
MC/DC33%1/3
defined $component and length $component and not ref $component
defined $component length $component not ref $component
Truth table50%2/4
A and B and not(C)
A= defined $componentB= length $componentC= not ref $component
ABCresult
0XX0
10X0
1100
1111
58 'storage_layer_contract_violations requires a component class name'
59 ) unless defined $component && length $component && !ref $component;
60  
61 11 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if defined $layer and ref $layer || !length($layer) 1 10 11
Condition83%5/6
defined $layer and ref $layer || !length($layer)
!ll&&!rl&&r
641
ref $layer or not length $layer
l!l&&r!l&&!r
104
MC/DC66%2/3
defined $layer and ref $layer || !length($layer)
defined $layer ref $layer not length $layer
Truth table75%3/4
A and B or not(C)
A= defined $layerB= ref $layerC= not length $layer
ABCresult
0XX0
1000
11X1
1011
62 'storage_layer_contract_violations layer argument must be a class name'
63 ) if defined $layer && (ref $layer || !length $layer);
64  
65 10 DBIO::Componentised->ensure_class_loaded($component);
66 10 ▶ DBIO::Componentised->ensure_class_loaded($layer) if defined $layer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $layer 4 6 10
67  
68 10 my @violations = (
69 _driver_welding_violations($component),
70 _storage_type_pin_violations($component),
71 );
72  
73 10 ▶ push @violations, _layer_violations($layer) if defined $layer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $layer 4 6 10
74  
75 10 return @violations;
76 }
77  
78 # --- check 1: the component hangs off no driver -------------------------
79  
80 10 ▶sub _driver_welding_violations {
Subroutine _driver_welding_violations: called CC=4 SCAR=14.0
81 10 my ($component) = @_;
82  
83 10 my @ancestors = @{ mro::get_linear_isa($component) };
84 10 shift @ancestors; # the class itself is always the first rung
85  
86 10 my @violations;
87 10 for my $ancestor (@ancestors) {
88  
89 38 ▶ if ($ancestor->isa('DBIO::Storage')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ancestor->isa("DBIO::Storage")) 0 38 38
90 0 push @violations,
91 "$component inherits the storage class $ancestor: a component that IS a "
92 . 'storage has already made the storage decision the DSN is supposed to '
93 . 'make. Register a storage layer instead.';
94 0 next;
95 }
96  
97 38 ▶ next unless grep { $_ eq 'connection' }
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "connection";} "DBIO::Storage::Composed"->_own_methods($ancestor) 36 2 38
98 DBIO::Storage::Composed->_own_methods($ancestor);
99  
100 push @violations,
101 "$component inherits the connect hook ${ancestor}::connection (via "
102 2 . join(' -> ', @{ mro::get_linear_isa($component) }[0 .. _isa_index($component, $ancestor)])
103 . '). A driver component pins its storage class from connection(); '
104 . "inheriting $ancestor inherits that decision and welds the extension to "
105 . 'one driver -- and, once a schema class loads both, breaks the C3 merge. '
106 . 'This is the dbio-postgresql-age shape.';
107 }
108  
109 10 return @violations;
110 }
111  
112 # Position of $ancestor in $class's linearised ISA -- used only to print the
113 # inheritance path that reached it.
114 2 ▶sub _isa_index {
Subroutine _isa_index: called CC=3 SCAR=11.1
115 2 my ($class, $ancestor) = @_;
116 2 my $isa = mro::get_linear_isa($class);
117 2 for my $i (0 .. $#$isa) {
118 4 ▶ return $i if $isa->[$i] eq $ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if $isa->[$i] eq $ancestor 2 2 4
119 }
120 0 return 0;
121 }
122  
123 # --- check 2: the component pins no storage_type ------------------------
124  
125 # Recorded by the probe rungs below. Localised around every probe run.
126 3 our @PROBE_STORAGE_TYPE_PINS;
127 3 our @PROBE_REGISTERED_LAYERS;
128  
129 {
130 # Most specific rung of the probe: intercepts the two class-data setters a
131 # connect hook can reach, whichever package in the chain calls them.
132 package # hide from the indexer
133 DBIO::Test::StorageLayer::_Recorder;
134  
135 4 ▶ sub storage_type {
Subroutine storage_type: called CC=3 SCAR=13.3
136 4 my $self = shift;
137 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 0 4
138 4 push @DBIO::Test::StorageLayer::PROBE_STORAGE_TYPE_PINS, $_[0];
139 4 return $_[0];
140 }
141 return @DBIO::Test::StorageLayer::PROBE_STORAGE_TYPE_PINS
142 0 ▶ ? $DBIO::Test::StorageLayer::PROBE_STORAGE_TYPE_PINS[-1]
Branch: 1 branch
BranchTrueFalseTotal
@DBIO::Test::StorageLayer::PROBE_STORAGE_TYPE_PINS ? : 0 0 0
143 : 'DBIO::Storage::DBI';
144 }
145  
146 6 ▶ sub register_storage_layer {
Subroutine register_storage_layer: called CC=1 SCAR=0.0
147 6 my ($self, $layer) = @_;
148 6 push @DBIO::Test::StorageLayer::PROBE_REGISTERED_LAYERS, $layer;
149 6 return $self;
150 }
151  
152 0 ▶ sub storage_layers { [ @DBIO::Test::StorageLayer::PROBE_REGISTERED_LAYERS ] }
Subroutine storage_layers: not called CC=1 SCAR=6.9
153  
154 0 ▶ sub throw_exception { shift; die @_ }
Subroutine throw_exception: not called CC=1 SCAR=6.9
155 }
156  
157 {
158 # Least specific rung: where the component's next::method chain terminates.
159 3 package # hide from the indexer
160 DBIO::Test::StorageLayer::_ConnectTerminal;
161  
162 10 ▶ sub connection { return $_[0] }
Subroutine connection: called CC=1 SCAR=0.0
163 }
164  
165 3 my $probe_seq = 0;
166  
167 10 ▶sub _storage_type_pin_violations {
Subroutine _storage_type_pin_violations: called CC=6 SCAR=18.4
168 10 my ($component) = @_;
169  
170 10 my $probe = __PACKAGE__ . '::_Probe' . ++$probe_seq;
171 {
172 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
173 10 @{"${probe}::ISA"} = (
174 __PACKAGE__ . '::_Recorder',
175 $component,
176 __PACKAGE__ . '::_ConnectTerminal',
177 );
178 }
179 10 mro::set_mro($probe, 'c3');
180  
181 10 local @PROBE_STORAGE_TYPE_PINS = ();
182 10 local @PROBE_REGISTERED_LAYERS = ();
183  
184 10 my $error;
185 {
186 10 local $@;
187 10 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $probe->connection("dbi:DBIO_Test_StorageLayer:probe", "", "", {}); 1 } } or $error = $@ || "unknown error" 10 0 10
188 10 $probe->connection('dbi:DBIO_Test_StorageLayer:probe', '', '', {});
189 10 ▶ 1;
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
190 } or $error = ($@ || 'unknown error');
191 }
192  
193 10 my @violations;
194  
195 10 ▶ if (@PROBE_STORAGE_TYPE_PINS) {
Branch: 2 branches
BranchTrueFalseTotal
if (@DBIO::Test::StorageLayer::PROBE_STORAGE_TYPE_PINS) { } 4 6 10
elsif (defined $error) { } 0 6 6
196 push @violations,
197 "$component pins storage_type from its connect path (set to "
198 4 ▶ . join(', ', map { defined $_ ? "'$_'" : 'undef' } @PROBE_STORAGE_TYPE_PINS)
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 4 0 4
199 . '). An extension does not choose the storage class -- the DSN does, via '
200 . '_determine_driver. Register a storage layer instead. '
201 . 'This is the dbio-mysql-ev shape.';
202 }
203 elsif (defined $error) {
204 0 chomp(my $msg = $error);
205 0 push @violations,
206 "$component: its connect hook could not be probed, so the storage_type "
207 . "check could not run: $msg. A component's connection() override must be "
208 . 'a thin hook -- register a layer, then chain with $self->next::method -- '
209 . 'and callable as such.';
210 }
211  
212 10 return @violations;
213 }
214  
215 # --- check 3: the layer is a real layer ---------------------------------
216  
217 4 ▶sub _layer_violations {
Subroutine _layer_violations: called CC=4 SCAR=13.9
218 4 my ($layer) = @_;
219  
220 4 my @violations;
221  
222 4 my @ancestors = @{ mro::get_linear_isa($layer) };
223 4 shift @ancestors; # the layer itself
224  
225 4 for my $ancestor (@ancestors) {
226 10 ▶ next unless $ancestor->isa('DBIO::Storage');
Branch: 1 branch
BranchTrueFalseTotal
unless $ancestor->isa("DBIO::Storage") 4 6 10
227 6 push @violations,
228 "$layer inherits the storage class $ancestor. A layer is a plain method "
229 . 'package composed ABOVE the base storage, not a storage subclass beside '
230 . 'it -- see DBIO::Storage::Composed "Layer rules". Drop the inheritance '
231 . 'and chain into the base with $self->next::method.';
232 }
233  
234 4 ▶ if (grep { $_ eq 'new' } DBIO::Storage::Composed->_own_methods($layer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "new";} "DBIO::Storage::Composed"->_own_methods($layer)) 1 3 4
235 1 push @violations,
236 "$layer defines its own constructor (new). A layer has no constructor: "
237 . 'the synthesised class is instantiated through the base storage. See '
238 . 'DBIO::Storage::Composed "Layer rules".';
239 }
240  
241 4 return @violations;
242 }
243  
244  
245 3 1;
246  
247 __END__
« blib/lib/DBIO/Relationship/Codegen.pm ↑ Summary blib/lib/DBIO/Introspect/Base.pm »