From 0c5be3ea21fb89c6e329b79304e8c0c53acb5034 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 4 Nov 2016 16:18:00 +0000 Subject: [PATCH] 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 Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/Patrons.t | 23 +++++++++++- t/db_dependent/Members.t | 82 +------------------------------------------ 2 files changed, 23 insertions(+), 82 deletions(-) diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 7be731c..fd97ca3 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 13; +use Test::More tests => 14; use Test::Warn; use C4::Members; @@ -362,6 +362,27 @@ subtest 'add_enrolment_fee_if_needed' => 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' ); diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index 963d790..2296805 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -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; @@ -90,8 +90,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()"); @@ -497,84 +495,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 { -- 2.7.4