From 3d63e03585c0b0a1f3e51bf030c73d7c8d05b506 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 9 Jul 2015 15:35:53 +0200 Subject: [PATCH] [PASSED QA] Bug 14494: Add some unit tests too for CalcDateDue 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 Amended patch: Set the number of tests for the second subtest (was commented) and perltidy the second block. Signed-off-by: Kyle M Hall --- t/db_dependent/Circulation_dateexpiry.t | 53 ++++++++++++++++++++++++++++--- 1 files changed, 48 insertions(+), 5 deletions(-) diff --git a/t/db_dependent/Circulation_dateexpiry.t b/t/db_dependent/Circulation_dateexpiry.t index 0c4d728..dfa4e4d 100644 --- a/t/db_dependent/Circulation_dateexpiry.t +++ b/t/db_dependent/Circulation_dateexpiry.t @@ -18,19 +18,24 @@ 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." ); +} -- 1.7.2.5