From 9555c9549849ad33cd2c136a25a9e0f9c3130602 Mon Sep 17 00:00:00 2001 From: Joachim Metz Date: Wed, 13 May 2026 19:43:02 +0200 Subject: [PATCH 1/2] Worked on tests --- dfdatetime/golang_time.py | 2 +- tests/golang_time.py | 9 +++++++-- tests/serializer.py | 26 +++++++++++++++++++++++++- tests/time_elements.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/dfdatetime/golang_time.py b/dfdatetime/golang_time.py index f3e6858..82110db 100644 --- a/dfdatetime/golang_time.py +++ b/dfdatetime/golang_time.py @@ -141,7 +141,7 @@ def _GetNumberOfSeconds(self, golang_timestamp): # TODO: add support for version 2 time zone offset in seconds - except struct.error as exception: + except (TypeError, struct.error) as exception: raise ValueError(( f'Unable to unpacked Golang time.Time timestamp with error: ' f'{exception!s}')) diff --git a/tests/golang_time.py b/tests/golang_time.py index 6c046f7..086769e 100644 --- a/tests/golang_time.py +++ b/tests/golang_time.py @@ -136,7 +136,7 @@ def testGetNumberOfSeconds(self): golang_time_object._GetNumberOfSeconds(golang_timestamp) with self.assertRaises(ValueError): - golang_timestamp = bytes.fromhex('ffffffffffffffffffffffffffff01') + golang_timestamp = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] golang_time_object._GetNumberOfSeconds(golang_timestamp) def testCopyFromDateTimeString(self): @@ -174,8 +174,13 @@ def testCopyFromDateTimeString(self): self.assertEqual(golang_time_object._nanoseconds, 567890000) self.assertEqual(golang_time_object._time_zone_offset, 60) + # Test with invalid date string. with self.assertRaises(ValueError): - golang_time_object.CopyFromDateTimeString('-0001-01-01') + golang_time_object.CopyFromDateTimeString('0001:01-01 00:01:00') + + # Test with negative year. + with self.assertRaises(ValueError): + golang_time_object.CopyFromDateTimeString('-001-01-01 00:01:00') def testCopyToDateTimeString(self): """Test the CopyToDateTimeString function.""" diff --git a/tests/serializer.py b/tests/serializer.py index 7d7da02..34cf623 100644 --- a/tests/serializer.py +++ b/tests/serializer.py @@ -138,6 +138,18 @@ def testConvertDateTimeValuesToJSON(self): rfc2579_date_time_object) self.assertEqual(json_dict, expected_json_dict) + negative_timezone_rfc2579_object = rfc2579_date_time.RFC2579DateTime( + rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '-', 2, 0)) + + expected_json_dict = { + '__class_name__': 'RFC2579DateTime', + '__type__': 'DateTimeValues', + 'rfc2579_date_time_tuple': (2010, 8, 12, 20, 6, 31, 6, '-', 2, 0)} + + json_dict = serializer.Serializer.ConvertDateTimeValuesToJSON( + negative_timezone_rfc2579_object) + self.assertEqual(json_dict, expected_json_dict) + systemtime_object = systemtime.Systemtime( system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142)) @@ -266,7 +278,8 @@ def testConvertJSONToDateTimeValues(self): '__class_name__': 'GolangTime', '__type__': 'DateTimeValues', 'golang_timestamp': ( - b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\xff\xff')} + b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\xff\xff'), + 'time_zone_offset': 60} golang_timestamp = bytes.fromhex('01000000000000000200000003ffff') expected_date_time_object = golang_time.GolangTime( @@ -338,6 +351,17 @@ def testConvertJSONToDateTimeValues(self): json_dict) self.assertEqual(date_time_object, expected_date_time_object) + # Test if is_delta is removed. + json_dict = { + '__class_name__': 'PosixTime', + '__type__': 'DateTimeValues', + 'timestamp': 1281643591, + 'is_delta': True} + + date_time_object = serializer.Serializer.ConvertJSONToDateTimeValues( + json_dict) + self.assertFalse(date_time_object.is_delta) + with self.assertRaises(KeyError): json_dict = { '__class_name__': 'UnknownType', '__type__': 'DateTimeValues'} diff --git a/tests/time_elements.py b/tests/time_elements.py index 8b50b11..5e3dd88 100644 --- a/tests/time_elements.py +++ b/tests/time_elements.py @@ -450,6 +450,42 @@ def testCopyTimeFromStringRFC(self): with self.assertRaises(ValueError): time_elements_object._CopyTimeFromStringRFC('11:57:09', 'XXX') + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('', 'GMT') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('11 57', 'GMT') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('12:34:56:78', 'GMT') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('12.34:56', 'GMT') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('12:34.56', 'GMT') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('11:57', '+01000') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('11:57', 'ZZZ') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('11:57', '1100') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('11:57', '+A500') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('11:57', '+1600') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('11:57', '+01A0') + + with self.assertRaises(ValueError): + time_elements_object._CopyTimeFromStringRFC('11:57', '+0160') + def testCopyFromDatetime(self): """Tests the CopyFromDatetime function.""" time_elements_object = time_elements.TimeElements() From e8e57c27bf6df37bdae8889829aca623594ee9ac Mon Sep 17 00:00:00 2001 From: Joachim Metz Date: Wed, 13 May 2026 19:54:31 +0200 Subject: [PATCH 2/2] Worked on tests --- tests/time_elements.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tests/time_elements.py b/tests/time_elements.py index 5e3dd88..9de9270 100644 --- a/tests/time_elements.py +++ b/tests/time_elements.py @@ -450,20 +450,14 @@ def testCopyTimeFromStringRFC(self): with self.assertRaises(ValueError): time_elements_object._CopyTimeFromStringRFC('11:57:09', 'XXX') - with self.assertRaises(ValueError): - time_elements_object._CopyTimeFromStringRFC('', 'GMT') - - with self.assertRaises(ValueError): - time_elements_object._CopyTimeFromStringRFC('11 57', 'GMT') - with self.assertRaises(ValueError): time_elements_object._CopyTimeFromStringRFC('12:34:56:78', 'GMT') with self.assertRaises(ValueError): - time_elements_object._CopyTimeFromStringRFC('12.34:56', 'GMT') + time_elements_object._CopyTimeFromStringRFC('12X34:56', 'GMT') with self.assertRaises(ValueError): - time_elements_object._CopyTimeFromStringRFC('12:34.56', 'GMT') + time_elements_object._CopyTimeFromStringRFC('12:34X56', 'GMT') with self.assertRaises(ValueError): time_elements_object._CopyTimeFromStringRFC('11:57', '+01000') @@ -471,9 +465,6 @@ def testCopyTimeFromStringRFC(self): with self.assertRaises(ValueError): time_elements_object._CopyTimeFromStringRFC('11:57', 'ZZZ') - with self.assertRaises(ValueError): - time_elements_object._CopyTimeFromStringRFC('11:57', '1100') - with self.assertRaises(ValueError): time_elements_object._CopyTimeFromStringRFC('11:57', '+A500')