From c1c9700c63a41f3662235c7bb174360af4b40277 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 12 Sep 2022 12:01:45 -0300 Subject: [PATCH] Bug 30982: Add Koha::BackgroundJobs->filter_by_current Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/BackgroundJobs.pm | 28 ++++++++++++++++++---- t/db_dependent/Koha/BackgroundJobs.t | 35 ++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/Koha/BackgroundJobs.pm b/Koha/BackgroundJobs.pm index 194123020d..dc88bc8c37 100644 --- a/Koha/BackgroundJobs.pm +++ b/Koha/BackgroundJobs.pm @@ -16,18 +16,18 @@ package Koha::BackgroundJobs; # along with Koha; if not, see . use Modern::Perl; -use base qw(Koha::Objects); + use Koha::BackgroundJob; +use base qw(Koha::Objects); + =head1 NAME Koha::BackgroundJobs - Koha BackgroundJob Object set class =head1 API -=head2 Class Methods - -=cut +=head2 Class methods =head2 search_limited @@ -57,6 +57,26 @@ sub search_limited { ->search( $params, $attributes ); } +=head3 filter_by_current + + my $current_jobs = $jobs->filter_by_current; + +Returns a new resultset, filtering out finished jobs. + +=cut + +sub filter_by_current { + my ($self) = @_; + + return $self->search( + { + status => { not_in => [ 'cancelled', 'failed', 'finished' ] } + } + ); +} + +=head2 Internal methods + =head3 _type =cut diff --git a/t/db_dependent/Koha/BackgroundJobs.t b/t/db_dependent/Koha/BackgroundJobs.t index 0f5bede989..5d1dc1c58a 100755 --- a/t/db_dependent/Koha/BackgroundJobs.t +++ b/t/db_dependent/Koha/BackgroundJobs.t @@ -19,9 +19,11 @@ use Modern::Perl; -use Test::More tests => 12; +use Test::More tests => 13; use Test::MockModule; +use List::MoreUtils qw(any); + use Koha::Database; use Koha::BackgroundJobs; use Koha::DateUtils qw( dt_from_string ); @@ -31,7 +33,8 @@ use t::lib::Mocks; use t::lib::Dates; use t::lib::Koha::BackgroundJob::BatchTest; -my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; +my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; t::lib::Mocks::mock_userenv; @@ -91,3 +94,31 @@ is_deeply( is_deeply( $new_job->additional_report(), {} ); $schema->storage->txn_rollback; + +subtest 'filter_by_current() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $job_new = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'new' } } ); + my $job_cancelled = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'cancelled' } } ); + my $job_failed = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'failed' } } ); + my $job_finished = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished' } } ); + + my $rs = Koha::BackgroundJobs->search( + { + id => [ $job_new->id, $job_cancelled->id, $job_failed->id, $job_finished->id ] + } + ); + + is( $rs->count, 4, '4 jobs in resultset' ); + ok( any {$_->status eq 'new'} @{$rs->as_list}, "There is a 'new' job" ); + + $rs = $rs->filter_by_current; + + is( $rs->count, 1, 'Only 1 job in filtered resultset' ); + is( $rs->next->status, 'new', "The only job in resultset is 'new'" ); + + $schema->storage->txn_rollback; +}; -- 2.39.5