From cdb7e561b33efa059a75f16479820fd211b34d0a Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 22 Aug 2019 15:03:04 +0100
Subject: [PATCH] Bug 23355: Tests for Account::Lines summation methods

---
 t/db_dependent/Koha/Account/Lines.t | 229 +++++++++++++++++++++++++++-
 1 file changed, 228 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Account/Lines.t b/t/db_dependent/Koha/Account/Lines.t
index cdc31720f5..0f87057a60 100755
--- a/t/db_dependent/Koha/Account/Lines.t
+++ b/t/db_dependent/Koha/Account/Lines.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 1;
+use Test::More tests => 4;
 use Test::Exception;
 
 use Koha::Account;
@@ -108,5 +108,232 @@ subtest 'total_outstanding() tests' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'total() tests' => sub {
+
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+
+    my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->total, 0, 'total returns 0 if no lines (undef case)' );
+
+    my $debit_1 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => 10,
+            amountoutstanding => 10,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    my $debit_2 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => 10,
+            amountoutstanding => 10,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->total, 20, 'total sums correctly' );
+
+    my $credit_1 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -10,
+            amountoutstanding => -10,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->total, 10, 'total sums correctly' );
+
+    my $credit_2 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -10,
+            amountoutstanding => -10,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->total, 0, 'total sums correctly' );
+
+    my $credit_3 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -100,
+            amountoutstanding => -100,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->total, -100, 'total sums correctly' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'credits_total() tests' => sub {
+
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+
+    my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->credits_total, 0, 'credits_total returns 0 if no lines (undef case)' );
+
+    my $debit_1 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => 10,
+            amountoutstanding => 10,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    my $debit_2 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => 10,
+            amountoutstanding => 10,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->credits_total, 0, 'credits_total sums correctly' );
+
+    my $credit_1 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -10,
+            amountoutstanding => -10,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->credits_total, -10, 'credits_total sums correctly' );
+
+    my $credit_2 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -10,
+            amountoutstanding => -10,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->credits_total, -20, 'credits_total sums correctly' );
+
+    my $credit_3 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -100,
+            amountoutstanding => -100,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->credits_total, -120, 'credits_total sums correctly' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'debits_total() tests' => sub {
+
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+
+    my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->debits_total, 0, 'debits_total returns 0 if no lines (undef case)' );
+
+    my $debit_1 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => 10,
+            amountoutstanding => 0,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    my $debit_2 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => 10,
+            amountoutstanding => 0,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->debits_total, 20, 'debits_total sums correctly' );
+
+    my $credit_1 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -10,
+            amountoutstanding => 0,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->debits_total, 20, 'debits_total sums correctly' );
+
+    my $credit_2 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -10,
+            amountoutstanding => 0,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->debits_total, 20, 'debits_total sums correctly' );
+
+    my $credit_3 = Koha::Account::Line->new(
+        {   borrowernumber    => $patron->id,
+            accounttype       => "OVERDUE",
+            status            => "RETURNED",
+            amount            => -100,
+            amountoutstanding => 0,
+            interface         => 'commandline',
+        }
+    )->store;
+
+    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
+    is( $lines->debits_total, 20, 'debits_total sums correctly' );
+
+    $schema->storage->txn_rollback;
+};
 
 1;
-- 
2.20.1