Skip to content

Commit af859cd

Browse files
authored
Worked on tests (#306)
1 parent df6f79d commit af859cd

4 files changed

Lines changed: 60 additions & 4 deletions

File tree

dfdatetime/golang_time.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _GetNumberOfSeconds(self, golang_timestamp):
141141

142142
# TODO: add support for version 2 time zone offset in seconds
143143

144-
except struct.error as exception:
144+
except (TypeError, struct.error) as exception:
145145
raise ValueError((
146146
f'Unable to unpacked Golang time.Time timestamp with error: '
147147
f'{exception!s}'))

tests/golang_time.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def testGetNumberOfSeconds(self):
136136
golang_time_object._GetNumberOfSeconds(golang_timestamp)
137137

138138
with self.assertRaises(ValueError):
139-
golang_timestamp = bytes.fromhex('ffffffffffffffffffffffffffff01')
139+
golang_timestamp = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
140140
golang_time_object._GetNumberOfSeconds(golang_timestamp)
141141

142142
def testCopyFromDateTimeString(self):
@@ -174,8 +174,13 @@ def testCopyFromDateTimeString(self):
174174
self.assertEqual(golang_time_object._nanoseconds, 567890000)
175175
self.assertEqual(golang_time_object._time_zone_offset, 60)
176176

177+
# Test with invalid date string.
177178
with self.assertRaises(ValueError):
178-
golang_time_object.CopyFromDateTimeString('-0001-01-01')
179+
golang_time_object.CopyFromDateTimeString('0001:01-01 00:01:00')
180+
181+
# Test with negative year.
182+
with self.assertRaises(ValueError):
183+
golang_time_object.CopyFromDateTimeString('-001-01-01 00:01:00')
179184

180185
def testCopyToDateTimeString(self):
181186
"""Test the CopyToDateTimeString function."""

tests/serializer.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ def testConvertDateTimeValuesToJSON(self):
138138
rfc2579_date_time_object)
139139
self.assertEqual(json_dict, expected_json_dict)
140140

141+
negative_timezone_rfc2579_object = rfc2579_date_time.RFC2579DateTime(
142+
rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '-', 2, 0))
143+
144+
expected_json_dict = {
145+
'__class_name__': 'RFC2579DateTime',
146+
'__type__': 'DateTimeValues',
147+
'rfc2579_date_time_tuple': (2010, 8, 12, 20, 6, 31, 6, '-', 2, 0)}
148+
149+
json_dict = serializer.Serializer.ConvertDateTimeValuesToJSON(
150+
negative_timezone_rfc2579_object)
151+
self.assertEqual(json_dict, expected_json_dict)
152+
141153
systemtime_object = systemtime.Systemtime(
142154
system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142))
143155

@@ -266,7 +278,8 @@ def testConvertJSONToDateTimeValues(self):
266278
'__class_name__': 'GolangTime',
267279
'__type__': 'DateTimeValues',
268280
'golang_timestamp': (
269-
b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\xff\xff')}
281+
b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\xff\xff'),
282+
'time_zone_offset': 60}
270283

271284
golang_timestamp = bytes.fromhex('01000000000000000200000003ffff')
272285
expected_date_time_object = golang_time.GolangTime(
@@ -338,6 +351,17 @@ def testConvertJSONToDateTimeValues(self):
338351
json_dict)
339352
self.assertEqual(date_time_object, expected_date_time_object)
340353

354+
# Test if is_delta is removed.
355+
json_dict = {
356+
'__class_name__': 'PosixTime',
357+
'__type__': 'DateTimeValues',
358+
'timestamp': 1281643591,
359+
'is_delta': True}
360+
361+
date_time_object = serializer.Serializer.ConvertJSONToDateTimeValues(
362+
json_dict)
363+
self.assertFalse(date_time_object.is_delta)
364+
341365
with self.assertRaises(KeyError):
342366
json_dict = {
343367
'__class_name__': 'UnknownType', '__type__': 'DateTimeValues'}

tests/time_elements.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,33 @@ def testCopyTimeFromStringRFC(self):
450450
with self.assertRaises(ValueError):
451451
time_elements_object._CopyTimeFromStringRFC('11:57:09', 'XXX')
452452

453+
with self.assertRaises(ValueError):
454+
time_elements_object._CopyTimeFromStringRFC('12:34:56:78', 'GMT')
455+
456+
with self.assertRaises(ValueError):
457+
time_elements_object._CopyTimeFromStringRFC('12X34:56', 'GMT')
458+
459+
with self.assertRaises(ValueError):
460+
time_elements_object._CopyTimeFromStringRFC('12:34X56', 'GMT')
461+
462+
with self.assertRaises(ValueError):
463+
time_elements_object._CopyTimeFromStringRFC('11:57', '+01000')
464+
465+
with self.assertRaises(ValueError):
466+
time_elements_object._CopyTimeFromStringRFC('11:57', 'ZZZ')
467+
468+
with self.assertRaises(ValueError):
469+
time_elements_object._CopyTimeFromStringRFC('11:57', '+A500')
470+
471+
with self.assertRaises(ValueError):
472+
time_elements_object._CopyTimeFromStringRFC('11:57', '+1600')
473+
474+
with self.assertRaises(ValueError):
475+
time_elements_object._CopyTimeFromStringRFC('11:57', '+01A0')
476+
477+
with self.assertRaises(ValueError):
478+
time_elements_object._CopyTimeFromStringRFC('11:57', '+0160')
479+
453480
def testCopyFromDatetime(self):
454481
"""Tests the CopyFromDatetime function."""
455482
time_elements_object = time_elements.TimeElements()

0 commit comments

Comments
 (0)