Bugzilla – Attachment 111116 Details for
Bug 22417
Add a task queue
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 22417: Add tests
Bug-22417-Add-tests.patch (text/plain), 5.83 KB, created by
Kyle M Hall (khall)
on 2020-10-02 11:11:59 UTC
(
hide
)
Description:
Bug 22417: Add tests
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2020-10-02 11:11:59 UTC
Size:
5.83 KB
patch
obsolete
>From 180cefb1f9aa7479fb562869f4bdc7bd86f227af Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Tue, 1 Sep 2020 11:01:25 +0200 >Subject: [PATCH] Bug 22417: Add tests > >Finally! > >Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> >--- > 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 <http://www.gnu.org/licenses>. >+ >+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 <http://www.gnu.org/licenses>. >+ >+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)
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 22417
:
105542
|
105543
|
107385
|
107386
|
107387
|
107388
|
107389
|
107390
|
107391
|
107392
|
107393
|
107394
|
107395
|
107396
|
107397
|
107398
|
107399
|
107400
|
107401
|
107402
|
107403
|
107404
|
107405
|
107406
|
107481
|
107482
|
107682
|
108023
|
108024
|
108025
|
108026
|
108027
|
108028
|
108029
|
108030
|
108031
|
108032
|
108033
|
108034
|
108035
|
108036
|
108037
|
108038
|
108039
|
108040
|
108041
|
108042
|
108043
|
108044
|
108045
|
108046
|
108047
|
109270
|
109271
|
109319
|
109320
|
109321
|
109322
|
109323
|
109324
|
109325
|
109326
|
109327
|
109328
|
109329
|
109330
|
109331
|
109332
|
109333
|
109334
|
109335
|
109336
|
109337
|
109338
|
109339
|
109340
|
109341
|
109342
|
109343
|
109344
|
109345
|
109350
|
109351
|
109355
|
109390
|
109391
|
109392
|
109393
|
109394
|
109395
|
109396
|
109397
|
109398
|
109399
|
109400
|
109401
|
109402
|
109403
|
109404
|
109405
|
109406
|
109407
|
109408
|
109409
|
109410
|
109411
|
109412
|
109413
|
109414
|
109415
|
109416
|
109417
|
109418
|
109474
|
109475
|
109476
|
109477
|
110150
|
111091
|
111092
|
111093
|
111094
|
111095
|
111096
|
111097
|
111098
|
111099
|
111100
|
111101
|
111102
|
111103
|
111104
|
111105
|
111106
|
111107
|
111108
|
111109
|
111110
|
111111
|
111112
|
111113
|
111114
|
111115
| 111116 |
111117
|
111118
|
111119
|
111120
|
111121
|
111238
|
111239
|
111245