Bug 30360: Add methods for dealing with the JSON payload

This patch adds high-level methods to handle the JSON payload
(de)serialization as needed. This allows devs implementing background
jobs to abstract themselves from the internals of the jobs handling.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/BackgroundJob.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2022-04-05 00:30:59 +02:00 committed by Martin Renvoize
parent 38fdd82d27
commit b13b4aaa4f
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
2 changed files with 45 additions and 2 deletions

View File

@ -234,6 +234,34 @@ sub finish {
)->store;
}
=head3 decoded_data
my $job_data = $self->decoded_data;
Returns the decoded JSON contents from $self->data.
=cut
sub decoded_data {
my ($self) = @_;
return decode_json($self->data);
}
=head3 set_encoded_data
$self->set_encoded_data( $data );
Serializes I<$data> as a JSON string and sets the I<data> attribute with it.
=cut
sub set_encoded_data {
my ( $self, $data ) = @_;
return $self->data( encode_json($data) );
}
=head3 job_type
Return the job type of the job. Must be a string.

View File

@ -17,14 +17,14 @@
use Modern::Perl;
use Test::More tests => 3;
use Test::More tests => 4;
use Test::Exception;
use Koha::Database;
use Koha::BackgroundJobs;
use Koha::BackgroundJob::BatchUpdateItem;
use JSON qw( decode_json );
use JSON qw( decode_json encode_json );
use t::lib::Mocks;
use t::lib::TestBuilder;
@ -152,3 +152,18 @@ subtest 'start(), step() and finish() tests' => sub {
$schema->storage->txn_rollback;
};
subtest 'decoded_data() and set_encoded_data() tests' => sub {
plan tests => 3;
my $job = Koha::BackgroundJob::BatchUpdateItem->new->set_encoded_data( undef );
is( $job->decoded_data, undef );
my $data = { some => 'data' };
$job->set_encoded_data( $data );
is_deeply( decode_json($job->data), $data );
is_deeply( $job->decoded_data, $data );
};