View | Details | Raw Unified | Return to bug 22417
Collapse All | Expand All

(-)a/t/db_dependent/Koha/BackgroundJobs.t (+83 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2020 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 11;
23
use Test::MockModule;
24
use JSON qw( decode_json );
25
26
use Koha::Database;
27
use Koha::BackgroundJobs;
28
use Koha::DateUtils;
29
30
use t::lib::TestBuilder;
31
use t::lib::Mocks;
32
use t::lib::Dates;
33
use t::lib::Koha::BackgroundJob::BatchTest;
34
35
my $schema = Koha::Database->new->schema;
36
$schema->storage->txn_begin;
37
38
t::lib::Mocks::mock_userenv;
39
40
my $net_stomp = Test::MockModule->new('Net::Stomp');
41
$net_stomp->mock( 'send_with_receipt', sub { return 1 } );
42
43
my $data     = { a => 'aaa', b => 'bbb' };
44
my $job_size = 10;
45
my $job_id   = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue(
46
    {
47
        size => $job_size,
48
        %$data
49
    }
50
);
51
52
# Enqueuing a new job
53
my $new_job = Koha::BackgroundJobs->find($job_id);
54
ok( $new_job, 'New job correctly enqueued' );
55
is_deeply( decode_json( $new_job->data ),
56
    $data, 'data retrieved and json encoded correctly' );
57
is( t::lib::Dates::compare( $new_job->enqueued_on, dt_from_string ),
58
    0, 'enqueued_on correctly filled with now()' );
59
is( $new_job->size,   $job_size,    'job size retrieved correctly' );
60
is( $new_job->status, "new",        'job has not started yet, status is new' );
61
is( $new_job->type,   "batch_test", 'job type retrieved from ->job_type' );
62
63
# Test cancelled job
64
$new_job->status('cancelled')->store;
65
my $processed_job =
66
  t::lib::Koha::BackgroundJob::BatchTest->process( { job_id => $new_job->id } );
67
is( $processed_job, undef );
68
$new_job->discard_changes;
69
is( $new_job->status, "cancelled", "A cancelled job has not been processed" );
70
71
# Test new job to process
72
$new_job->status('new')->store;
73
$new_job =
74
  t::lib::Koha::BackgroundJob::BatchTest->process( { job_id => $new_job->id } );
75
is( $new_job->status,             "finished", 'job is new finished!' );
76
is( scalar( $new_job->messages ), 10,         '10 messages generated' );
77
is_deeply(
78
    $new_job->report,
79
    { total_records => 10, total_success => 10 },
80
    'Correct number of records processed'
81
);
82
83
$schema->storage->txn_rollback;
(-)a/t/lib/Koha/BackgroundJob/BatchTest.pm (-1 / +81 lines)
Line 0 Link Here
0
- 
1
package t::lib::Koha::BackgroundJob::BatchTest;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use JSON qw( encode_json decode_json );
20
21
use Koha::BackgroundJobs;
22
use Koha::DateUtils qw( dt_from_string );
23
24
use base 'Koha::BackgroundJob';
25
26
sub job_type {
27
    return 'batch_test';
28
}
29
30
sub process {
31
    my ( $self, $args ) = @_;
32
33
    my $job = Koha::BackgroundJobs->find( $args->{job_id} );
34
35
    if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
36
        return;
37
    }
38
39
    my $job_progress = 0;
40
    $job->started_on(dt_from_string)
41
        ->progress($job_progress)
42
        ->status('started')
43
        ->store;
44
45
    my $report = {
46
        total_records => $job->size,
47
        total_success => 0,
48
    };
49
    my @messages;
50
    for my $i ( 0 .. $job->size - 1 ) {
51
52
        last if $job->get_from_storage->status eq 'cancelled';
53
54
        push @messages, {
55
            type => 'success',
56
            i => $i,
57
        };
58
        $report->{total_success}++;
59
        $job->progress( ++$job_progress )->store;
60
    }
61
62
    my $job_data = decode_json $job->data;
63
    $job_data->{messages} = \@messages;
64
    $job_data->{report} = $report;
65
66
    $job->ended_on(dt_from_string)
67
        ->data(encode_json $job_data);
68
    $job->status('finished') if $job->status ne 'cancelled';
69
    $job->store;
70
}
71
72
sub enqueue {
73
    my ( $self, $args) = @_;
74
75
    $self->SUPER::enqueue({
76
        job_size => $args->{size},
77
        job_args => {a => $args->{a}, b => $args->{b}}
78
    });
79
}
80
81
1;

Return to bug 22417