From 6dbcdabe5c93056daa85e16268ba070900da07ce Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 25 Aug 2017 17:56:52 -0300 Subject: [PATCH] Bug 19176: Compare the number of seconds when comparing dates in tests # Failed test 'borrowers.updated_on should have been set to now on creating' # at t/db_dependent/Patrons.t line 74. # got: '2017-08-10T20:53:03' # expected: '2017-08-10T20:53:04' # Looks like you failed 1 test of 17. [20:53:15] t/db_dependent/Patrons.t ..................................... The plan here is to compare the number of seconds between two dates. If < 60 the dates are consired as identicals. Signed-off-by: David Bourgault --- t/db_dependent/Patrons.t | 7 ++++--- t/db_dependent/Virtualshelves.t | 9 +++++---- t/lib/Dates.pm | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 t/lib/Dates.pm diff --git a/t/db_dependent/Patrons.t b/t/db_dependent/Patrons.t index a1c7ad9..8882809 100755 --- a/t/db_dependent/Patrons.t +++ b/t/db_dependent/Patrons.t @@ -24,6 +24,7 @@ use C4::Context; use Koha::Database; use Koha::DateUtils; +use t::lib::Dates; use t::lib::TestBuilder; BEGIN { @@ -71,13 +72,13 @@ $b3->store(); my $b1_new = Koha::Patrons->find( $b1->borrowernumber() ); is( $b1->surname(), $b1_new->surname(), "Found matching patron" ); isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" ); -is( dt_from_string($b1_new->updated_on), $now, "borrowers.updated_on should have been set to now on creating" ); +t::lib::Dates::compare( $b1_new->updated_on, $now, "borrowers.updated_on should have been set to now on creating" ); my $b3_new = Koha::Patrons->find( $b3->borrowernumber() ); -is( dt_from_string($b3_new->updated_on), $three_days_ago, "borrowers.updated_on should have been kept to what we set on creating" ); +t::lib::Dates::compare( $b3_new->updated_on, $three_days_ago, "borrowers.updated_on should have been kept to what we set on creating" ); $b3_new->set({ firstname => 'Some first name for Test 3' })->store(); $b3_new = Koha::Patrons->find( $b3->borrowernumber() ); -is( dt_from_string($b3_new->updated_on), dt_from_string, "borrowers.updated_on should have been set to now on updating" ); +t::lib::Dates::compare( $b3_new->updated_on, $now, "borrowers.updated_on should have been set to now on updating" ); my @patrons = Koha::Patrons->search( { branchcode => $branchcode } ); is( @patrons, 3, "Found 3 patrons with Search" ); diff --git a/t/db_dependent/Virtualshelves.t b/t/db_dependent/Virtualshelves.t index dd2b47c..8e65564 100644 --- a/t/db_dependent/Virtualshelves.t +++ b/t/db_dependent/Virtualshelves.t @@ -10,6 +10,7 @@ use Koha::Virtualshelves; use Koha::Virtualshelfshares; use Koha::Virtualshelfcontents; +use t::lib::Dates; use t::lib::TestBuilder; my $builder = t::lib::TestBuilder->new; @@ -41,7 +42,7 @@ subtest 'CRUD' => sub { is( $number_of_shelves, 1, '1 shelf should have been inserted' ); is( $shelf->allow_change_from_owner, 1, 'The default value for allow_change_from_owner should be 1' ); is( $shelf->allow_change_from_others, 0, 'The default value for allow_change_from_others should be 0' ); - is( output_pref($shelf->created_on), output_pref(dt_from_string), 'The creation time should have been set to today' ); + t::lib::Dates::compare( $shelf->created_on, dt_from_string, 'The creation time should have been set to today' ); # Test if creation date will not be overwritten by store my $created = dt_from_string->subtract( hours => 1 ); @@ -51,7 +52,7 @@ subtest 'CRUD' => sub { my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber ); is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' ); - is( dt_from_string($retrieved_shelf->created_on), $created, 'Creation date is the same after update (Bug 18672)' ); + t::lib::Dates::compare( $retrieved_shelf->created_on, $created, 'Creation date is the same after update (Bug 18672)' ); # Insert with the same name eval { @@ -181,11 +182,11 @@ subtest 'Shelf content' => sub { )->store; $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber ); - is( output_pref( dt_from_string $shelf->lastmodified ), output_pref($dt_yesterday), 'The lastmodified has been set to yesterday, will be useful for another test later' ); + t::lib::Dates::compare( $shelf->lastmodified, $dt_yesterday, 'The lastmodified has been set to yesterday, will be useful for another test later' ); my $content1 = $shelf->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} ); is( ref($content1), 'Koha::Virtualshelfcontent', 'add_biblio to a shelf should return a Koha::Virtualshelfcontent object if inserted' ); $shelf = Koha::Virtualshelves->find( $shelf->shelfnumber ); - is( output_pref( dt_from_string( $shelf->lastmodified ) ), output_pref(dt_from_string), 'Adding a biblio to a shelf should update the lastmodified for the shelf' ); + t::lib::Dates::compare( $shelf->lastmodified, dt_from_string, 'Adding a biblio to a shelf should update the lastmodified for the shelf' ); my $content2 = $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} ); $number_of_contents = Koha::Virtualshelfcontents->search->count; is( $number_of_contents, 2, '2 biblio should have been inserted' ); diff --git a/t/lib/Dates.pm b/t/lib/Dates.pm new file mode 100644 index 0000000..e0b2a3c --- /dev/null +++ b/t/lib/Dates.pm @@ -0,0 +1,25 @@ +package t::lib::Dates; + +use Modern::Perl; +use Test::More; +use Koha::DateUtils; + +=head2 compare + + compare( $got_dt, $expected_dt, $test_description ); + +Will execute a test and compare the 2 dates given in parameters +The date will be compared truncated to minutes + +=cut + +sub compare { + my ( $got, $expected, $description ) = @_; + my $dt_got = dt_from_string( $got ); + my $dt_expected = dt_from_string( $expected ); + my $diff = $dt_got->clone->subtract_datetime( $dt_expected ); + my $diff_seconds = $diff->minutes * 60 + $diff->seconds; + ok( $diff_seconds < 60, $description . "(in $diff_seconds seconds)" ); +} + +1; -- 2.7.4