diff --git a/Koha/BackgroundJob/MARCImportCommitBatch.pm b/Koha/BackgroundJob/MARCImportCommitBatch.pm index d37289d7b8..55743f139f 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 48b1d5f92d..c3074370d4 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; +};