From cb1a7af5ce120f8aac7dbd94583ee7df5a3fe2dc Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Fri, 14 Jul 2023 12:52:43 +0000 Subject: [PATCH] Bug 34277: Add unit test prove t/db_dependent/api/v1/patrons_accounts --- t/db_dependent/api/v1/patrons_accounts.t | 173 ++++++++++++++++++++++- 1 file changed, 172 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/api/v1/patrons_accounts.t b/t/db_dependent/api/v1/patrons_accounts.t index 6217a0fad2..c1d40ccb72 100755 --- a/t/db_dependent/api/v1/patrons_accounts.t +++ b/t/db_dependent/api/v1/patrons_accounts.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Test::Mojo; @@ -59,6 +59,7 @@ subtest 'get_balance() tests' => sub { $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") ->status_is(200)->json_is( { + borrowernumber => $patron->borrowernumber, balance => 0.00, outstanding_debits => { total => 0, lines => [] }, outstanding_credits => { total => 0, lines => [] } @@ -92,6 +93,7 @@ subtest 'get_balance() tests' => sub { $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") ->status_is(200)->json_is( { + borrowernumber => $patron->borrowernumber, balance => 100.01, outstanding_debits => { total => 100.01, @@ -117,6 +119,7 @@ subtest 'get_balance() tests' => sub { $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") ->status_is(200)->json_is( { + borrowernumber => $patron->borrowernumber, balance => 0, outstanding_debits => { total => 0, lines => [] }, outstanding_credits => { total => 0, lines => [] } @@ -139,6 +142,7 @@ subtest 'get_balance() tests' => sub { $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") ->status_is(200)->json_is( { + borrowernumber => $patron->borrowernumber, balance => -10, outstanding_debits => { total => 0, @@ -167,6 +171,7 @@ subtest 'get_balance() tests' => sub { $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") ->status_is(200)->json_is( { + borrowernumber => $patron->borrowernumber, balance => 40.00, outstanding_debits => { total => 50.00, @@ -367,6 +372,9 @@ subtest 'list_debits() test' => sub { ->tx->res->json; is(140, $ret->[0]->{amount} + $ret->[1]->{amount}, 'Total debits are 140'); + + $schema->storage->txn_rollback; + }; subtest 'add_debit() tests' => sub { @@ -472,3 +480,166 @@ subtest 'add_debit() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'get_patron_balances() tests' => sub { + plan tests => 15; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 1 } + } + ); + my $password = 'thePassword123'; + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $patron->userid; + my $patron_2 = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 1 } + } + ); + $patron_2->set_password( { password => $password, skip_validation => 1 } ); + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $account = $patron->account; + my $account_2 = $patron_2->account; + + $t->get_ok("//$userid:$password@/api/v1/patrons/get_balances") + ->status_is(200)->json_is([]); + + my $debit_1 = $account->add_debit( + { + amount => 10.00, + description => "A description", + type => "NEW_CARD", + user_id => $patron->borrowernumber, + library_id => $library->id, + interface => 'test', + } + )->store(); + $debit_1->discard_changes; + + $t->get_ok("//$userid:$password@/api/v1/patrons/get_balances") + ->status_is(200)->json_is([ + { + borrowernumber => $patron->borrowernumber, + balance => 10.00, + outstanding_debits => { + total => 10.00, + lines => [ $debit_1->to_api ] + }, + outstanding_credits => { + total => 0, + lines => [] + } + } + ]); + my $debit_2 = $account_2->add_debit( + { + amount => 5.00, + description => "A description", + type => "NEW_CARD", + user_id => $patron_2->borrowernumber, + library_id => $library->id, + interface => 'test', + } + )->store(); + $debit_2->discard_changes; + + $t->get_ok("//$userid:$password@/api/v1/patrons/get_balances") + ->status_is(200)->json_is([ + { + borrowernumber => $patron->borrowernumber, + balance => 10.00, + outstanding_debits => { + total => 10.00, + lines => [ $debit_1->to_api ] + }, + outstanding_credits => { + total => 0, + lines => [] + } + }, + { + borrowernumber => $patron_2->borrowernumber, + balance => 5.00, + outstanding_debits => { + total => 5.00, + lines => [ $debit_2->to_api ] + }, + outstanding_credits => { + total => 0, + lines => [] + } + } + ]); + + my $credit_1 = $account->add_credit( + { + amount => 10, + user_id => $patron->id, + library_id => $library->id, + interface => 'test' + } + ); + $credit_1->discard_changes; + + $t->get_ok("//$userid:$password@/api/v1/patrons/get_balances") + ->status_is(200)->json_is([ + { + borrowernumber => $patron->borrowernumber, + balance => 0, + outstanding_debits => { + total => 10.00, + lines => [ $debit_1->to_api ] + }, + outstanding_credits => { + total => -10.00, + lines => [ $credit_1->to_api ] + } + }, + { + borrowernumber => $patron_2->borrowernumber, + balance => 5.00, + outstanding_debits => { + total => 5.00, + lines => [ $debit_2->to_api ] + }, + outstanding_credits => { + total => 0, + lines => [] + } + } + ]); + + $account_2->pay( + { + amount => 5.00, + note => 'Fine paid in full', + description => 'Closed', + library_id => $patron_2->branchcode, + account_type => 'PAYMENT' + } + ); + + $t->get_ok("//$userid:$password@/api/v1/patrons/get_balances") + ->status_is(200)->json_is([ + { + borrowernumber => $patron->borrowernumber, + balance => 0, + outstanding_debits => { + total => 10.00, + lines => [ $debit_1->to_api ] + }, + outstanding_credits => { + total => -10.00, + lines => [ $credit_1->to_api ] + } + } + ]); + + $schema->storage->txn_rollback; +} -- 2.37.1 (Apple Git-137.1)