Bugzilla – Attachment 76451 Details for
Bug 20997
Add Koha::Account::Line::apply
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 20997: Add unit tests for Koha::Account::Line::apply
Bug-20997-Add-unit-tests-for-KohaAccountLineapply.patch (text/plain), 6.70 KB, created by
Tomás Cohen Arazi (tcohen)
on 2018-06-26 14:56:18 UTC
(
hide
)
Description:
Bug 20997: Add unit tests for Koha::Account::Line::apply
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2018-06-26 14:56:18 UTC
Size:
6.70 KB
patch
obsolete
>From 9140773b84a8e2769b29a3270f079ceddd276265 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 26 Jun 2018 11:51:41 -0300 >Subject: [PATCH] Bug 20997: Add unit tests for Koha::Account::Line::apply > >This patch adds unit tests for the mentioned method. > >To test: >- Apply this patch >- Run: > $ khell > k$ prove t/db_dependent/Koha/Account/Lines.t >=> FAIL: The method is not already implemented! >--- > t/db_dependent/Koha/Account/Lines.t | 162 ++++++++++++++++++++++------ > 1 file changed, 130 insertions(+), 32 deletions(-) > >diff --git a/t/db_dependent/Koha/Account/Lines.t b/t/db_dependent/Koha/Account/Lines.t >index 0c3287d8c3..05876aee9a 100755 >--- a/t/db_dependent/Koha/Account/Lines.t >+++ b/t/db_dependent/Koha/Account/Lines.t >@@ -19,47 +19,145 @@ > > use Modern::Perl; > >-use Test::More tests => 1; >+use Test::More tests => 3; >+use Test::Exception; > >+use Koha::Account; > use Koha::Account::Lines; >+use Koha::Account::Offsets; > use Koha::Items; > > use t::lib::TestBuilder; > > my $schema = Koha::Database->new->schema; >-$schema->storage->txn_begin; >- > my $builder = t::lib::TestBuilder->new; > >-subtest 'item' => sub { >- plan tests => 2; >- >- my $library = $builder->build( { source => 'Branch' } ); >- my $biblioitem = $builder->build( { source => 'Biblioitem' } ); >- my $patron = $builder->build( { source => 'Borrower' } ); >- my $item = Koha::Item->new( >- { >- biblionumber => $biblioitem->{biblionumber}, >- biblioitemnumber => $biblioitem->{biblioitemnumber}, >- homebranch => $library->{branchcode}, >- holdingbranch => $library->{branchcode}, >- barcode => 'some_barcode_12', >- itype => 'BK', >- })->store; >- >- my $line = Koha::Account::Line->new( >- { >- borrowernumber => $patron->{borrowernumber}, >- itemnumber => $item->itemnumber, >- accounttype => "F", >- amount => 10, >- })->store; >- >- my $account_line_item = $line->item; >- is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' ); >- is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' ); >+subtest 'item() tests' => sub { >+ plan tests => 2; >+ >+ $schema->storage->txn_begin; >+ >+ my $library = $builder->build( { source => 'Branch' } ); >+ my $biblioitem = $builder->build( { source => 'Biblioitem' } ); >+ my $patron = $builder->build( { source => 'Borrower' } ); >+ my $item = Koha::Item->new( >+ { >+ biblionumber => $biblioitem->{biblionumber}, >+ biblioitemnumber => $biblioitem->{biblioitemnumber}, >+ homebranch => $library->{branchcode}, >+ holdingbranch => $library->{branchcode}, >+ barcode => 'some_barcode_12', >+ itype => 'BK', >+ })->store; >+ >+ my $line = Koha::Account::Line->new( >+ { >+ borrowernumber => $patron->{borrowernumber}, >+ itemnumber => $item->itemnumber, >+ accounttype => "F", >+ amount => 10, >+ })->store; >+ >+ my $account_line_item = $line->item; >+ is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' ); >+ is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' ); >+ >+ $schema->storage->txn_rollback; > }; > >-$schema->storage->txn_rollback; >+subtest 'is_credit() tests' => sub { >+ >+ plan tests => 2; >+ >+ $schema->storage->txn_begin; >+ >+ my $patron = $builder->build_object({ class => 'Koha::Patrons' }); >+ my $account = $patron->account; >+ >+ my $credit = $account->add_credit({ amount => 100, user_id => $patron->id }); >+ >+ ok( $credit->is_credit, 'is_credit detects credits' ); >+ >+ my $debit = Koha::Account::Line->new( >+ { >+ borrowernumber => $patron->id, >+ accounttype => "F", >+ amount => 10, >+ })->store; >+ >+ ok( !$debit->is_credit, 'is_credit() detects debits' ); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'apply() tests' => sub { >+ >+ plan tests => 12; >+ >+ $schema->storage->txn_begin; >+ >+ my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); >+ my $account = $patron->account; >+ >+ my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } ); >+ >+ my $debit_1 = Koha::Account::Line->new( >+ { borrowernumber => $patron->id, >+ accounttype => "F", >+ amount => 10, >+ amountoutstanding => 10 >+ } >+ )->store; >+ >+ my $debit_2 = Koha::Account::Line->new( >+ { borrowernumber => $patron->id, >+ accounttype => "F", >+ amount => 100, >+ amountoutstanding => 100 >+ } >+ )->store; >+ >+ $credit->discard_changes; >+ $debit_1->discard_changes; >+ >+ my $remaining_credit = $credit->apply( { debit => $debit_1, offset_type => 'Manual Credit' } ); >+ is( $remaining_credit, 90, 'Remaining credit is correctly calculated' ); >+ $credit->discard_changes; >+ is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' ); >+ >+ # re-read debit info >+ $debit_1->discard_changes; >+ is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' ); >+ >+ my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } ); >+ is( $offsets->count, 1, 'Only one offset is generated' ); >+ my $THE_offest = $offsets->next; >+ is( $THE_offest->amount * 1, 10, 'Amount was calculated correctly (less than the available credit)' ); >+ is( $THE_offest->type, 'Manual Credit', 'Passed type stored correctly' ); >+ >+ $remaining_credit = $credit->apply( { debit => $debit_2, offset_type => 'Manual Credit' } ); >+ is( $remaining_credit, 0, 'No remaining credit left' ); >+ $credit->discard_changes; >+ is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' ); >+ $debit_2->discard_changes; >+ is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' ); >+ >+ throws_ok >+ { $debit_1->apply({ debit => $credit }); } >+ 'Koha::Exceptions::Account::IsNotCredit', >+ '->apply() can only be used with credits'; >+ >+ throws_ok >+ { $credit->apply({ debit => $credit }); } >+ 'Koha::Exceptions::Account::IsNotDebit', >+ '->apply() can only be applied to credits'; >+ >+ throws_ok >+ { $credit->apply({ debit => $debit_1 }); } >+ 'Koha::Exceptions::Account::NoAvailableCredit', >+ '->apply() can only be used with outstanding credits'; >+ >+ >+ $schema->storage->txn_rollback; >+}; > >-1; >-- >2.18.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 20997
:
76451
|
76452
|
76459
|
76460
|
76461
|
76462
|
76463
|
76464
|
76465
|
76549
|
76550
|
76551
|
76654
|
76655
|
77194
|
77195
|
77196
|
77197
|
77198
|
77199
|
77310
|
77349
|
77350
|
77351
|
77352
|
77353
|
77354
|
77355
|
77356