From 180cefb1f9aa7479fb562869f4bdc7bd86f227af Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 1 Sep 2020 11:01:25 +0200 Subject: [PATCH] Bug 22417: Add tests Finally! Signed-off-by: Kyle M Hall --- t/db_dependent/Koha/BackgroundJobs.t | 83 +++++++++++++++++++++++++++ t/lib/Koha/BackgroundJob/BatchTest.pm | 81 ++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 t/db_dependent/Koha/BackgroundJobs.t create mode 100644 t/lib/Koha/BackgroundJob/BatchTest.pm diff --git a/t/db_dependent/Koha/BackgroundJobs.t b/t/db_dependent/Koha/BackgroundJobs.t new file mode 100644 index 0000000000..72280d059e --- /dev/null +++ b/t/db_dependent/Koha/BackgroundJobs.t @@ -0,0 +1,83 @@ +#!/usr/bin/perl + +# Copyright 2020 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 11; +use Test::MockModule; +use JSON qw( decode_json ); + +use Koha::Database; +use Koha::BackgroundJobs; +use Koha::DateUtils; + +use t::lib::TestBuilder; +use t::lib::Mocks; +use t::lib::Dates; +use t::lib::Koha::BackgroundJob::BatchTest; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +t::lib::Mocks::mock_userenv; + +my $net_stomp = Test::MockModule->new('Net::Stomp'); +$net_stomp->mock( 'send_with_receipt', sub { return 1 } ); + +my $data = { a => 'aaa', b => 'bbb' }; +my $job_size = 10; +my $job_id = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue( + { + size => $job_size, + %$data + } +); + +# 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 ), + $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()' ); +is( $new_job->size, $job_size, 'job size retrieved correctly' ); +is( $new_job->status, "new", 'job has not started yet, status is new' ); +is( $new_job->type, "batch_test", 'job type retrieved from ->job_type' ); + +# Test cancelled job +$new_job->status('cancelled')->store; +my $processed_job = + t::lib::Koha::BackgroundJob::BatchTest->process( { job_id => $new_job->id } ); +is( $processed_job, undef ); +$new_job->discard_changes; +is( $new_job->status, "cancelled", "A cancelled job has not been processed" ); + +# Test new job to process +$new_job->status('new')->store; +$new_job = + t::lib::Koha::BackgroundJob::BatchTest->process( { job_id => $new_job->id } ); +is( $new_job->status, "finished", 'job is new finished!' ); +is( scalar( $new_job->messages ), 10, '10 messages generated' ); +is_deeply( + $new_job->report, + { total_records => 10, total_success => 10 }, + 'Correct number of records processed' +); + +$schema->storage->txn_rollback; diff --git a/t/lib/Koha/BackgroundJob/BatchTest.pm b/t/lib/Koha/BackgroundJob/BatchTest.pm new file mode 100644 index 0000000000..6e74038651 --- /dev/null +++ b/t/lib/Koha/BackgroundJob/BatchTest.pm @@ -0,0 +1,81 @@ +package t::lib::Koha::BackgroundJob::BatchTest; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use JSON qw( encode_json decode_json ); + +use Koha::BackgroundJobs; +use Koha::DateUtils qw( dt_from_string ); + +use base 'Koha::BackgroundJob'; + +sub job_type { + return 'batch_test'; +} + +sub process { + my ( $self, $args ) = @_; + + my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + + if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + return; + } + + my $job_progress = 0; + $job->started_on(dt_from_string) + ->progress($job_progress) + ->status('started') + ->store; + + my $report = { + total_records => $job->size, + total_success => 0, + }; + my @messages; + for my $i ( 0 .. $job->size - 1 ) { + + last if $job->get_from_storage->status eq 'cancelled'; + + push @messages, { + type => 'success', + i => $i, + }; + $report->{total_success}++; + $job->progress( ++$job_progress )->store; + } + + my $job_data = decode_json $job->data; + $job_data->{messages} = \@messages; + $job_data->{report} = $report; + + $job->ended_on(dt_from_string) + ->data(encode_json $job_data); + $job->status('finished') if $job->status ne 'cancelled'; + $job->store; +} + +sub enqueue { + my ( $self, $args) = @_; + + $self->SUPER::enqueue({ + job_size => $args->{size}, + job_args => {a => $args->{a}, b => $args->{b}} + }); +} + +1; -- 2.24.1 (Apple Git-126)