Bug 30728: Add unit tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 10 May 2022 17:17:37 +0000 (14:17 -0300)
committerFridolin Somers <fridolin.somers@biblibre.com>
Fri, 13 May 2022 08:17:46 +0000 (22:17 -1000)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
t/db_dependent/Biblio.t
t/db_dependent/Biblio_holdsqueue.t
t/db_dependent/Circulation_holdsqueue.t
t/db_dependent/Koha/BackgroundJobs/BatchDeleteBiblio.t
t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t
t/db_dependent/Koha/Hold.t
t/db_dependent/Koha/Item.t
t/db_dependent/Reserves.t

index 1ffa014..7afa3e2 100755 (executable)
@@ -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 );
index 5753a3c..4c687a6 100755 (executable)
@@ -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;
 };
index 3b542f9..61d8294 100755 (executable)
@@ -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;
 };
index b2d8049..60e60ed 100755 (executable)
@@ -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;
 };
index a3779f0..ee40088 100755 (executable)
@@ -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;
 };
index 9547e35..8c32bda 100755 (executable)
@@ -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,
             }
         }
     );
index c449b30..8be4d45 100755 (executable)
@@ -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;
     };
 };
index cfac978..5bda45c 100755 (executable)
@@ -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;
 };