Bug 14494: Add some unit tests too for CalcDateDue
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Thu, 9 Jul 2015 13:35:53 +0000 (15:35 +0200)
committerTomas Cohen Arazi <tomascohen@unc.edu.ar>
Mon, 20 Jul 2015 13:16:24 +0000 (10:16 -0300)
The second patch of this report made some changes to CalcDateDue.
We are adding some unit tests here.
See the commments on the third patch too.

Test plan:
Run t/db_dependent/Circulation_dateexpiry.t

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Amended patch: Set the number of tests for the second subtest (was
commented) and perltidy the second block.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@unc.edu.ar>
t/db_dependent/Circulation_dateexpiry.t

index 0c4d728..dfa4e4d 100644 (file)
 use Modern::Perl;
 
 use DateTime;
-use Time::HiRes qw/gettimeofday/;
+use Time::HiRes qw/gettimeofday time/;
+use Test::More tests => 2;
 use C4::Members;
 use Koha::DateUtils;
 use t::lib::TestBuilder;
-use Test::More tests => 1;
+use t::lib::Mocks qw( mock_preference );
 
+my $builder = t::lib::TestBuilder->new();
 subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub {
     plan tests => 4;
-    date_expiry();
+    can_book_be_issued();
+};
+subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
+    plan tests => 4;
+    calc_date_due();
 };
 
-sub date_expiry {
-    my $builder = t::lib::TestBuilder->new();
+sub can_book_be_issued {
     my $item    = $builder->build( { source => 'Item' } );
     my $patron  = $builder->build(
         {   source => 'Borrower',
@@ -66,3 +71,41 @@ sub date_expiry {
     is( not( exists $issuingimpossible->{EXPIRED} ), 1, 'The patron should not be considered as expired if dateexpiry is tomorrow' );
 
 }
+
+sub calc_date_due {
+    t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
+
+    # this triggers the compare between expiry and due date
+
+    my $patron = $builder->build( { source => 'Borrower' } );
+    my $item   = $builder->build( { source => 'Item' } );
+    my $branch = $builder->build( { source => 'Branch' } );
+    my $today  = dt_from_string();
+
+    # first test with empty expiry date
+    # note that this expiry date will never lead to an issue btw !!
+    $patron->{dateexpiry} = '0000-00-00';
+    my $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
+    is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry 0000-00-00" );
+
+    # second test expiry date==today
+    my $d2 = output_pref( { dt => $today, dateonly => 1, dateformat => 'sql' } );
+    $patron->{dateexpiry} = $d2;
+    $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
+    is( ref $d eq "DateTime" && DateTime->compare( $d->truncate( to => 'day' ), $today->truncate( to => 'day' ) ) == 0, 1, "CalcDateDue with expiry today" );
+
+    # third test expiry date tomorrow
+    my $dur = DateTime::Duration->new( days => 1 );
+    my $tomorrow = $today->clone->add_duration($dur);
+    $d2 = output_pref( { dt => $tomorrow, dateonly => 1, dateformat => 'sql' } );
+    $patron->{dateexpiry} = $d2;
+    $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
+    is( ref $d eq "DateTime" && $d->mdy() =~ /^\d+/, 1, "CalcDateDue with expiry tomorrow" );
+
+    # fourth test far future
+    $patron->{dateexpiry} = '9876-12-31';
+    my $t1 = time;
+    $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
+    my $t2 = time;
+    is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
+}