From aacdf3c9c298fcae65ebb2dfbafeae695e270cd8 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 21 Oct 2025 15:40:21 +0000 Subject: [PATCH] Bug 40846: Improve handling of ignored records Currently, a jobis marked failed if there are no records added or updated. We should take ignored records into account. Additionally, I mark the job failed when the number of records processed doesn't equal the number of records in the bacth To test: 1 - Export some records from your Koha, include some with items 2 - Stage and import those records into Koha, matching on biblionumber, ignoring matcheds, updating items 3 - View the import job, it is marked failed 4 - View the batch, all records imported 5 - Apply patch, restart all 6 - Stage/import as before 7 - Job is now marked finished 8 - Records imported as before --- Koha/BackgroundJob/MARCImportCommitBatch.pm | 9 ++-- .../BackgroundJob/MARCImportCommitBatch.t | 41 ++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/Koha/BackgroundJob/MARCImportCommitBatch.pm b/Koha/BackgroundJob/MARCImportCommitBatch.pm index d37289d7b82..55743f139f9 100644 --- a/Koha/BackgroundJob/MARCImportCommitBatch.pm +++ b/Koha/BackgroundJob/MARCImportCommitBatch.pm @@ -84,11 +84,10 @@ sub process { progress_callback => sub { my $job_progress = shift; $self->progress($job_progress)->store }, } ); - my $count = $num_added + $num_updated; - if ($count) { - $self->set( { progress => $count, size => $count } ); - } else { # TODO Refine later - $self->set( { progress => 0, status => 'failed' } ); + my $count = $num_added + $num_updated + $num_ignored; + $self->set( { progress => $count } ); + if ( $count != $size ) { + $self->set( { status => 'failed' } ); } } catch { warn $_; diff --git a/t/db_dependent/Koha/BackgroundJob/MARCImportCommitBatch.t b/t/db_dependent/Koha/BackgroundJob/MARCImportCommitBatch.t index 48b1d5f92de..c3074370d40 100755 --- a/t/db_dependent/Koha/BackgroundJob/MARCImportCommitBatch.t +++ b/t/db_dependent/Koha/BackgroundJob/MARCImportCommitBatch.t @@ -17,8 +17,9 @@ use Modern::Perl; +use Test::MockModule; use Test::NoWarnings; -use Test::More tests => 2; +use Test::More tests => 3; use Koha::Database; use Koha::BackgroundJobs; @@ -51,3 +52,41 @@ subtest 'enqueue() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'process() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $batch_return = []; + my $output_module = Test::MockModule->new('Koha::BackgroundJob::MARCImportCommitBatch'); + $output_module->mock( + 'BatchCommitRecords', + sub { + return @{$batch_return}; + } + ); + my $import_batch = $builder->build_object( { class => 'Koha::ImportBatches' } ); + + # Add two records + $builder->build_object( { class => 'Koha::Import::Records', value => { import_batch_id => $import_batch->id } } ); + $builder->build_object( { class => 'Koha::Import::Records', value => { import_batch_id => $import_batch->id } } ); + + my $job_id = Koha::BackgroundJob::MARCImportCommitBatch->new->enqueue( { import_batch_id => $import_batch->id } ); + my $job = Koha::BackgroundJobs->find($job_id)->_derived_class; + + @{$batch_return} = ( 0, 0, 0, 0, 0, 2 ); + $job->process( { import_batch_id => $import_batch->id } ); + $job->discard_changes; + is( $job->status, 'finished', 'A job where all records are ignored is a success' ); + + $job->status('new')->store; + @{$batch_return} = ( 1, 1, 0, 0, 0, 1 ); + $job->process( { import_batch_id => $import_batch->id } ); + $job->discard_changes; + is( $job->status, 'failed', 'A job where the record counts do not match is failed' ); + is( $job->size, '2', 'Size is based on number of records' ); + is( $job->progress, '3', 'Progress is the total number of records processed, including added/updated/ignored' ); + $schema->storage->txn_rollback; +}; -- 2.39.5