From a2c82e1ab64d64f208459ad7b50e62b4291ec2a9 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 22 Dec 2021 15:20:16 +0000 Subject: [PATCH] Bug 29754: Unit test This adds a unit test to the SIP code It also adds some spacing and dividers to make the tests for 'relationships_debt' easier to read To test: prove -v t/db_dependent/Koha/Patron.t - passes and is readable prove -v t/db_dependent/SIP/Patron.t - fails Signed-off-by: Martin Renvoize --- t/db_dependent/Koha/Patron.t | 11 +++++----- t/db_dependent/SIP/Patron.t | 39 +++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 8d492168d7..37fe4f8399 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -89,8 +89,8 @@ subtest 'relationships_debt() tests' => sub { my $parent_1 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Parent 1" } }); my $parent_2 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Parent 2" } }); - my $child_1 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Child 1" } }); - my $child_2 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Child 2" } }); + my $child_1 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => " Child 1" } }); + my $child_2 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => " Child 2" } }); $child_1->add_guarantor({ guarantor_id => $parent_1->borrowernumber, relationship => 'parent' }); $child_1->add_guarantor({ guarantor_id => $parent_2->borrowernumber, relationship => 'parent' }); @@ -134,7 +134,7 @@ subtest 'relationships_debt() tests' => sub { sub _test_combinations { my ( $patrons, $parent1_debt, $parent2_debt, $child1_debt, $child2_debt ) = @_; - + diag("Testing with parent 1 debt $parent1_debt | Parent 2 debt $parent2_debt | Child 1 debt $child1_debt | Child 2 debt $child2_debt"); # Options # P1 => P1 + C1 + C2 ( - P1 ) ( + P2 ) # P2 => P2 + C1 + C2 ( - P2 ) ( + P1 ) @@ -145,6 +145,7 @@ sub _test_combinations { for my $i ( 0 .. 7 ) { my ( $only_this_guarantor, $include_guarantors, $include_this_patron ) = split '', sprintf( "%03b", $i ); + diag("---------------------"); for my $patron ( @$patrons ) { if ( $only_this_guarantor && !$patron->guarantee_relationships->count ) @@ -175,7 +176,7 @@ sub _test_combinations { $debt += $child1_debt + $child2_debt; $debt += $parent1_debt unless ($only_this_guarantor || !$include_guarantors); } - elsif ( $patron->firstname eq 'Child 1' ) { + elsif ( $patron->firstname eq ' Child 1' ) { $debt += $child1_debt if ($include_this_patron); $debt += $child2_debt; $debt += $parent1_debt + $parent2_debt if ($include_guarantors); @@ -196,7 +197,7 @@ sub _test_combinations { ), $debt, $patron->firstname - . " debt of $debt calculated correctly for ( only_this_guarantor: $only_this_guarantor, include_guarantors: $include_guarantors, include_this_patron: $include_this_patron)" + . " debt of " . sprintf('%02d',$debt) . " calculated correctly for ( only_this_guarantor: $only_this_guarantor, include_guarantors: $include_guarantors, include_this_patron: $include_this_patron)" ); } } diff --git a/t/db_dependent/SIP/Patron.t b/t/db_dependent/SIP/Patron.t index 9106b1e739..f0a1f26dce 100755 --- a/t/db_dependent/SIP/Patron.t +++ b/t/db_dependent/SIP/Patron.t @@ -4,7 +4,7 @@ # This needs to be extended! Your help is appreciated.. use Modern::Perl; -use Test::More tests => 8; +use Test::More tests => 9; use t::lib::Mocks; use t::lib::TestBuilder; @@ -272,4 +272,41 @@ subtest "fine_items tests" => sub { is( @$fine_items, 0, "Got zero fine items" ); }; +subtest "NoIssuesChargeGuarantorsWithGuarantees tests" => sub { + + plan tests => 1; + + t::lib::Mocks::mock_preference( 'borrowerRelationship', 'parent' ); + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $child = $builder->build_object({ class => 'Koha::Patrons' }); + $child->add_guarantor({ guarantor_id => $patron->borrowernumber, relationship => 'parent' }); + + t::lib::Mocks::mock_preference('NoIssuesChargeGuarantorsWithGuarantees', 1); + + my $fee1 = $builder->build_object( + { + class => 'Koha::Account::Lines', + value => { + borrowernumber => $patron->borrowernumber, + amountoutstanding => 11, + } + } + )->store; + + my $fee2 = $builder->build_object( + { + class => 'Koha::Account::Lines', + value => { + borrowernumber => $child->borrowernumber, + amountoutstanding => 0.11, + } + } + )->store; + + my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber ); + + is( $sip_patron->fines_amount, 11.11,"Guarantee fines correctly included"); + +}; $schema->storage->txn_rollback; -- 2.20.1