From f44348140fd63db65757d4e9474f139e28e67eee Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 10 May 2022 14:17:37 -0300 Subject: [PATCH] Bug 30728: Add unit tests Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Biblio.t | 9 ++--- t/db_dependent/Biblio_holdsqueue.t | 24 +++++++++++++ t/db_dependent/Circulation_holdsqueue.t | 9 +++++ .../Koha/BackgroundJobs/BatchDeleteBiblio.t | 29 +++++++++++++++- .../Koha/BackgroundJobs/BatchDeleteItem.t | 34 +++++++++++++++++-- t/db_dependent/Koha/Hold.t | 29 ++++++++++++++++ t/db_dependent/Koha/Item.t | 6 ++++ t/db_dependent/Reserves.t | 27 +++++++++++++++ 8 files changed, 158 insertions(+), 9 deletions(-) diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index 1ffa0143cb..7afa3e29f4 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -251,8 +251,7 @@ sub run_tests { # roll back ES index changes. t::lib::Mocks::mock_preference('SearchEngine', 'Zebra'); - my $bgj_mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); - $bgj_mock->mock( 'enqueue', undef ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); my $isbn = '0590353403'; my $title = 'Foundation'; @@ -639,8 +638,7 @@ subtest 'IsMarcStructureInternal' => sub { subtest 'deletedbiblio_metadata' => sub { plan tests => 2; - my $bgj_mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); - $bgj_mock->mock( 'enqueue', undef ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, ''); my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber ); @@ -655,8 +653,7 @@ subtest 'DelBiblio' => sub { plan tests => 5; - my $bgj_mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); - $bgj_mock->mock( 'enqueue', undef ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio(MARC::Record->new, ''); my $deleted = C4::Biblio::DelBiblio( $biblionumber ); diff --git a/t/db_dependent/Biblio_holdsqueue.t b/t/db_dependent/Biblio_holdsqueue.t index 5753a3ca94..4c687a69ba 100755 --- a/t/db_dependent/Biblio_holdsqueue.t +++ b/t/db_dependent/Biblio_holdsqueue.t @@ -36,6 +36,8 @@ subtest 'ModBiblio() + holds_queue update tests' => sub { my $biblio = $builder->build_sample_biblio; + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); $mock->mock( 'enqueue', sub { my ( $self, $args ) = @_; @@ -69,6 +71,20 @@ subtest 'ModBiblio() + holds_queue update tests' => sub { $biblio->frameworkcode, { skip_holds_queue => 1 } ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + # this call should not trigger the mocked 'enqueue' + C4::Biblio::ModBiblio( + $biblio->metadata->record, $biblio->id, + $biblio->frameworkcode, { skip_holds_queue => 0 } + ); + + # this call shoul not trigger the mocked 'enqueue' + C4::Biblio::ModBiblio( + $biblio->metadata->record, $biblio->id, + $biblio->frameworkcode, { skip_holds_queue => 1 } + ); + $schema->storage->txn_rollback; }; @@ -78,6 +94,8 @@ subtest 'DelBiblio + holds_queue update tests' => sub { $schema->storage->txn_begin; + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + my $biblio = $builder->build_sample_biblio; my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); @@ -102,5 +120,11 @@ subtest 'DelBiblio + holds_queue update tests' => sub { C4::Biblio::DelBiblio( $biblio->id ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + $biblio = $builder->build_sample_biblio; + + C4::Biblio::DelBiblio( $biblio->id ); + $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/Circulation_holdsqueue.t b/t/db_dependent/Circulation_holdsqueue.t index 3b542f9913..61d8294076 100755 --- a/t/db_dependent/Circulation_holdsqueue.t +++ b/t/db_dependent/Circulation_holdsqueue.t @@ -42,6 +42,7 @@ subtest 'AddIssue() and AddReturn() real-time holds queue tests' => sub { t::lib::Mocks::mock_userenv({ branchcode => $library->id }); t::lib::Mocks::mock_preference( 'UpdateTotalIssuesOnCirc', 1 ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); my $action; @@ -62,5 +63,13 @@ subtest 'AddIssue() and AddReturn() real-time holds queue tests' => sub { $action = 'AddReturn'; AddReturn( $item->barcode ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + $action = 'AddIssue'; + AddIssue( $patron->unblessed, $item->barcode, ); + + $action = 'AddReturn'; + AddReturn( $item->barcode ); + $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/Koha/BackgroundJobs/BatchDeleteBiblio.t b/t/db_dependent/Koha/BackgroundJobs/BatchDeleteBiblio.t index b2d80491aa..60e60ed049 100755 --- a/t/db_dependent/Koha/BackgroundJobs/BatchDeleteBiblio.t +++ b/t/db_dependent/Koha/BackgroundJobs/BatchDeleteBiblio.t @@ -28,6 +28,7 @@ use Koha::Database; use Koha::BackgroundJob::BatchDeleteBiblio; use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; +use t::lib::Mocks; use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; @@ -35,7 +36,7 @@ my $builder = t::lib::TestBuilder->new; subtest "process() tests" => sub { - plan tests => 1; + plan tests => 2; $schema->storage->txn_begin; @@ -72,6 +73,8 @@ subtest "process() tests" => sub { } ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + $job->process( { record_ids => [ $biblio->id ], @@ -80,5 +83,29 @@ subtest "process() tests" => sub { is( $counter, 1, 'Holds queue update is enqueued only once' ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + $biblio = $builder->build_sample_biblio; + + $job = Koha::BackgroundJob::BatchDeleteBiblio->new( + { + status => 'new', + size => 1, + borrowernumber => undef, + type => 'batch_biblio_record_deletion', + data => encode_json { + record_ids => [ $biblio->id ], + } + } + ); + + $job->process( + { + record_ids => [ $biblio->id ], + } + ); + + is( $counter, 1, 'Counter untouched with RealTimeHoldsQueue disabled' ); + $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t b/t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t index a3779f0b1f..ee40088c8c 100755 --- a/t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t +++ b/t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t @@ -25,6 +25,7 @@ use JSON qw( encode_json ); use Koha::Database; use Koha::BackgroundJob::BatchDeleteItem; +use t::lib::Mocks; use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; @@ -32,7 +33,7 @@ my $builder = t::lib::TestBuilder->new; subtest "process() tests" => sub { - plan tests => 1; + plan tests => 2; $schema->storage->txn_begin; @@ -47,12 +48,14 @@ subtest "process() tests" => sub { $counter++; }); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + my $job = Koha::BackgroundJob::BatchDeleteItem->new( { status => 'new', size => 2, borrowernumber => undef, - type => 'batch_biblio_record_modification', + type => 'batch_item_record_deletion', data => encode_json { record_ids => [ $item_1->id, $item_2->id ], delete_biblios => 1, @@ -69,5 +72,32 @@ subtest "process() tests" => sub { is( $counter, 1, 'Holds queue update is enqueued only once' ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + $biblio = $builder->build_sample_biblio; + my $item = $builder->build_sample_item({ biblionumber => $biblio->id }); + + $job = Koha::BackgroundJob::BatchDeleteItem->new( + { + status => 'new', + size => 2, + borrowernumber => undef, + type => 'batch_item_record_deletion', + data => encode_json { + record_ids => [ $item->id ], + delete_biblios => 1, + } + } + ); + + $job->process( + { + record_ids => [ $item->id ], + delete_biblios => 1, + } + ); + + is( $counter, 1, 'Counter untouched with RealTimeHoldsQueue disabled' ); + $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t index 9547e353a2..8c32bda3f7 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -249,6 +249,19 @@ subtest 'fill() tests' => sub { ); } ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + + $builder->build_object( + { + class => 'Koha::Holds', + value => { + biblionumber => $biblio->id, + } + } + )->fill; + + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + # this call shouldn't add a new test $builder->build_object( { class => 'Koha::Holds', @@ -558,6 +571,8 @@ subtest 'cancel() tests' => sub { my $biblio = $builder->build_sample_biblio; + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); $mock->mock( 'enqueue', sub { my ( $self, $args ) = @_; @@ -586,6 +601,17 @@ subtest 'cancel() tests' => sub { } } )->cancel({ skip_holds_queue => 1 }); + + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + $builder->build_object( + { + class => 'Koha::Holds', + value => { + biblionumber => $biblio->id, + } + } + )->cancel({ skip_holds_queue => 0 }); }; $schema->storage->txn_rollback; @@ -600,6 +626,8 @@ subtest 'suspend_hold() and resume() tests' => sub { my $biblio = $builder->build_sample_biblio; my $action; + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); $mock->mock( 'enqueue', sub { my ( $self, $args ) = @_; @@ -615,6 +643,7 @@ subtest 'suspend_hold() and resume() tests' => sub { class => 'Koha::Holds', value => { biblionumber => $biblio->id, + found => undef, } } ); diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index c449b30032..8be4d4525c 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -1243,12 +1243,18 @@ subtest 'store() tests' => sub { ); } ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + # new item my $item = $builder->build_sample_item({ biblionumber => $biblio->id }); # updated item $item->set({ reserves => 1 })->store; + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + # updated item + $item->set({ reserves => 0 })->store; + $schema->storage->txn_rollback; }; }; diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index cfac978aeb..5bda45c4fa 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -961,6 +961,8 @@ subtest 'reserves.item_level_hold' => sub { ); } ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + # Revert the waiting status C4::Reserves::RevertWaitingStatus( { itemnumber => $item->itemnumber } ); @@ -969,6 +971,14 @@ subtest 'reserves.item_level_hold' => sub { is( $hold->itemnumber, $item->itemnumber, 'Itemnumber should not be removed when the waiting status is revert' ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + $hold->set_waiting; + + # Revert the waiting status, RealTimeHoldsQueue => shouldn't add a test + C4::Reserves::RevertWaitingStatus( + { itemnumber => $item->itemnumber } ); + $hold->delete; # cleanup }; @@ -1428,6 +1438,18 @@ subtest 'AddReserve() tests' => sub { ); } ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + + AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron->id, + biblionumber => $biblio->id, + } + ); + + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + AddReserve( { branchcode => $library->branchcode, @@ -1483,6 +1505,7 @@ subtest 'AlterPriorty() tests' => sub { ); } ); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); AlterPriority( "bottom", $reserve_id, 1, 2, 1, 3 ); @@ -1490,5 +1513,9 @@ subtest 'AlterPriorty() tests' => sub { is($hold->priority,3,'Successfully altered priority to bottom'); + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + AlterPriority( "bottom", $reserve_id, 1, 2, 1, 3 ); + $schema->storage->txn_rollback; }; -- 2.34.1