From 0f9b5e519c3691aae1c18222d2925ac98590a95e 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 | 112 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index 024ec5dc8c..823232630e 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,115 @@ 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 $spent_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } }); + my $spent_invoice = $builder->build({ source => 'Aqinvoice'}); + my $spent_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'BOO' } }); + my $spent_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $spent_currency->{currency}, invoiceprice => $spent_currency->{currency} } }); + my $budget_authcat = $builder->build({ source => 'AuthorisedValueCategory', value => {} }); + my $spent_sort1 = $builder->build({ source => 'AuthorisedValue', value => { + category => $budget_authcat->{category_name}, + authorised_value => 'PICKLE', + } + }); + my $spent_budget_period = $builder->build({ source => 'Aqbudgetperiod', value => { + } + }); + my $spent_budget = $builder->build({ source => 'Aqbudget', value => { + sort1_authcat => $budget_authcat->{category_name}, + budget_period_id => $spent_budget_period->{budget_period_id}, + budget_parent_id => undef, + } + }); + my $orderinfo = { + basketno => $spent_basket->{basketno}, + booksellerid => $spent_vendor->{id}, + rrp => 16.99, + discount => 0, + ecost => 16.91, + biblionumber => $item_1->biblionumber, + currency => $spent_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 => $spent_budget->{budget_id}, + sort1 => $spent_sort1->{authorised_value}, + }; + + # 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 => $spent_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($spent_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($spent_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