EntityModel/Test/Storage.pm

filter: Statementstmt 49.5% 50 / 101 Branchbran 2.1% 1 / 46 Conditioncond 30.7% 4 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 54.5% 12 / 22 Podpod 100% 2 / 2 totaltotal 36.1% 69 / 191 CC 31 SCAR 28.6
CC
31
Cov
34%
_simple_model
56.0
__ANON__
30.0
storage_ok
20.6
CRAP 17.5
« EntityModel/Storage.pm ↑ Summary EntityModel/EntityCollection.pm »
1 package EntityModel::Test::Storage;
2 {
3 2 $EntityModel::Test::Storage::VERSION = '0.102';
4 }
5 2 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 _isa => [qw(Exporter)],
7 2 };
8 2 ▶no if $] >= 5.017011, warnings => "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 EntityModel::Test::Storage - tests for L<EntityModel::Storage> and subclasses
13  
14 =head1 VERSION
15  
16 version 0.102
17  
18 =head1 SYNOPSIS
19  
20 use EntityModel::Test::Storage;
21 storage_ok('EntityModel::Storage::Perl', '::Perl subclass works');
22  
23 =head1 DESCRIPTION
24  
25 Provides functions for testing L<EntityModel::Storage> subclasses.
26  
27 =cut
28  
29 2 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use CPS qw(kseq);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use EntityModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 ▶use constant STORAGE_METHODS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
35 new
36 register
37 apply_model
38 apply_model_and_schema
39 read
40 create
41 store
42 remove
43 find
44 adjacent
45 prev
46 next
47 outer
48 first
49 last
50 2 );
51  
52 =head1 EXPORTS
53  
54 Since this is a test class, functions are exported automatically
55 to match behaviour of other test modules such as L<Test::More>.
56 To disable this, pass an empty list on the C<use> line or
57 use C<require> instead:
58  
59 use EntityModel::Test::Storage ();
60 EntityModel::Test::Storage::storage_ok(...);
61  
62 =cut
63  
64 2 our @EXPORT = qw(
65 storage_ok
66 storage_methods_ok
67 );
68  
69 =head1 FUNCTIONS
70  
71 =cut
72  
73 =head2 storage_ok
74  
75 Runs all available tests (including attempting to load the module) and returns the usual
76 L<Test::Builder> ok/fail response.
77  
78 =cut
79  
80 1 ▶sub storage_ok {
Subroutine storage_ok: called CC=7 SCAR=30.2
Pod documented
81 1 my $class = shift;
82 1 ▶ my $opt = shift || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
83 1 ▶ my $msg = shift || "$class is a valid, working EntityModel::Storage (sub)class";
Condition50%1/2
shift() || "$class is a valid, working EntityModel::Storage (sub)class"
l!l
01
MC/DC0%0/1
shift() || "$class is a valid, working EntityModel::Storage (sub)class"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
84  
85 # First we need to be able to load our module
86 try {
87 1 Module::Load::load($class);
88 1 } catch {
89 1 return _report_fail($msg, $_);
90 };
91  
92 0 ▶ $class->isa('EntityModel::Storage') or return _report_fail($msg, 'is not an EntityModel::Storage (sub)class');
Branch: 1 branch
BranchTrueFalseTotal
$class->isa("EntityModel::Storage") or return _report_fail($msg, "is not an EntityModel::Storage (sub)class") 0 0 0
93 0 ▶ _methods_ok($class, $msg) or return;
Branch: 1 branch
BranchTrueFalseTotal
_methods_ok($class, $msg) or return 0 0 0
94  
95 # Abstract base class won't work very well for 'real' model handling, so skip that
96  
97 0 ▶ unless($class eq 'EntityModel::Storage') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class eq "EntityModel::Storage") 0 0 0
98 0 ▶ _simple_model($class, $opt, $msg) or return;
Branch: 1 branch
BranchTrueFalseTotal
_simple_model($class, $opt, $msg) or return 0 0 0
99 }
100  
101 0 return _report_pass($msg);
102 }
103  
104 =head2 storage_methods_ok
105  
106 Check whether the expected methods are present. Requires the class to be loaded first.
107  
108 =cut
109  
110 1 ▶sub storage_methods_ok {
Subroutine storage_methods_ok: called CC=4 SCAR=17.1
Pod documented
111 1 my $class = shift;
112 1 ▶ my $opt = shift || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
113 1 ▶ my $msg = shift || "$class has all the required methods";
Condition50%1/2
shift() || "$class has all the required methods"
l!l
01
MC/DC0%0/1
shift() || "$class has all the required methods"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
114 1 ▶ return 0 unless _methods_ok($class, $msg);
Branch: 1 branch
BranchTrueFalseTotal
unless _methods_ok($class, $msg) 0 0 0
115 0 return _report_pass($msg);
116 }
117  
118 =head2 _methods_ok
119  
120 Internal helper function to report whether the expected methods are present for the subclass.
121  
122 =cut
123  
124 1 ▶sub _methods_ok {
Subroutine _methods_ok: called CC=5 SCAR=23.1
125 1 my $class = shift;
126 1 my $msg = shift;
127  
128 1 my %failed;
129 1 foreach my $method (STORAGE_METHODS) {
130 try {
131 1 $class->can($method);
132 } catch {
133 1 ▶ $failed{$method} = $_;
Condition0%0/2
$failed{$method} ||= "not available"
l!l
00
MC/DC0%0/1
$failed{$method} ||= "not available"
$failed{$method}
Truth table0%0/2
A
A= $failed{$method}
Aresult
11
00
134 1 ▶ } or $failed{$method} ||= 'not available';
Branch: 1 branch
BranchTrueFalseTotal
do { $class->can($method) }->try(do { $failed{$method} = $_ }->catch) or $failed{$method} ||= "not available" 0 0 0
135 }
136 0 ▶ if(keys %failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %failed) 0 0 0
137 0 return _report_fail($msg, join "\n", map { "Could not $class->$_ because: " . $failed{$_} } sort keys %failed);
138 }
139 0 return 1;
140 }
141  
142 =head2 _simple_model
143  
144 =cut
145  
146 0 ▶sub _simple_model {
Subroutine _simple_model: not called CC=7 SCAR=40.3
147 0 my $class = shift;
148 0 my $opt = shift;
149 0 my $msg = shift;
150  
151 # Bring in a simple model - if this fails then it's not the storage class' fault so die() rather than marking as failure
152 0 ▶ my $model = EntityModel->new->load_from(
Branch: 1 branch
BranchTrueFalseTotal
my $model = "EntityModel"->new->load_from("Perl", {"name", "mymodel", "entity", [{"name", "thing", "primary", "id", "field", [{"name", "id", "type", "int"}, {"name", "name", "type", "varchar"}]}, {"name", "other", "primary", "id", "field", [{"name", "id", "type", "int"}, {"name", "extra", "type", "varchar"}]}]}) or die "Model creation failed" 0 0 0
153 Perl => {
154 "name" => "mymodel",
155 "entity" => [ {
156 "name" => "thing",
157 "primary" => "id",
158 "field" => [
159 { "name" => "id", "type" => "int" },
160 { "name" => "name", "type" => "varchar" }
161 ]
162 }, {
163 "name" => "other",
164 "primary" => "id",
165 "field" => [
166 { "name" => "id", "type" => "int" },
167 { "name" => "extra", "type" => "varchar" }
168 ]
169 } ]
170 }
171 ) or die "Model creation failed";
172  
173 # Now we try to apply the storage model
174 0 ▶ $model->add_storage($class => $opt) or die "Failed to add storage";
Branch: 1 branch
BranchTrueFalseTotal
$model->add_storage($class, $opt) or die "Failed to add storage" 0 0 0
175  
176 # Sanity check that we end up with single storage item of the expected class
177 0 my @storage = $model->storage->list;
178 0 ▶ return _report_fail($msg, "expected 1 storage item, found " . scalar(@storage)) unless @storage == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @storage == 1 0 0 0
179 0 ▶ return _report_fail($msg, "unexpected class found, wanted $class but had " . join(',', map ref, @storage)) if grep { ref($_) ne $class } @storage;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne $class;} @storage 0 0 0
180  
181 # Now the model has been applied, we'll try to do some simple tests directly against storage:
182 # first we'll create an entry
183 0 my ($storage) = @storage;
184 0 my ($thing) = grep { $_->name eq 'thing' } $model->entity->list;
185 0 ▶ die "no thing" unless $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless $thing 0 0 0
186  
187 # Support failure passthrough in our continuations
188 0 my $failed = 0;
189 0 ▶ my $fail = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
190 0 my ($err, $next) = @_;
191 # Bail out immediately if we're already in failure state.
192 0 ▶ $next->() if $failed && $next;
Branch: 1 branch
BranchTrueFalseTotal
if $failed and $next 0 0 0
Condition0%0/3
$failed and $next
!ll&&!rl&&r
000
MC/DC0%0/2
$failed and $next
$failed $next
Truth table0%0/3
A and B
A= $failedB= $next
ABresult
0X0
100
111
193  
194 # This would be a most lamentable state of affairs
195 0 ▶ die "Failed already and no continuation, help!" if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 0 0 0
196  
197 0 $failed = 1;
198 0 _report_fail($msg, $err);
199 0 ▶ $next->() if $next;
Branch: 1 branch
BranchTrueFalseTotal
if $next 0 0 0
200 0 };
201  
202 # Take advantage of CPS to avoid excessive indentation
203 0 my ($id, $data);
204 0 ▶ kseq(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
205 # First we create a simple entity instance
206 0 my $next = pop;
207 $storage->create(
208 entity => $thing,
209 data => {
210 name => 'Test name',
211 },
212 0 ▶ on_complete => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 $id = shift;
214 0 $next->();
215 },
216 0 ▶ on_fail => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 $fail->("Something failed", $next);
218 }
219 0 ▶ ) or $fail->("->create returned false", $next);
Branch: 1 branch
BranchTrueFalseTotal
$storage->create("entity", $thing, "data", {"name", "Test name"}, "on_complete", sub { $id = shift(); &$next(); } , "on_fail", sub { &$fail("Something failed", $next); } ) or &$fail("->create returned false", $next) 0 0 0
220 0 ▶ }, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
221 # Next we check, then read it back
222 0 my $next = pop;
223 0 ▶ $next->() if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 0 0 0
224 0 ▶ $fail->("no ID assigned", $next) unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
225  
226 $storage->read(
227 entity => $thing,
228 id => $id,
229 0 ▶ on_complete => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 0 $data = shift;
231 0 $next->();
232 },
233 0 ▶ on_fail => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 0 $fail->("Something failed", $next);
235 }
236 0 ▶ ) or $fail->("->read returned false", $next);
Branch: 1 branch
BranchTrueFalseTotal
$storage->read("entity", $thing, "id", $id, "on_complete", sub { $data = shift(); &$next(); } , "on_fail", sub { &$fail("Something failed", $next); } ) or &$fail("->read returned false", $next) 0 0 0
237 0 ▶ }, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
238 # Verify that we read back what we wrote originally
239 0 my %read = %$data;
240 0 ▶ $fail->("wrong keys returned: " . join ',', keys %read) unless [sort keys %read] ~~ [qw(id name)];
Branch: 1 branch
BranchTrueFalseTotal
unless [sort(keys %read)] ~~ ["id", "name"] 0 0 0
241 0 ▶ $fail->("wrong data for name - returned: " . $read{name}) unless $read{name} eq 'Test name';
Branch: 1 branch
BranchTrueFalseTotal
unless $read{"name"} eq "Test name" 0 0 0
242 0 });
243 0 ▶ return 0 if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 0 0 0
244 0 return 1;
245 }
246  
247 =head2 _report_status
248  
249 Internal helper function to report pass/fail via L<Test::Builder>.
250  
251 =cut
252  
253 1 ▶sub _report_status {
Subroutine _report_status: called CC=2 SCAR=7.0
254 1 my $ok = shift;
255 1 my $msg = shift;
256 1 my $diag = shift;
257  
258 1 my $test = Test::Builder->new;
259 1 $test->ok($ok, $msg);
260 1 ▶ $test->diag($diag) if defined $diag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $diag 0 1 1
261 1 return $ok;
262 }
263  
264 0 ▶sub _report_pass { _report_status(1, @_) }
Subroutine _report_pass: not called CC=1 SCAR=6.9
265  
266 1 ▶sub _report_fail { _report_status(0, @_) }
Subroutine _report_fail: called CC=1 SCAR=0.0
267  
268 2 1;
269  
270 __END__
« EntityModel/Storage.pm ↑ Summary EntityModel/EntityCollection.pm »