From a385ecee859ae5b855da0f177015d1ca8afe0a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nyl=C3=A9n?= Date: Thu, 27 Oct 2022 10:47:56 +0200 Subject: [PATCH] Bug 31969: (follow-up) Unit tests for Koha::BackgroundJobs->purge Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/BackgroundJobs.t | 67 +++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/BackgroundJobs.t b/t/db_dependent/Koha/BackgroundJobs.t index cfb9c28cc7..ed4fd8d9fb 100755 --- a/t/db_dependent/Koha/BackgroundJobs.t +++ b/t/db_dependent/Koha/BackgroundJobs.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 14; +use Test::More tests => 15; use Test::MockModule; use List::MoreUtils qw(any); @@ -140,3 +140,68 @@ subtest 'search_limited' => sub { $schema->storage->txn_rollback; }; + +subtest 'purge' => sub { + plan tests => 9; + $schema->storage->txn_begin; + + my $recent_date = dt_from_string; + my $old_date = dt_from_string->subtract({ days => 3 }); + my $job_recent_t1_new = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'new', ended_on => $old_date, type => 'type1' } } ); + my $job_recent_t2_fin = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $recent_date, type => 'type2' } } ); + my $job_old_t1_fin = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $old_date, type => 'type1' } } ); + my $job_old_t2_fin = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $old_date, type => 'type2' } } ); + + my $params = { job_types => ['type1'] , # Arrayref of jobtypes to be purged + days => 1, # Age in days of jobs to be purged + confirm => 0, # Confirm deletion + }; + is( Koha::BackgroundJobs->purge($params), 1, 'Only the old finished type1 job would be purged' ); + + $params->{'job_types'} = ['all']; + is( Koha::BackgroundJobs->purge($params), 2, 'All finished old jobs would be purged with job_types = all' ); + + my $rs = Koha::BackgroundJobs->search( + { + id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ] + } + ); + is( $rs->count, 4, 'All jobs still left in queue'); + + $params->{'job_types'} = ['type1']; + $params->{'confirm'} = 1; + is( Koha::BackgroundJobs->purge($params), 1, 'Only the old finished type1 job is purged' ); + + $rs = Koha::BackgroundJobs->search( + { + id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ] + } + ); + is( $rs->count, 3, '3 jobs still left in queue'); + + $params->{'job_types'} = ['all']; + is( Koha::BackgroundJobs->purge($params), 1, 'The remaining old finished jobs is purged' ); + $rs = Koha::BackgroundJobs->search( + { + id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ] + } + ); + is( $rs->count, 2, '2 jobs still left in queue'); + + $rs = Koha::BackgroundJobs->search( + { + id => [ $job_recent_t1_new->id ] + } + ); + is( $rs->count, 1, 'Unfinished job still left in queue'); + + $rs = Koha::BackgroundJobs->search( + { + id => [ $job_recent_t2_fin->id ] + } + ); + is( $rs->count, 1, 'Recent finished job still left in queue'); + + $schema->storage->txn_rollback; + +}; -- 2.34.1