From 897d8c245b2551c332ee2a892790a9e4b629a78f Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 25 Nov 2020 14:10:34 +0000 Subject: [PATCH] Bug 27079: Unit tests Add a unit test to check for floating point errors in UpdateFine relating to catching matching CalcFine amounts compared to DB amount values. Test plan 1/ Run the unit test and confirm it fails 2/ Apply the second patch in the series 3/ Run the unit test and confirm it passes Signed-off-by: Josef Moravec --- t/db_dependent/Overdues.t | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Overdues.t b/t/db_dependent/Overdues.t index e270c52506..f58d1bb0eb 100755 --- a/t/db_dependent/Overdues.t +++ b/t/db_dependent/Overdues.t @@ -132,7 +132,7 @@ $schema->storage->txn_rollback; subtest 'UpdateFine tests' => sub { - plan tests => 70; + plan tests => 74; $schema->storage->txn_begin; @@ -430,7 +430,7 @@ subtest 'UpdateFine tests' => sub { is( $fine3->amount+0, 30, "Third fine reduced" ); is( $fine3->amountoutstanding+0, 10, "Third fine amount outstanding is reduced" ); - # Ensure calculations work correctly for floats (bug #25127) + # Ensure maxfine calculations work correctly for floats (bug #25127) # 7.2 (maxfine) - 7.2 (total_amount_other) != 8.88178419700125e-16 (😢) t::lib::Mocks::mock_preference( 'MaxFine', '7.2' ); my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } ); @@ -503,5 +503,38 @@ subtest 'UpdateFine tests' => sub { is( $fines->count, 4, "New amount should be 0 so no fine added" ); ok( C4::Circulation::AddReturn( $item_1->barcode, $item_1->homebranch, 1), "Returning the item and forgiving fines succeeds"); + t::lib::Mocks::mock_preference( 'MaxFine', 0 ); + + # Ensure CalcFine calculations work correctly for floats (bug #27079) + # 1.800000 (amount from database) != 1.8~ (CalcFine of 0.15cents * 12units) (😢) + my $amount = 0.15 * 12; + UpdateFine( + { + issue_id => $checkout_2->issue_id, + itemnumber => $item_2->itemnumber, + borrowernumber => $patron_1->borrowernumber, + amount => $amount, + due => $checkout_2->date_due + } + ); + $fine = Koha::Account::Lines->search({ issue_id => $checkout_2->issue_id })->single; + ok( $fine, 'Fine added for checkout 2'); + is( $fine->amount, "1.800000", "Fine amount is 1.800000 as expected"); + + $fine->amountoutstanding(0)->store; + $fine->discard_changes; + is( $fine->amountoutstanding + 0, 0, "Fine was paid off"); + UpdateFine( + { + issue_id => $checkout_2->issue_id, + itemnumber => $item_2->itemnumber, + borrowernumber => $patron_1->borrowernumber, + amount => $amount, + due => $checkout_2->date_due + } + ); + my $refunds = Koha::Account::Lines->search({ itemnumber => $item_2->itemnumber, credit_type_code => 'OVERPAYMENT' }); + is( $refunds->count, 0, "Overpayment refund not added when the amounts are equal" ); + $schema->storage->txn_rollback; }; -- 2.11.0