From 5c87bcb232e5a62084729690180feb728d9aed35 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. --- t/Test/Dates.t | 26 ++++++++++++++++++++++++++ t/db_dependent/Patrons.t | 7 ++++--- t/db_dependent/Virtualshelves.t | 9 +++++---- t/lib/Dates.pm | 27 +++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 t/Test/Dates.t create mode 100644 t/lib/Dates.pm diff --git a/t/Test/Dates.t b/t/Test/Dates.t new file mode 100644 index 0000000000..5241f9e9ab --- /dev/null +++ b/t/Test/Dates.t @@ -0,0 +1,26 @@ +use Modern::Perl; +use Test::More tests => 7; +use t::lib::Dates; +use Koha::DateUtils qw( dt_from_string ); + +my $date_1 = '2017-01-01 01:00:00'; +my $date_2 = '2018-02-02 01:00:00'; +my $dt_1 = dt_from_string($date_1); +my $dt_2 = dt_from_string($date_2); + +is( t::lib::Dates::compare( $dt_1, $dt_2 ), -1, '2017 is before 2018' ); +is( t::lib::Dates::compare( $dt_2, $dt_1 ), 1, '2018 is after 2017' ); + +is( t::lib::Dates::compare( $date_1, $date_2 ), -1, '2017 is before 2018 (strings comparison)' ); +is( t::lib::Dates::compare( $date_2, $date_1 ), 1, '2018 is after 2017 (strings comparison)' ); + +my $dt_3 = $dt_1->clone->subtract( seconds => 59 ); +is( t::lib::Dates::compare( $dt_1, $dt_3 ), + 0, 'If there is less than 1min, the dates are considered identicals' ); +is( t::lib::Dates::compare( $dt_3, $dt_1 ), + 0, 'If there is less than 1min, the dates are considered identicals' ); + +$dt_1->set_time_zone('+0000'); +$dt_3 = $dt_1->clone->set_time_zone('+0400'); + +is( t::lib::Dates::compare( $dt_1, $dt_3 ), -1, "Compare different timezones" ); diff --git a/t/db_dependent/Patrons.t b/t/db_dependent/Patrons.t index a1c7ad9829..f0d116a7bc 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" ); +is( t::lib::Dates::compare( $b1_new->updated_on, $now), 0, "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" ); +is( t::lib::Dates::compare( $b3_new->updated_on, $three_days_ago), 0, "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" ); +is( t::lib::Dates::compare( $b3_new->updated_on, $now), 0, "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 dd2b47c921..f77e36b32c 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' ); + is( t::lib::Dates::compare( $shelf->created_on, dt_from_string), 0, '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)' ); + is( t::lib::Dates::compare( $retrieved_shelf->created_on, $created), 0, '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' ); + is( t::lib::Dates::compare( $shelf->lastmodified, $dt_yesterday), 0, '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' ); + is( t::lib::Dates::compare( $shelf->lastmodified, dt_from_string), 0, '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 0000000000..1ba4d223bf --- /dev/null +++ b/t/lib/Dates.pm @@ -0,0 +1,27 @@ +package t::lib::Dates; + +use Modern::Perl; +use Test::More; +use Koha::DateUtils; +use DateTime; +=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); + $dt_got->set_time_zone('floating'); + $dt_expected->set_time_zone('floating'); + my $diff = $dt_got->epoch - $dt_expected->epoch; + if ( abs($diff) < 60 ) { return 0 } + return $diff > 0 ? 1 : -1; +} + +1; -- 2.11.0