From a809c4d79d2d4f092bbfcadfb9958f2caac57733 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 9 Aug 2022 09:30:43 +0200 Subject: [PATCH] Bug 27421: Add tests Signed-off-by: Nick Clemens --- .../Koha/BackgroundJobs/StageMARCForImport.t | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 t/db_dependent/Koha/BackgroundJobs/StageMARCForImport.t diff --git a/t/db_dependent/Koha/BackgroundJobs/StageMARCForImport.t b/t/db_dependent/Koha/BackgroundJobs/StageMARCForImport.t new file mode 100644 index 0000000000..7818807830 --- /dev/null +++ b/t/db_dependent/Koha/BackgroundJobs/StageMARCForImport.t @@ -0,0 +1,132 @@ +#!/usr/bin/perl + +# Copyright 2022 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 . + +use Modern::Perl; + +use Test::More tests => 3; +use Test::MockModule; +use JSON qw( encode_json decode_json ); + +use Koha::Database; +use Koha::BackgroundJobs; +use Koha::BackgroundJob::StageMARCForImport; +use Koha::BackgroundJob::MARCImportCommitBatch; +use Koha::BackgroundJob::MARCImportRevertBatch; +use Koha::Import::Records; +use Koha::Exception; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; + +my $builder = t::lib::TestBuilder->new; + +$schema->storage->txn_begin; + +my $biblio = $builder->build_sample_biblio; +my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); +my $import_batch_id; + +subtest 'StageMARCForImport' => sub { + + plan tests => 4; + + my $job = Koha::BackgroundJob::StageMARCForImport->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'stage_marc_for_import', + } + )->store; + $job = Koha::BackgroundJobs->find( $job->id ); + $job->process( + { + job_id => $job->id, + record_type => 'biblio', + encoding => 'UTF-8', + format => 'ISO2709', + filepath => 't/db_dependent/data/marc21/zebraexport/biblio/exported_records', + filename => 'some_records', + parse_items => 1, + } + ); + + my $report = decode_json($job->get_from_storage->data)->{report}; + is( $report->{num_items}, 138 ); + is( $report->{staged}, 178 ); + is( $report->{total}, 179 ); + is( $report->{import_errors}, 1 ); + $import_batch_id = $report->{import_batch_id}; + +}; + +subtest 'MARCImportCommitBatch' => sub { + + plan tests => 2; + + my $job = Koha::BackgroundJob::MARCImportCommitBatch->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'marc_import_commit_batch' + } + )->store; + $job = Koha::BackgroundJobs->find( $job->id ); + $job->process( + { + job_id => $job->id, + import_batch_id => $import_batch_id, + frameworkcode => q{}, + } + ); + + my $report = decode_json( $job->get_from_storage->data )->{report}; + is( $report->{num_added}, 178 ); + is( $report->{num_items_added}, 138 ); + +}; + +subtest 'MARCImportRevertBatch' => sub { + + plan tests => 2; + + my $job = Koha::BackgroundJob::MARCImportRevertBatch->new( + { + status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'marc_import_revert_batch' + } + )->store; + $job = Koha::BackgroundJobs->find( $job->id ); + $job->process( + { + job_id => $job->id, + import_batch_id => $import_batch_id, + } + ); + + my $report = decode_json( $job->get_from_storage->data )->{report}; + is( $report->{num_deleted}, 178 ); + is( $report->{num_items_deleted}, 138 ); + +}; + +$schema->storage->txn_rollback; -- 2.30.2