From 9494010f4f0e96afaec7ff201b50a9451731b358 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Tue, 21 Feb 2023 17:48:02 +1300 Subject: [PATCH] Bug 31631: Unit tests Test plan: 1. Run tests in koha-shell sudo koha-shell prove t/db_dependent/Budgets.t Sponsored-by: Waikato Institute of Technology, New Zealand --- t/db_dependent/Budgets.t | 102 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index 024ec5dc8c..0d76984ae9 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -1,6 +1,6 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 154; +use Test::More tests => 155; use JSON; BEGIN { @@ -30,6 +30,7 @@ my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; +$dbh->do(q|DELETE FROM vendor_edi_accounts|); $dbh->do(q|DELETE FROM aqbudgetperiods|); $dbh->do(q|DELETE FROM aqbudgets|); @@ -1208,6 +1209,105 @@ subtest 'GetBudgetSpent GetBudgetOrdered GetBudgetsPlanCell tests' => sub { is ( @$gbh[0]->{budget_spent}+0, 78.8, "We expect this to be a rounded order cost * quantity"); is ( @$gbh[0]->{budget_ordered}, 78.8, "We expect this to be a rounded order cost * quantity"); + $schema->storage->txn_rollback; +}; + + +subtest 'FieldsForCalculatingFundValues tests' => sub { + plan tests => 10; + + $schema->storage->txn_begin; + + # Test FieldsForCalculatingFundValues() + t::lib::Mocks::mock_preference('CalculateFundValuesIncludingTax', '1'); + my ( $unitprice_field, $ecost_field ) = C4::Budgets->FieldsForCalculatingFundValues(); + is ( $unitprice_field, 'unitprice_tax_included', "We expect this to be unitprice_tax_included" ); + is ( $ecost_field, 'ecost_tax_included', "We expect this to be ecost_tax_included" ); + t::lib::Mocks::mock_preference('CalculateFundValuesIncludingTax', '0'); + ( $unitprice_field, $ecost_field ) = C4::Budgets->FieldsForCalculatingFundValues(); + is ( $unitprice_field, 'unitprice_tax_excluded', "We expect this to be unitprice_tax_excluded" ); + is ( $ecost_field, 'ecost_tax_excluded', "We expect this to be ecost_tax_excluded" ); + + # Build an order + t::lib::Mocks::mock_preference('OrderPriceRounding','nearest_cent'); + my $item_1 = $builder->build_sample_item; + my $basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } }); + my $invoice = $builder->build({ source => 'Aqinvoice'}); + my $currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'BOO' } }); + my $vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $currency->{currency}, invoiceprice => $currency->{currency} } }); + my $budget_period = $builder->build({ source => 'Aqbudgetperiod' }); + my $budget = $builder->build({ source => 'Aqbudget', value => { + budget_period_id => $budget_period->{budget_period_id}, + budget_parent_id => undef, + } + }); + my $orderinfo = { + basketno => $basket->{basketno}, + booksellerid => $vendor->{id}, + rrp => 16.99, + discount => 0, + ecost => 16.91, + biblionumber => $item_1->biblionumber, + currency => $currency->{currency}, + tax_rate_on_ordering => 0.15, + tax_value_on_ordering => 0, + tax_rate_on_receiving => 0.15, + tax_value_on_receiving => 0, + quantity => 8, + quantityreceived => 0, + datecancellationprinted => undef, + datereceived => undef, + budget_id => $budget->{budget_id}, + }; + + # Do some maths + my $ordered_order = Koha::Acquisition::Order->new($orderinfo); + $ordered_order->populate_with_prices_for_ordering(); + my $ordered_orderinfo = $ordered_order->unblessed(); + + # Place the order + $ordered_order = $builder->build({ source => 'Aqorder', value => $ordered_orderinfo }); + + # Check order values with different CalculateFundValuesIncludingTax options + t::lib::Mocks::mock_preference('CalculateFundValuesIncludingTax', '0'); + my $budget_ordered = GetBudgetOrdered( $ordered_order->{budget_id} ); + is ( $budget_ordered, 135.92, "We expect this to be the tax exclusive value" ); + t::lib::Mocks::mock_preference('CalculateFundValuesIncludingTax', '1'); + $budget_ordered = GetBudgetOrdered( $ordered_order->{budget_id} ); + is ( $budget_ordered, 156.32, "We expect this to be the tax inclusive value" ); + + # Receive the order + $orderinfo->{unitprice} = 9.85; #we are paying what we expected + + # Do some maths + my $spent_order = Koha::Acquisition::Order->new($orderinfo); + $spent_order->populate_with_prices_for_receiving(); + my $spent_orderinfo = $spent_order->unblessed(); + my $received_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo }); + + # Receive a copy of the order + ModReceiveOrder({ + biblionumber => $spent_orderinfo->{biblionumber}, + order => $received_order, + invoice => $invoice, + quantityreceived => 8, + budget_id => $spent_orderinfo->{budget_id}, + received_items => [], + }); + + t::lib::Mocks::mock_preference('CalculateFundValuesIncludingTax', '0'); + my $spent_spent = GetBudgetSpent( $spent_orderinfo->{budget_id} ); + is ( $spent_spent, 78.8, "We expect this to be the tax exclusive value" ); + my $gbh = GetBudgetHierarchy($budget->{budget_period_id}); + is ( @$gbh[0]->{budget_spent}, 78.8, "We expect this value to be the tax exclusive value"); + + t::lib::Mocks::mock_preference('CalculateFundValuesIncludingTax', '1'); + $spent_spent = GetBudgetSpent( $spent_orderinfo->{budget_id} ); + is ( $spent_spent, 90.64 , "We expect this to be the tax inclusive value" ); + $gbh = GetBudgetHierarchy($budget->{budget_period_id}); + is ( @$gbh[0]->{budget_spent}, 90.64, "We expect this value to be the tax inclusive value"); + + $schema->storage->txn_rollback; }; sub _get_dependencies { -- 2.20.1