Tapper/Schema/TestrunDB/Result/Testrun.pm

filter: Statementstmt 69.4% 109 / 157 Branchbran 42.5% 17 / 40 Conditioncond 31.0% 9 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 68.7% 11 / 16 Podpod 100% 12 / 12 totaltotal 57.8% 158 / 273 CC 37 SCAR 31.9
CC
37
Cov
60%
rerun
49.9
update_content
42.0
add_requested_resource_by_name
12.0
CRAP 24.4
« Tapper/Schema/TestrunDB/Result/Precondition.pm ↑ Summary Tapper/Schema/TestrunDB/Result/TestrunScheduling.pm »
1 package Tapper::Schema::TestrunDB::Result::Testrun;
2 10 our $AUTHORITY = 'cpan:TAPPER';
3 10 $Tapper::Schema::TestrunDB::Result::Testrun::VERSION = '5.0.12';
4 # ABSTRACT: Tapper - Containing Testruns
5  
6 10 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use parent 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 __PACKAGE__->load_components(qw/InflateColumn::DateTime TimeStamp Core/);
13 10 __PACKAGE__->table('testrun');
14 10 __PACKAGE__->add_columns(
15 'id', {
16 data_type => 'INT',
17 default_value => undef,
18 is_nullable => 0,
19 size => 11,
20 is_auto_increment => 1,
21 },
22 'shortname', {
23 data_type => 'VARCHAR',
24 default_value => undef,
25 is_nullable => 0,
26 size => 255,
27 },
28 'notes', {
29 data_type => 'TEXT',
30 default_value => undef,
31 is_nullable => 1,
32 },
33 'topic_name', {
34 data_type => 'VARCHAR',
35 default_value => undef,
36 is_nullable => 0,
37 size => 255,
38 is_foreign_key => 1,
39 },
40 'starttime_earliest', {
41 data_type => 'TIMESTAMP',
42 default_value => undef,
43 is_nullable => 1,
44 },
45 'starttime_testrun', {
46 data_type => 'TIMESTAMP',
47 default_value => undef,
48 is_nullable => 1,
49 },
50 'starttime_test_program', {
51 data_type => 'TIMESTAMP',
52 default_value => undef,
53 is_nullable => 1,
54 },
55 'endtime_test_program', {
56 data_type => 'TIMESTAMP',
57 default_value => undef,
58 is_nullable => 1,
59 },
60 'owner_id', {
61 data_type => 'INT',
62 default_value => undef,
63 is_nullable => 1,
64 size => 11,
65 is_foreign_key => 1,
66 },
67 'testplan_id', {
68 data_type => 'INT',
69 default_value => undef,
70 is_nullable => 1,
71 size => 11,
72 is_foreign_key => 1,
73 },
74 'wait_after_tests', {
75 data_type => 'TINYINT',
76 default_value => 0,
77 is_nullable => 1, #TODO: 0
78 size => 1,
79 },
80 # number of times to rerun this test on error
81 'rerun_on_error', {
82 data_type => 'TINYINT',
83 default_value => 0,
84 is_nullable => 1, #TODO: 0
85 size => 1,
86 },
87 'created_at', {
88 data_type => 'TIMESTAMP',
89 default_value => undef,
90 is_nullable => 1,
91 set_on_create => 1,
92 },
93 'updated_at', {
94 data_type => 'TIMESTAMP',
95 default_value => undef,
96 is_nullable => 1,
97 set_on_create => 1,
98 set_on_update => 1,
99 },
100 );
101  
102 10 __PACKAGE__->set_primary_key('id');
103  
104 10 (my $basepkg = __PACKAGE__) =~ s/::\w+$//;
105  
106 # * : 1
107 10 __PACKAGE__->belongs_to(
108 owner => "${basepkg}::Owner",
109 { 'foreign.id' => 'self.owner_id' },
110 );
111 10 __PACKAGE__->belongs_to(
112 testplan_instance => "${basepkg}::TestplanInstance",
113 { 'foreign.id' => 'self.testplan_id' },
114 );
115  
116 # 1 : 0,1
117 10 __PACKAGE__->might_have(
118 testrun_scheduling => "${basepkg}::TestrunScheduling",
119 { 'foreign.testrun_id' => 'self.id' },
120 );
121 10 __PACKAGE__->might_have(
122 scenario_element => "${basepkg}::ScenarioElement",
123 { 'foreign.testrun_id' => 'self.id' },
124 );
125 10 __PACKAGE__->might_have(
126 state => "${basepkg}::State",
127 { 'foreign.testrun_id' => 'self.id' },
128 );
129 10 __PACKAGE__->might_have(
130 reportgrouptestrunstats => "${basepkg}::ReportgroupTestrunStats",
131 { 'foreign.testrun_id' => 'self.id' },
132 );
133  
134 # 1 : *
135 10 __PACKAGE__->has_many(
136 testrun_precondition => "${basepkg}::TestrunPrecondition",
137 { 'foreign.testrun_id' => 'self.id' },
138 );
139 10 __PACKAGE__->has_many(
140 message => "${basepkg}::Message",
141 { 'foreign.testrun_id' => 'self.id' },
142 );
143 10 __PACKAGE__->has_many(
144 testrun_requested_host => "${basepkg}::TestrunRequestedHost",
145 { 'foreign.testrun_id' => 'self.id' },
146 );
147  
148 10 __PACKAGE__->has_many(
149 testrun_requested_resource => "${basepkg}::TestrunRequestedResource",
150 { 'foreign.testrun_id' => 'self.id' },
151 );
152  
153 10 __PACKAGE__->has_many(
154 testrun_dependencies => "${basepkg}::TestrunDependency",
155 { 'foreign.depender_testrun_id' => 'self.id' }
156 );
157  
158 # * : *
159 10 __PACKAGE__->many_to_many(
160 preconditions => "testrun_precondition",
161 'precondition'
162 );
163  
164 10 __PACKAGE__->many_to_many(
165 depending_testruns => "testrun_dependencies",
166 'dependee'
167 );
168  
169 # -------------------- methods on results --------------------
170  
171  
172 sub to_string
173 0 ▶{
Subroutine to_string: not called CC=2 SCAR=17.9
Pod documented
174 0 my ($self) = @_;
175  
176 0 my $format = join( $Tapper::Schema::TestrunDB::DELIM, qw/%s %s %s %s %s %s %s %s %s %s %s %s %s %s /, '');
177 sprintf (
178 $format,
179 map {
180 0 ▶ defined $self->$_
Branch: 1 branch
BranchTrueFalseTotal
defined $self->$_ ? : 0 0 0
181 ? $self->$_
182 : $Tapper::Schema::TestrunDB::NULL
183 0 } @{$self->result_source->{_ordered_columns} }
184 );
185 }
186  
187  
188 0 ▶sub claimed_resources {
Subroutine claimed_resources: not called CC=1 SCAR=6.9
Pod documented
189 0 my ($self) = @_;
190 0 return map { $_->selected_resource } $self->testrun_requested_resource
191 ->search({ 'selected_resource_id' => { '!=', undef } }, { 'prefetch' => 'selected_resource' })->all();
192 }
193  
194  
195 sub is_member
196 20 ▶{
Subroutine is_member: called CC=1 SCAR=0.0
Pod documented
197 20 my ($head, @tail) = @_;
198 20 grep { $head->id eq $_->id } @tail;
199 }
200  
201  
202 sub ordered_preconditions
203 2 ▶{
Subroutine ordered_preconditions: called CC=4 SCAR=13.9
Pod documented
204 2 my ($self) = @_;
205  
206 2 my @done = ();
207 2 my %seen = ();
208 2 my @todo = ();
209  
210 2 @todo = $self->preconditions->search({}, {order_by => 'succession'})->all;
211  
212 2 ▶ while (my $head = shift @todo)
Branch: 1 branch
BranchTrueFalseTotal
if (my $head = shift @todo) 32 2 34
213 {
214 32 ▶ if ($seen{$head->id})
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$head->id}) { } 20 12 32
215 {
216 20 ▶ push @done, $head unless is_member($head, @done);
Branch: 1 branch
BranchTrueFalseTotal
unless is_member($head, @done) 12 8 20
217 }
218 else
219 {
220 12 $seen{$head->id} = 1;
221 12 my @pre_todo = $head->child_preconditions->search({}, { order_by => 'succession' } )->all;
222 12 unshift @todo, @pre_todo, $head;
223 }
224 }
225 2 return @done;
226 }
227  
228  
229 0 ▶sub update_content {
Subroutine update_content: not called CC=6 SCAR=37.4
Pod documented
230 0 my ($self, $args) =@_;
231  
232 0 ▶ $self->notes ( $args->{notes} ) if $args->{notes};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"notes"} 0 0 0
233 0 ▶ $self->shortname ( $args->{shortname} ) if $args->{shortname};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"shortname"} 0 0 0
234 0 ▶ $self->topic_name ( $args->{topic} ) if $args->{topic};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"topic"} 0 0 0
235 0 ▶ $self->starttime_earliest ( $args->{date} ) if $args->{date};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"date"} 0 0 0
236 0 ▶ $self->owner_id ( $args->{owner_id} ) if $args->{owner_id};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"owner_id"} 0 0 0
237 0 $self->update;
238 0 return $self->id;
239 }
240  
241  
242 sub rerun
243 1 ▶{
Subroutine rerun: called CC=18 SCAR=39.1
Pod documented
244 1 my ($self, $args) = @_;
245  
246 my $testrun_new = $self->result_source->schema->resultset('Testrun')->new
247 ({
248 notes => $args->{notes} || $self->notes,
249 shortname => $args->{shortname} || $self->shortname,
250 topic_name => $args->{topic_name} || $self->topic_name,
251 starttime_earliest => $args->{earliest} || DateTime->now,
252 1 ▶ owner_id => $args->{owner_id} || $self->owner_id,
Condition33%5/15
$args->{'notes'} || $self->notes
l!l&&r!l&&!r
010
$args->{'shortname'} || $self->shortname
l!l&&r!l&&!r
010
$args->{'topic_name'} || $self->topic_name
l!l&&r!l&&!r
010
$args->{'earliest'} || 'DateTime'->now
l!l&&r!l&&!r
010
$args->{'owner_id'} || $self->owner_id
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'notes'} || $self->notes
$args->{'notes'} $self->notes
MC/DC0%0/2
$args->{'shortname'} || $self->shortname
$args->{'shortname'} $self->shortname
MC/DC0%0/2
$args->{'topic_name'} || $self->topic_name
$args->{'topic_name'} $self->topic_name
MC/DC0%0/2
$args->{'earliest'} || 'DateTime'->now
$args->{'earliest'} 'DateTime'->now
MC/DC0%0/2
$args->{'owner_id'} || $self->owner_id
$args->{'owner_id'} $self->owner_id
Truth table33%1/3
A || B
A= $args->{'notes'}B= $self->notes
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args->{'shortname'}B= $self->shortname
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args->{'topic_name'}B= $self->topic_name
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args->{'earliest'}B= 'DateTime'->now
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args->{'owner_id'}B= $self->owner_id
ABresult
1X1
011
000
253 });
254  
255 # prepare job scheduling infos
256 1 my $testrunscheduling = $self->result_source->schema->resultset('TestrunScheduling')->search({ testrun_id => $self->id }, {rows => 1})->first;
257 1 my ($queue_id, $host_id, $auto_rerun, $requested_features, $requested_hosts);
258 1 my @resource_requests;
259 1 ▶ if ($testrunscheduling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($testrunscheduling) { } 1 0 1
260 1 $queue_id = $testrunscheduling->queue_id;
261 1 $host_id = $testrunscheduling->host_id;
262 1 $auto_rerun = $testrunscheduling->auto_rerun;
263 1 $requested_features = $testrunscheduling->requested_features;
264 1 $requested_hosts = $testrunscheduling->requested_hosts;
265 1 @resource_requests = $testrunscheduling->requested_resources;
266 } else {
267 0 my $queue = $self->result_source->schema->resultset('Queue')->search({ name => "AdHoc"}, {rows => 1})->first;
268 0 ▶ if (not $queue) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($queue) 0 0 0
269 0 die "No default queue 'AdHoc' found.";
270 }
271 0 $queue_id = $queue->id;
272 0 $auto_rerun = 0;
273 }
274  
275 # create testrun and job
276 1 $testrun_new->insert;
277 my $testrunscheduling_new = $self->result_source->schema->resultset('TestrunScheduling')->new
278 ({
279 testrun_id => $testrun_new->id,
280 queue_id => $args->{queue_id} || $queue_id,
281 status => "prepare",
282 1 ▶ auto_rerun => $args->{host_id} // $auto_rerun,
Condition33%2/6
$args->{'queue_id'} || $queue_id
l!l&&r!l&&!r
001
$args->{"host_id"} // $auto_rerun
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'queue_id'} || $queue_id
$args->{'queue_id'} $queue_id
MC/DC0%0/2
$args->{"host_id"} // $auto_rerun
$args->{"host_id"} $auto_rerun
Truth table33%1/3
A || B
A= $args->{'queue_id'}B= $queue_id
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args->{"host_id"}B= $auto_rerun
ABresult
1X1
011
000
283 host_id => undef,
284 });
285 1 $testrunscheduling_new->insert;
286  
287 # assign requested host and features
288 1 ▶ if ($testrunscheduling and $testrunscheduling->requested_features->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($testrunscheduling and $testrunscheduling->requested_features->count) 0 1 1
Condition33%1/3
$testrunscheduling and $testrunscheduling->requested_features->count
!ll&&!rl&&r
010
MC/DC0%0/2
$testrunscheduling and $testrunscheduling->requested_features->count
$testrunscheduling $testrunscheduling->requested_features->count
Truth table33%1/3
A and B
A= $testrunschedulingB= $testrunscheduling->requested_features->count
ABresult
0X0
100
111
289 0 foreach my $feature (map {$_->feature}$testrunscheduling->requested_features->all) {
290 0 my $assigned_feature = $self->result_source->schema->resultset('TestrunRequestedFeature')->new({feature => $feature, testrun_id => $testrun_new->id});
291 0 $assigned_feature->insert;
292 }
293 }
294 1 ▶ if ($testrunscheduling and $testrunscheduling->requested_hosts->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($testrunscheduling and $testrunscheduling->requested_hosts->count) 1 0 1
Condition33%1/3
$testrunscheduling and $testrunscheduling->requested_hosts->count
!ll&&!rl&&r
001
MC/DC0%0/2
$testrunscheduling and $testrunscheduling->requested_hosts->count
$testrunscheduling $testrunscheduling->requested_hosts->count
Truth table33%1/3
A and B
A= $testrunschedulingB= $testrunscheduling->requested_hosts->count
ABresult
0X0
100
111
295 1 foreach my $host_id (map {$_->host_id}$testrunscheduling->requested_hosts->all) {
296 3 my $assigned_host = $self->result_source->schema->resultset('TestrunRequestedHost')->new({host_id => $host_id, testrun_id => $testrun_new->id});
297 3 $assigned_host->insert;
298 }
299 }
300 # copy resource requests
301 1 foreach my $request (@resource_requests) {
302 0 $testrun_new->add_requested_resource_by_id(map { $_->resource_id } $request->alternatives);
303 }
304  
305 # assign preconditions
306 1 my $preconditions = $self->preconditions->search({}, {order_by => 'succession'});
307 1 my @preconditions;
308 1 ▶ while (my $precond = $preconditions->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $precond = $preconditions->next) 0 1 1
309 0 push @preconditions, $precond->id;
310 }
311 1 $testrunscheduling_new->status('schedule');
312 1 $testrunscheduling_new->update;
313 1 $testrun_new->assign_preconditions(@preconditions);
314  
315 1 return $testrun_new;
316  
317 }
318  
319  
320 2 ▶sub assign_preconditions {
Subroutine assign_preconditions: called CC=3 SCAR=11.0
Pod documented
321 2 my ($self, @preconditions) = @_;
322  
323 2 my $succession = 1;
324 2 foreach my $precondition_id (@preconditions) {
325 2 my $testrun_precondition = $self->result_source->schema->resultset('TestrunPrecondition')->new
326 ({
327 testrun_id => $self->id,
328 precondition_id => $precondition_id,
329 succession => $succession,
330 });
331 2 eval {
332 2 $testrun_precondition->insert;
333 };
334 2 ▶ return "Can not assign $precondition_id: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
335 2 $succession++;
336 }
337 2 return 0;
338 }
339  
340  
341 1 ▶sub insert_preconditions {
Subroutine insert_preconditions: called CC=4 SCAR=13.9
Pod documented
342 1 my ($self, $position, @preconditions) = @_;
343  
344 1 my $succession = $position;
345 1 my $testrun_precondition = $self->result_source->schema->resultset('TestrunPrecondition');
346  
347 # move existing preconditions
348 1 my $remaining_preconditions = $testrun_precondition->search({testrun_id => $self->id,
349 succession => { '>=' => $position }});
350 1 ▶ while (my $remain = $remaining_preconditions->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $remain = $remaining_preconditions->next) 3 1 4
351 3 $remain->succession($remain->succession + int @preconditions);
352 3 $remain->update;
353 }
354  
355 # assign new ones
356 1 foreach my $precondition_id (@preconditions) {
357 2 my $testrun_precondition = $testrun_precondition->new
358 ({
359 testrun_id => $self->id,
360 precondition_id => $precondition_id,
361 succession => $succession,
362 });
363 2 eval {
364 2 $testrun_precondition->insert;
365 };
366 2 ▶ return "Can not assign $precondition_id: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
367 2 $succession++;
368 }
369 1 return 0;
370 }
371  
372  
373 1 ▶sub disassign_preconditions {
Subroutine disassign_preconditions: called CC=3 SCAR=11.1
Pod documented
374 1 my ($self, @preconditions) = @_;
375  
376 1 my $table = $self->result_source->schema->resultset('TestrunPrecondition');
377 1 my $preconditions;
378 1 ▶ if (not @preconditions) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @preconditions) { } 1 0 1
379 1 $preconditions = $table->search({testrun_id => $self->id});
380 } else {
381 0 $preconditions = $table->search({testrun_id => $self->id,
382 precondition_id => [ -or => [ @preconditions ]]});
383 }
384  
385  
386 1 ▶ while( my $precondition = $preconditions->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $precondition = $preconditions->next) 2 1 3
387 2 $precondition->delete();
388 }
389 1 return 0;
390 }
391  
392  
393 sub sqlt_deploy_hook
394 8 ▶{
Subroutine sqlt_deploy_hook: called CC=1 SCAR=0.0
Pod documented
395 8 my ($self, $sqlt_table) = @_;
396 8 $sqlt_table->add_index(name => 'testrun_idx_created_at', fields => ['created_at']);
397 }
398  
399  
400 0 ▶sub add_requested_resource_by_name {
Subroutine add_requested_resource_by_name: not called CC=3 SCAR=24.8
Pod documented
401 0 my $self = shift;
402 0 my @alternatives = @_;
403  
404 0 my $schema = $self->result_source->schema;
405  
406 0 my @resource_objects;
407 0 eval {
408 @resource_objects = map {
409 0 ▶ $schema->resultset('Resource')->search({ name => $_ })->first
Condition0%0/2
$schema->resultset('Resource')->search({'name', $_})->first || die("Resource $_ does not exist")
l!l
00
MC/DC0%0/1
$schema->resultset('Resource')->search({'name', $_})->first || die("Resource $_ does not exist")
$schema->resultset('Resource')->search({'name', $_})->first
Truth table0%0/2
A
A= $schema->resultset('Resource')->search({'name', $_})->first
Aresult
11
00
410 or die "Resource $_ does not exist";
411 } @alternatives;
412 0 1;
413 };
414  
415 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
416 0 return 0;
417 }
418  
419 0 return $self->add_requested_resource_by_id(map { $_->id } @resource_objects);
420 }
421  
422  
423 0 ▶sub add_requested_resource_by_id {
Subroutine add_requested_resource_by_id: not called CC=2 SCAR=17.9
Pod documented
424 0 my $self = shift;
425 0 my @resource_ids = @_;
426  
427 0 my $schema = $self->result_source->schema;
428  
429 0 my $request = $schema
430 ->resultset('TestrunRequestedResource')
431 ->create({ testrun_id => $self->id });
432  
433 0 foreach my $resource_id (@resource_ids) {
434 0 $schema
435 ->resultset('TestrunRequestedResourceAlternative')
436 ->create({ request_id => $request->id, resource_id => $resource_id });
437 }
438  
439 0 return 1;
440 }
441  
442 10 1;
443  
444 __END__
« Tapper/Schema/TestrunDB/Result/Precondition.pm ↑ Summary Tapper/Schema/TestrunDB/Result/TestrunScheduling.pm »