Bug 14402: Add unit tests for purge_zero_balance_fees()
authorBarton Chittenden <barton@bywatersolutions.com>
Sat, 18 Jul 2015 19:58:51 +0000 (12:58 -0700)
committerTomas Cohen Arazi <tomascohen@theke.io>
Mon, 9 Nov 2015 17:56:52 +0000 (14:56 -0300)
The function C4::Accounts::purge_zero_balance_fees() should delete rows
in accountlines where amountoutstanding is 0 and accountlines.date is
less than the current date minus '$days', i.e a number of days passed
to the function. Tests were added to prove the following:

* accountlines.amountoutstanding is 0, and date is set to CURRENT_DATE.
  The accountlines row should not be deleted. This is merely a sanity check,
  because difference between today's date and the fee date cannot be
  greater than $days.
* 'Before', 'On' and 'After' tests for accountlines.amountoutstanding = 0
    * accountlines.amountoutstanding is 0, and date is set to $days - 1
      days ago. The accountlines row should not be deleted.
    * accountlines.amountoutstanding is 0, and date is set to $days
      days ago.  the accountlines row should not be deleted, because
      the difference must be *greater* than $days.
    * accountlines.amountoutstanding is 0, and date is set to $days + 1
      days ago. The accountlines row *will* be deleted in this case.
* 'Before', 'On' and 'After' tests for accountlines.amountoutstanding > 0.
  (3 tests). The accountlines row will never be deleted, because
  amountoutstanding is not 0.
* 'Before', 'On' and 'After' tests for accountlines.amountoutstanding < 0.
  (3 testes) This tests credits. Again, the accountlines row will never
  be deleted, because amountoutstanding is not 0.

http://bugs.koha-community.org/show_bug.cgi?id=14402

Signed-off-by: Nancy Keener <nkeener@washoecounty.us>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
t/db_dependent/Accounts.t

index 991de12..653fe1a 100644 (file)
@@ -18,7 +18,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 8;
+use Test::More tests => 18;
 use Test::MockModule;
 use Test::Warn;
 
@@ -44,7 +44,8 @@ can_ok( 'C4::Accounts',
         ReversePayment
         recordpayment_selectaccts
         makepartialpayment
-        WriteOffFee )
+        WriteOffFee
+        purge_zero_balance_fees )
 );
 
 my $schema  = Koha::Database->new->schema;
@@ -73,6 +74,62 @@ $context->mock( 'userenv', sub {
     };
 });
 
+# Testing purge_zero_balance_fees
+
+# The 3rd value in the insert is 'days ago' --
+# 0 => today
+# 1 => yesterday
+# etc.
+
+$sth = $dbh->prepare(
+    "INSERT INTO accountlines (
+         borrowernumber,
+         amountoutstanding,
+         date,
+         description
+     )
+     VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
+);
+
+my $days = 5;
+
+my @test_data = (
+    { amount => 0  , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0 } ,
+    { amount => 0  , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0 } ,
+    { amount => 0  , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0 } ,
+    { amount => 0  , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1 } ,
+    { amount => 5  , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with postive amout owed before threshold day'  , delete => 0 } ,
+    { amount => 5  , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with postive amout owed on threshold day'      , delete => 0 } ,
+    { amount => 5  , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with postive amout owed after threshold day'   , delete => 0 } ,
+    { amount => -5 , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
+    { amount => -5 , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
+    { amount => -5 , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
+);
+
+for my $data  ( @test_data ) {
+    $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
+}
+
+purge_zero_balance_fees( $days );
+
+$sth = $dbh->prepare(
+            "select count(*) = 0 as deleted
+             from accountlines
+             where description = ?"
+       );
+
+#
+sub is_delete_correct {
+    my $should_delete = shift;
+    my $description = shift;
+    $sth->execute( $description );
+    my $test = $sth->fetchrow_hashref();
+    is( $test->{deleted}, $should_delete, $description )
+}
+
+for my $data  (@test_data) {
+    is_delete_correct( $data->{delete}, $data->{description});
+}
 
 subtest "recordpayment() tests" => sub {