From 4bc90c6d5f8642d206d04bf93b4fd82bc14d1568 Mon Sep 17 00:00:00 2001 From: Prashant Sharma Date: Sat, 11 Jun 2022 20:15:37 +0530 Subject: [PATCH] test(test_date.py): add tests for negative timestamp evaluation --- tests/test_date.py | 28 ++++++++++++++++++++++++++++ tests/test_date_parser.py | 12 ++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/test_date.py b/tests/test_date.py index 7ea636663..b560b38d9 100644 --- a/tests/test_date.py +++ b/tests/test_date.py @@ -726,6 +726,34 @@ def test_timestamp_in_microseconds(self): datetime.fromtimestamp(1570308760).replace(microsecond=263111) ) + @parameterized.expand([ + param( + input_timestamp='-1570308760', + negative_allowed=True, + result=datetime.fromtimestamp(-1570308760) + ), + param( + input_timestamp='-1570308760', + negative_allowed=False, + result=None + ), + param( + input_timestamp='1570308760', + negative_allowed=True, + result=None + ), + param( + input_timestamp='1570308760', + negative_allowed=False, + result=datetime.fromtimestamp(1570308760) + ) + ]) + def test_timestamp_with_negative(self, input_timestamp, negative_allowed, result): + self.assertEqual( + date.get_date_from_timestamp(input_timestamp, None, negative=negative_allowed), + result + ) + @parameterized.expand([ param(date_string='15703087602631'), param(date_string='157030876026xx'), diff --git a/tests/test_date_parser.py b/tests/test_date_parser.py index ead18292b..b9d7bf076 100644 --- a/tests/test_date_parser.py +++ b/tests/test_date_parser.py @@ -645,6 +645,18 @@ def test_parse_timestamp(self, date_string, expected): self.then_date_obj_exactly_is(expected) self.then_period_is('day') + @parameterized.expand([ + param('-1484823450', expected=datetime(1922, 12, 13, 13, 2, 30)), + param('-1436745600000', expected=datetime(1924, 6, 22, 0, 0)), + param('-1015673450000001', expected=datetime(1937, 10, 25, 12, 29, 10, 1)) + ]) + def test_parse_negative_timestamp(self, date_string, expected): + self.given_local_tz_offset(0) + self.given_parser(settings={'TO_TIMEZONE': 'UTC', 'PARSERS': ['negative-timestamp']}) + self.when_date_is_parsed(date_string) + self.then_date_obj_exactly_is(expected) + self.then_period_is('day') + @parameterized.expand([ # Epoch timestamps. param('1484823450', expected=datetime(2017, 1, 19, 10, 57, 30)),