Bug 20946: (RM follow-up) Additional tests and fix addition to undef
authorNick Clemens <nick@bywatersolutions.com>
Wed, 27 Jun 2018 03:53:27 +0000 (03:53 +0000)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 6 Jul 2018 10:33:14 +0000 (10:33 +0000)
Added a few tests with credit lines and found we got warnings, adjusted
the routine to avoid this

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/Account.pm
t/db_dependent/Koha/Account.t

index bffc98b..4b48d69 100644 (file)
@@ -22,7 +22,7 @@ use Modern::Perl;
 use Carp;
 use Data::Dumper;
 use List::MoreUtils qw( uniq );
-use List::Util qw( sum );
+use List::Util qw( sum0 );
 
 use C4::Log qw( logaction );
 use C4::Stats qw( UpdateStats );
@@ -305,8 +305,7 @@ sub outstanding_debits {
         }
     );
 
-    # sum returns undef it list is empty
-    my $total = sum( $lines->get_column('amountoutstanding') ) + 0;
+    my $total = sum0( $lines->get_column('amountoutstanding') );
 
     return ( $total, $lines );
 }
index 5d15f4a..c0cce13 100755 (executable)
@@ -31,7 +31,7 @@ my $builder = t::lib::TestBuilder->new;
 
 subtest 'outstanding_debits() tests' => sub {
 
-    plan tests => 7;
+    plan tests => 12;
 
     $schema->storage->txn_begin;
 
@@ -59,6 +59,23 @@ subtest 'outstanding_debits() tests' => sub {
     is( $total, 0, "Total if no outstanding debits is 0" );
     is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
 
+    my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
+    my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding =>  3 })->store;
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -6 })->store;
+    ( $total, $lines ) =  Koha::Account->new({ patron_id => $patron_2->id })->outstanding_debits();
+    is( $total, 3, "Total if some outstanding debits and some credits is only debits" );
+    is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
+    my $the_line = Koha::Account::Lines->find( $just_one->id );
+    is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
+
+    my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -20 })->store;
+    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -200 })->store;
+    ( $total, $lines ) =  Koha::Account->new({ patron_id => $patron_3->id })->outstanding_debits();
+    is( $total, 0, "Total if no outstanding debits total is 0" );
+    is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
 
     $schema->storage->txn_rollback;
 };