Bug 17557: Revised patron age calculation tests

The SetAge and GetAge test coverage are excessive.
First the SetAge subroutine was only created for testing purpose.
The goal of GetAge is quite simple and it seems quite easy to provide
corect test coverage using DateTime->add using negative numbers.

Edit: rebased so it applies

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Jonathan Druart 2016-11-04 16:18:00 +00:00 committed by Kyle M Hall
parent b133b0e94d
commit 7808bd690b
2 changed files with 23 additions and 82 deletions

View file

@ -19,7 +19,7 @@
use Modern::Perl;
use Test::More tests => 15;
use Test::More tests => 16;
use Test::Warn;
use DateTime;
@ -471,6 +471,27 @@ subtest 'get_overdues' => sub {
$patron->delete;
};
subtest 'get_age' => sub {
plan tests => 6;
my $patron = $builder->build( { source => 'Borrower' } );
$patron = Koha::Patrons->find( $patron->{borrowernumber} );
my $today = dt_from_string;
$patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
is( $patron->get_age, 12, 'Patron should be 12' );
$patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1 ) );
is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
$patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0 ) );
is( $patron->get_age, 18, 'Patron should be 18' );
$patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31 ) );
is( $patron->get_age, 19, 'Patron should be 19' );
$patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30 ) );
is( $patron->get_age, 19, 'Patron should be 19 again' );
$patron->dateofbirth( $today->clone->add( years => 0, months => -1, days => -1 ) );
is( $patron->get_age, 0, 'Patron is a newborn child' );
$patron->delete;
};
$retrieved_patron_1->delete;
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );

View file

@ -17,7 +17,7 @@
use Modern::Perl;
use Test::More tests => 79;
use Test::More tests => 66;
use Test::MockModule;
use Data::Dumper;
use C4::Context;
@ -93,8 +93,6 @@ my %data = (
userid => 'tomasito'
);
testAgeAccessors(\%data); #Age accessor tests don't touch the db so it is safe to run them with just the object.
my $addmem=AddMember(%data);
ok($addmem, "AddMember()");
@ -503,84 +501,6 @@ $borrower = GetMember(borrowernumber => $borrowernumber);
my $hashed_up = Koha::AuthUtils::hash_password("Nexus-6", $borrower->{password});
is( $borrower->{password} eq $hashed_up, 1, 'Check password hash equals hash of submitted password' );
### ------------------------------------- ###
### Testing GetAge() / SetAge() functions ###
### ------------------------------------- ###
#USES the package $member-variable to mock a koha.borrowers-object
sub testAgeAccessors {
my ($member) = @_;
my $original_dateofbirth = $member->{dateofbirth};
##Testing GetAge()
my $age=GetAge("1992-08-14", "2011-01-19");
is ($age, "18", "Age correct");
$age=GetAge("2011-01-19", "1992-01-19");
is ($age, "-19", "Birthday In the Future");
##Testing SetAge() for now()
my $dt_now = DateTime->now();
$age = DateTime::Duration->new(years => 12, months => 6, days => 1);
C4::Members::SetAge( $member, $age );
$age = C4::Members::GetAge( $member->{dateofbirth} );
is ($age, '12', "SetAge 12 years");
$age = DateTime::Duration->new(years => 18, months => 12, days => 31);
C4::Members::SetAge( $member, $age );
$age = C4::Members::GetAge( $member->{dateofbirth} );
is ($age, '19', "SetAge 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18.
$age = DateTime::Duration->new(years => 18, months => 12, days => 30);
C4::Members::SetAge( $member, $age );
$age = C4::Members::GetAge( $member->{dateofbirth} );
is ($age, '19', "SetAge 18 years");
$age = DateTime::Duration->new(years => 0, months => 1, days => 1);
C4::Members::SetAge( $member, $age );
$age = C4::Members::GetAge( $member->{dateofbirth} );
is ($age, '0', "SetAge 0 years");
$age = '0018-12-31';
C4::Members::SetAge( $member, $age );
$age = C4::Members::GetAge( $member->{dateofbirth} );
is ($age, '19', "SetAge ISO_Date 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18.
$age = '0018-12-30';
C4::Members::SetAge( $member, $age );
$age = C4::Members::GetAge( $member->{dateofbirth} );
is ($age, '19', "SetAge ISO_Date 18 years");
$age = '18-1-1';
eval { C4::Members::SetAge( $member, $age ); };
is ((length $@ > 1), '1', "SetAge ISO_Date $age years FAILS");
$age = '0018-01-01';
eval { C4::Members::SetAge( $member, $age ); };
is ((length $@ == 0), '1', "SetAge ISO_Date $age years succeeds");
##Testing SetAge() for relative_date
my $relative_date = DateTime->new(year => 3010, month => 3, day => 15);
$age = DateTime::Duration->new(years => 10, months => 3);
C4::Members::SetAge( $member, $age, $relative_date );
$age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
is ($age, '10', "SetAge, 10 years and 3 months old person was born on ".$member->{dateofbirth}." if todays is ".$relative_date->ymd());
$age = DateTime::Duration->new(years => 112, months => 1, days => 1);
C4::Members::SetAge( $member, $age, $relative_date );
$age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
is ($age, '112', "SetAge, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd());
$age = '0112-01-01';
C4::Members::SetAge( $member, $age, $relative_date );
$age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() );
is ($age, '112', "SetAge ISO_Date, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd());
$member->{dateofbirth} = $original_dateofbirth; #It is polite to revert made changes in the unit tests.
} #sub testAgeAccessors
# regression test for bug 16009
my $patron;
eval {