From 1a71acc8ee68d1c871e2656f8327e9920100fe02 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 7 Sep 2022 12:29:52 +0000 Subject: [PATCH] Bug 31351: (QA follow-up) Adjust tests accordingly Test plan: Run t/db_dependent/Koha/BackgroundJob.t Run t/db_dependent/Koha/BackgroundJobs.t Prove t/db_dependent/Koha/BackgroundJobs Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 727193deade2795db350b84bec0ca2e28163124a) Signed-off-by: Lucas Gass --- t/db_dependent/Koha/BackgroundJob.t | 17 +++++++---------- t/db_dependent/Koha/BackgroundJobs.t | 3 +-- .../Koha/BackgroundJobs/BatchUpdateBiblio.t | 10 ++++------ t/lib/Koha/BackgroundJob/BatchTest.pm | 5 ++--- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/t/db_dependent/Koha/BackgroundJob.t b/t/db_dependent/Koha/BackgroundJob.t index 2c368383f5..848dbd50b7 100755 --- a/t/db_dependent/Koha/BackgroundJob.t +++ b/t/db_dependent/Koha/BackgroundJob.t @@ -25,8 +25,6 @@ use Koha::Database; use Koha::BackgroundJobs; use Koha::BackgroundJob::BatchUpdateItem; -use JSON qw( decode_json encode_json ); - use t::lib::Mocks; use t::lib::Mocks::Logger; use t::lib::TestBuilder; @@ -108,7 +106,7 @@ subtest 'enqueue() tests' => sub { is( $job->size, 3, 'Three steps' ); is( $job->status, 'new', 'Initial status set correctly' ); is( $job->borrowernumber, $patron->id, 'Borrowernumber set from userenv' ); - is_deeply( decode_json( $job->context ), $job_context, 'Context set from userenv + interface' ); + is_deeply( $job->json->decode( $job->context ), $job_context, 'Context set from userenv + interface' ); $schema->storage->txn_rollback; }; @@ -150,14 +148,14 @@ subtest 'start(), step() and finish() tests' => sub { is( $job->status, 'cancelled', "'finish' leaves 'cancelled' untouched" ); isnt( $job->ended_on, undef, 'ended_on set' ); - is_deeply( decode_json( $job->data ), $data ); + is_deeply( $job->json->decode( $job->data ), $data ); $job->status('started')->store; $job->finish( $data ); is( $job->status, 'finished' ); isnt( $job->ended_on, undef, 'ended_on set' ); - is_deeply( decode_json( $job->data ), $data ); + is_deeply( $job->json->decode( $job->data ), $data ); throws_ok { $job->start; } @@ -224,15 +222,14 @@ subtest 'process tests' => sub { is_deeply( C4::Context->interface, 'intranet', "Interface set from job context on process" ); # Manually add a job (->new->store) without context + my $json = $job->json; # sorry, quickly borrowing your json object + my $data = $json->encode({ a => 'a', b => 'b' }); my $incomplete_job = t::lib::Koha::BackgroundJob::BatchTest->new( { status => 'new', size => 1, borrowernumber => $patron->borrowernumber, type => 'batch_test', - data => encode_json { - a => 'a', - b => 'b', - }, + data => $data, } )->store; @@ -254,6 +251,6 @@ subtest 'decoded_data() and set_encoded_data() tests' => sub { $job->set_encoded_data( $data ); - is_deeply( decode_json($job->data), $data ); + is_deeply( $job->json->decode($job->data), $data ); is_deeply( $job->decoded_data, $data ); }; diff --git a/t/db_dependent/Koha/BackgroundJobs.t b/t/db_dependent/Koha/BackgroundJobs.t index df2172f00c..0f5bede989 100755 --- a/t/db_dependent/Koha/BackgroundJobs.t +++ b/t/db_dependent/Koha/BackgroundJobs.t @@ -21,7 +21,6 @@ use Modern::Perl; use Test::More tests => 12; use Test::MockModule; -use JSON qw( decode_json ); use Koha::Database; use Koha::BackgroundJobs; @@ -60,7 +59,7 @@ my $job_id = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue( # Enqueuing a new job my $new_job = Koha::BackgroundJobs->find($job_id); ok( $new_job, 'New job correctly enqueued' ); -is_deeply( decode_json( $new_job->data ), +is_deeply( $new_job->json->decode( $new_job->data ), $data, 'data retrieved and json encoded correctly' ); is( t::lib::Dates::compare( $new_job->enqueued_on, dt_from_string ), 0, 'enqueued_on correctly filled with now()' ); diff --git a/t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t b/t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t index 74ca9d50b5..aa9b0e49d5 100755 --- a/t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t +++ b/t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t @@ -21,7 +21,6 @@ use Modern::Perl; use Test::More tests => 1; use Test::MockModule; -use JSON qw( encode_json decode_json ); use Koha::Database; use Koha::BackgroundJobs; @@ -50,16 +49,15 @@ subtest "Exceptions must be stringified" => sub { size => 1, borrowernumber => $patron->borrowernumber, type => 'batch_biblio_record_modification', - data => encode_json { - record_ids => [ $biblio->biblionumber ], - } } - )->store; + ); + my $data = $job->json->encode({ record_ids => [ $biblio->biblionumber ] }); + $job->data( $data )->store; $job = Koha::BackgroundJobs->find( $job->id ); $job->process( { job_id => $job->id, record_ids => [ $biblio->biblionumber ] } ); - my $data = decode_json $job->get_from_storage->data; + $data = $job->json->decode( $job->get_from_storage->data ); is_deeply( $data->{messages}->[0], { diff --git a/t/lib/Koha/BackgroundJob/BatchTest.pm b/t/lib/Koha/BackgroundJob/BatchTest.pm index 5c0edd3ffb..1ae84ef4e9 100644 --- a/t/lib/Koha/BackgroundJob/BatchTest.pm +++ b/t/lib/Koha/BackgroundJob/BatchTest.pm @@ -16,7 +16,6 @@ package t::lib::Koha::BackgroundJob::BatchTest; # along with Koha; if not, see . use Modern::Perl; -use JSON qw( decode_json encode_json ); use Koha::BackgroundJobs; use Koha::DateUtils qw( dt_from_string ); @@ -57,12 +56,12 @@ sub process { $self->progress( ++$job_progress )->store; } - my $job_data = decode_json $self->data; + my $job_data = $self->json->decode($self->data); $job_data->{messages} = \@messages; $job_data->{report} = $report; $self->ended_on(dt_from_string) - ->data(encode_json $job_data); + ->data( $self->json->encode($job_data) ); $self->status('finished') if $self->status ne 'cancelled'; $self->store; } -- 2.39.5