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

(-)a/t/db_dependent/Koha/BackgroundJob.t (-10 / +7 lines)
Lines 25-32 use Koha::Database; Link Here
25
use Koha::BackgroundJobs;
25
use Koha::BackgroundJobs;
26
use Koha::BackgroundJob::BatchUpdateItem;
26
use Koha::BackgroundJob::BatchUpdateItem;
27
27
28
use JSON qw( decode_json encode_json );
29
30
use t::lib::Mocks;
28
use t::lib::Mocks;
31
use t::lib::Mocks::Logger;
29
use t::lib::Mocks::Logger;
32
use t::lib::TestBuilder;
30
use t::lib::TestBuilder;
Lines 108-114 subtest 'enqueue() tests' => sub { Link Here
108
    is( $job->size,           3,           'Three steps' );
106
    is( $job->size,           3,           'Three steps' );
109
    is( $job->status,         'new',       'Initial status set correctly' );
107
    is( $job->status,         'new',       'Initial status set correctly' );
110
    is( $job->borrowernumber, $patron->id, 'Borrowernumber set from userenv' );
108
    is( $job->borrowernumber, $patron->id, 'Borrowernumber set from userenv' );
111
    is_deeply( decode_json( $job->context ), $job_context, 'Context set from userenv + interface' );
109
    is_deeply( $job->json->decode( $job->context ), $job_context, 'Context set from userenv + interface' );
112
110
113
    $schema->storage->txn_rollback;
111
    $schema->storage->txn_rollback;
114
};
112
};
Lines 150-163 subtest 'start(), step() and finish() tests' => sub { Link Here
150
148
151
    is( $job->status, 'cancelled', "'finish' leaves 'cancelled' untouched" );
149
    is( $job->status, 'cancelled', "'finish' leaves 'cancelled' untouched" );
152
    isnt( $job->ended_on, undef, 'ended_on set' );
150
    isnt( $job->ended_on, undef, 'ended_on set' );
153
    is_deeply( decode_json( $job->data ), $data );
151
    is_deeply( $job->json->decode( $job->data ), $data );
154
152
155
    $job->status('started')->store;
153
    $job->status('started')->store;
156
    $job->finish( $data );
154
    $job->finish( $data );
157
155
158
    is( $job->status, 'finished' );
156
    is( $job->status, 'finished' );
159
    isnt( $job->ended_on, undef, 'ended_on set' );
157
    isnt( $job->ended_on, undef, 'ended_on set' );
160
    is_deeply( decode_json( $job->data ), $data );
158
    is_deeply( $job->json->decode( $job->data ), $data );
161
159
162
    throws_ok
160
    throws_ok
163
        { $job->start; }
161
        { $job->start; }
Lines 224-238 subtest 'process tests' => sub { Link Here
224
    is_deeply( C4::Context->interface, 'intranet', "Interface set from job context on process" );
222
    is_deeply( C4::Context->interface, 'intranet', "Interface set from job context on process" );
225
223
226
    # Manually add a job (->new->store) without context
224
    # Manually add a job (->new->store) without context
225
    my $json = $job->json; # sorry, quickly borrowing your json object
226
    my $data = $json->encode({ a => 'a', b => 'b' });
227
    my $incomplete_job = t::lib::Koha::BackgroundJob::BatchTest->new(
227
    my $incomplete_job = t::lib::Koha::BackgroundJob::BatchTest->new(
228
        {   status         => 'new',
228
        {   status         => 'new',
229
            size           => 1,
229
            size           => 1,
230
            borrowernumber => $patron->borrowernumber,
230
            borrowernumber => $patron->borrowernumber,
231
            type           => 'batch_test',
231
            type           => 'batch_test',
232
            data           => encode_json {
232
            data           => $data,
233
                a => 'a',
234
                b => 'b',
235
            },
236
        }
233
        }
237
    )->store;
234
    )->store;
238
235
Lines 254-259 subtest 'decoded_data() and set_encoded_data() tests' => sub { Link Here
254
251
255
    $job->set_encoded_data( $data );
252
    $job->set_encoded_data( $data );
256
253
257
    is_deeply( decode_json($job->data), $data );
254
    is_deeply( $job->json->decode($job->data), $data );
258
    is_deeply( $job->decoded_data, $data );
255
    is_deeply( $job->decoded_data, $data );
259
};
256
};
(-)a/t/db_dependent/Koha/BackgroundJobs.t (-2 / +1 lines)
Lines 21-27 use Modern::Perl; Link Here
21
21
22
use Test::More tests => 12;
22
use Test::More tests => 12;
23
use Test::MockModule;
23
use Test::MockModule;
24
use JSON qw( decode_json );
25
24
26
use Koha::Database;
25
use Koha::Database;
27
use Koha::BackgroundJobs;
26
use Koha::BackgroundJobs;
Lines 60-66 my $job_id = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue( Link Here
60
# Enqueuing a new job
59
# Enqueuing a new job
61
my $new_job = Koha::BackgroundJobs->find($job_id);
60
my $new_job = Koha::BackgroundJobs->find($job_id);
62
ok( $new_job, 'New job correctly enqueued' );
61
ok( $new_job, 'New job correctly enqueued' );
63
is_deeply( decode_json( $new_job->data ),
62
is_deeply( $new_job->json->decode( $new_job->data ),
64
    $data, 'data retrieved and json encoded correctly' );
63
    $data, 'data retrieved and json encoded correctly' );
65
is( t::lib::Dates::compare( $new_job->enqueued_on, dt_from_string ),
64
is( t::lib::Dates::compare( $new_job->enqueued_on, dt_from_string ),
66
    0, 'enqueued_on correctly filled with now()' );
65
    0, 'enqueued_on correctly filled with now()' );
(-)a/t/db_dependent/Koha/BackgroundJobs/BatchUpdateBiblio.t (-6 / +4 lines)
Lines 21-27 use Modern::Perl; Link Here
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 1;
23
use Test::MockModule;
23
use Test::MockModule;
24
use JSON qw( encode_json decode_json );
25
24
26
use Koha::Database;
25
use Koha::Database;
27
use Koha::BackgroundJobs;
26
use Koha::BackgroundJobs;
Lines 50-65 subtest "Exceptions must be stringified" => sub { Link Here
50
            size           => 1,
49
            size           => 1,
51
            borrowernumber => $patron->borrowernumber,
50
            borrowernumber => $patron->borrowernumber,
52
            type           => 'batch_biblio_record_modification',
51
            type           => 'batch_biblio_record_modification',
53
            data           => encode_json {
54
                record_ids => [ $biblio->biblionumber ],
55
            }
56
        }
52
        }
57
    )->store;
53
    );
54
    my $data = $job->json->encode({ record_ids => [ $biblio->biblionumber ] });
55
    $job->data( $data )->store;
58
    $job = Koha::BackgroundJobs->find( $job->id );
56
    $job = Koha::BackgroundJobs->find( $job->id );
59
    $job->process(
57
    $job->process(
60
        { job_id => $job->id, record_ids => [ $biblio->biblionumber ] } );
58
        { job_id => $job->id, record_ids => [ $biblio->biblionumber ] } );
61
59
62
    my $data = decode_json $job->get_from_storage->data;
60
    $data = $job->json->decode( $job->get_from_storage->data );
63
    is_deeply(
61
    is_deeply(
64
        $data->{messages}->[0],
62
        $data->{messages}->[0],
65
        {
63
        {
(-)a/t/lib/Koha/BackgroundJob/BatchTest.pm (-4 / +2 lines)
Lines 16-22 package t::lib::Koha::BackgroundJob::BatchTest; Link Here
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
use JSON qw( decode_json encode_json );
20
19
21
use Koha::BackgroundJobs;
20
use Koha::BackgroundJobs;
22
use Koha::DateUtils qw( dt_from_string );
21
use Koha::DateUtils qw( dt_from_string );
Lines 57-68 sub process { Link Here
57
        $self->progress( ++$job_progress )->store;
56
        $self->progress( ++$job_progress )->store;
58
    }
57
    }
59
58
60
    my $job_data = decode_json $self->data;
59
    my $job_data = $self->json->decode($self->data);
61
    $job_data->{messages} = \@messages;
60
    $job_data->{messages} = \@messages;
62
    $job_data->{report} = $report;
61
    $job_data->{report} = $report;
63
62
64
    $self->ended_on(dt_from_string)
63
    $self->ended_on(dt_from_string)
65
        ->data(encode_json $job_data);
64
        ->data( $self->json->encode($job_data) );
66
    $self->status('finished') if $self->status ne 'cancelled';
65
    $self->status('finished') if $self->status ne 'cancelled';
67
    $self->store;
66
    $self->store;
68
}
67
}
69
- 

Return to bug 31351