Lines 17-28
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 2; |
20 |
use Test::More tests => 3; |
|
|
21 |
use Test::Exception; |
21 |
|
22 |
|
22 |
use Koha::Database; |
23 |
use Koha::Database; |
23 |
use Koha::BackgroundJobs; |
24 |
use Koha::BackgroundJobs; |
24 |
use Koha::BackgroundJob::BatchUpdateItem; |
25 |
use Koha::BackgroundJob::BatchUpdateItem; |
25 |
|
26 |
|
|
|
27 |
use JSON qw( decode_json ); |
28 |
|
26 |
use t::lib::Mocks; |
29 |
use t::lib::Mocks; |
27 |
use t::lib::TestBuilder; |
30 |
use t::lib::TestBuilder; |
28 |
|
31 |
|
Lines 86-88
subtest 'enqueue() tests' => sub {
Link Here
|
86 |
|
89 |
|
87 |
$schema->storage->txn_rollback; |
90 |
$schema->storage->txn_rollback; |
88 |
}; |
91 |
}; |
89 |
- |
92 |
|
|
|
93 |
subtest 'start(), step() and finish() tests' => sub { |
94 |
|
95 |
plan tests => 19; |
96 |
|
97 |
$schema->storage->txn_begin; |
98 |
|
99 |
# FIXME: This all feels we need to do it better... |
100 |
my $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2 ] } ); |
101 |
my $job = Koha::BackgroundJobs->find($job_id)->_derived_class; |
102 |
|
103 |
is( $job->started_on, undef, 'started_on not set yet' ); |
104 |
is( $job->size, 2, 'Two steps' ); |
105 |
|
106 |
$job->start; |
107 |
|
108 |
isnt( $job->started_on, undef, 'started_on set' ); |
109 |
is( $job->status, 'started' ); |
110 |
is( $job->progress, 0, 'No progress yet' ); |
111 |
|
112 |
$job->step; |
113 |
is( $job->progress, 1, 'First step' ); |
114 |
$job->step; |
115 |
is( $job->progress, 2, 'Second step' ); |
116 |
throws_ok |
117 |
{ $job->step; } |
118 |
'Koha::Exceptions::BackgroundJob::StepOutOfBounds', |
119 |
'Tried to make a forbidden extra step'; |
120 |
|
121 |
is( $job->progress, 2, 'progress remains unchanged' ); |
122 |
|
123 |
my $data = { some => 'data' }; |
124 |
|
125 |
$job->status('cancelled')->store; |
126 |
$job->finish( $data ); |
127 |
|
128 |
is( $job->status, 'cancelled', "'finish' leaves 'cancelled' untouched" ); |
129 |
isnt( $job->ended_on, undef, 'ended_on set' ); |
130 |
is_deeply( decode_json( $job->data ), $data ); |
131 |
|
132 |
$job->status('started')->store; |
133 |
$job->finish( $data ); |
134 |
|
135 |
is( $job->status, 'finished' ); |
136 |
isnt( $job->ended_on, undef, 'ended_on set' ); |
137 |
is_deeply( decode_json( $job->data ), $data ); |
138 |
|
139 |
throws_ok |
140 |
{ $job->start; } |
141 |
'Koha::Exceptions::BackgroundJob::InconsistentStatus', |
142 |
'Exception thrown trying to start a finished job'; |
143 |
|
144 |
is( $@->expected_status, 'new' ); |
145 |
|
146 |
throws_ok |
147 |
{ $job->step; } |
148 |
'Koha::Exceptions::BackgroundJob::InconsistentStatus', |
149 |
'Exception thrown trying to start a finished job'; |
150 |
|
151 |
is( $@->expected_status, 'started' ); |
152 |
|
153 |
$schema->storage->txn_rollback; |
154 |
}; |