Koha/t/Test/Dates.t
Jonathan Druart dfcb2b8071 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: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2017-10-30 14:55:18 -03:00

26 lines
1 KiB
Perl

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" );