| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Tests for the UUID version 1 time implementation.""" |
| 3 |
|
| 4 |
import decimal |
| 5 |
import uuid |
| 6 |
import unittest |
| 7 |
|
| 8 |
from dfdatetime import uuid_time |
| 9 |
|
| 10 |
|
| 11 |
class UUIDTimeEpochTEst(unittest.TestCase): |
| 12 |
"""Tests for the UUID version 1 time time epoch.""" |
| 13 |
|
| 14 |
def testInitialize(self): |
| 15 |
"""Tests the __init__ function.""" |
| 16 |
uuid_time_epoch = uuid_time.UUIDTimeEpoch() |
| 17 |
self.assertIsNotNone(uuid_time_epoch) |
| 18 |
|
| 19 |
|
| 20 |
class UUIDTimeTest(unittest.TestCase): |
| 21 |
"""Tests for the UUID version 1 timestamp.""" |
| 22 |
|
| 23 |
# pylint: disable=protected-access |
| 24 |
|
| 25 |
def testInitialize(self): |
| 26 |
"""Tests the initialization function.""" |
| 27 |
uuid_time_object = uuid_time.UUIDTime() |
| 28 |
self.assertIsNotNone(uuid_time_object) |
| 29 |
|
| 30 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 31 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 32 |
self.assertIsNotNone(uuid_time_object) |
| 33 |
expected_timestamp = 135564234616544084 |
| 34 |
self.assertEqual(uuid_time_object.timestamp, expected_timestamp) |
| 35 |
|
| 36 |
with self.assertRaises(ValueError): |
| 37 |
uuid_time.UUIDTime(timestamp=0x1FFFFFFFFFFFFFFF) |
| 38 |
|
| 39 |
with self.assertRaises(ValueError): |
| 40 |
uuid_time.UUIDTime(timestamp=-1) |
| 41 |
|
| 42 |
def testProperties(self): |
| 43 |
"""Tests the properties.""" |
| 44 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 45 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 46 |
self.assertEqual(uuid_time_object.timestamp, 0x1E19EF400911B54) |
| 47 |
|
| 48 |
uuid_time_object = uuid_time.UUIDTime() |
| 49 |
self.assertIsNone(uuid_time_object.timestamp) |
| 50 |
|
| 51 |
def testGetNormalizedTimestamp(self): |
| 52 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 53 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 54 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 55 |
|
| 56 |
normalized_timestamp = uuid_time_object._GetNormalizedTimestamp() |
| 57 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1337130661.6544084")) |
| 58 |
|
| 59 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 60 |
uuid_time_object = uuid_time.UUIDTime( |
| 61 |
time_zone_offset=60, timestamp=uuid_object.time |
| 62 |
) |
| 63 |
normalized_timestamp = uuid_time_object._GetNormalizedTimestamp() |
| 64 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1337127061.6544084")) |
| 65 |
|
| 66 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 67 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 68 |
uuid_time_object.time_zone_offset = 60 |
| 69 |
|
| 70 |
normalized_timestamp = uuid_time_object._GetNormalizedTimestamp() |
| 71 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1337127061.6544084")) |
| 72 |
|
| 73 |
uuid_time_object = uuid_time.UUIDTime() |
| 74 |
uuid_time_object._timestamp = 0x1FFFFFFFFFFFFFFF |
| 75 |
|
| 76 |
normalized_timestamp = uuid_time_object._GetNormalizedTimestamp() |
| 77 |
self.assertIsNone(normalized_timestamp) |
| 78 |
|
| 79 |
uuid_time_object = uuid_time.UUIDTime() |
| 80 |
uuid_time_object._timestamp = -1 |
| 81 |
|
| 82 |
normalized_timestamp = uuid_time_object._GetNormalizedTimestamp() |
| 83 |
self.assertIsNone(normalized_timestamp) |
| 84 |
|
| 85 |
uuid_time_object = uuid_time.UUIDTime() |
| 86 |
|
| 87 |
normalized_timestamp = uuid_time_object._GetNormalizedTimestamp() |
| 88 |
self.assertIsNone(normalized_timestamp) |
| 89 |
|
| 90 |
def testCopyFromDateTimeString(self): |
| 91 |
"""Tests the CopyFromDateTimeString function.""" |
| 92 |
uuid_time_object = uuid_time.UUIDTime() |
| 93 |
|
| 94 |
uuid_time_object.CopyFromDateTimeString("2013-08-01") |
| 95 |
self.assertEqual(uuid_time_object._timestamp, 135946080000000000) |
| 96 |
self.assertEqual(uuid_time_object._time_zone_offset, None) |
| 97 |
|
| 98 |
uuid_time_object.CopyFromDateTimeString("2013-08-01 15:25:28") |
| 99 |
self.assertEqual(uuid_time_object._timestamp, 135946635280000000) |
| 100 |
self.assertEqual(uuid_time_object._time_zone_offset, None) |
| 101 |
|
| 102 |
uuid_time_object.CopyFromDateTimeString("2013-08-01 15:25:28.546875") |
| 103 |
self.assertEqual(uuid_time_object._timestamp, 135946635285468750) |
| 104 |
self.assertEqual(uuid_time_object._time_zone_offset, None) |
| 105 |
|
| 106 |
uuid_time_object.CopyFromDateTimeString("2013-08-01 15:25:28.546875-01:00") |
| 107 |
self.assertEqual(uuid_time_object._timestamp, 135946635285468750) |
| 108 |
self.assertEqual(uuid_time_object._time_zone_offset, -60) |
| 109 |
|
| 110 |
uuid_time_object.CopyFromDateTimeString("2013-08-01 15:25:28.546875+01:00") |
| 111 |
self.assertEqual(uuid_time_object._timestamp, 135946635285468750) |
| 112 |
self.assertEqual(uuid_time_object._time_zone_offset, 60) |
| 113 |
|
| 114 |
uuid_time_object.CopyFromDateTimeString("1582-10-16 00:00:00") |
| 115 |
self.assertEqual(uuid_time_object._timestamp, 864000000000) |
| 116 |
self.assertEqual(uuid_time_object._time_zone_offset, None) |
| 117 |
|
| 118 |
with self.assertRaises(ValueError): |
| 119 |
uuid_time_object.CopyFromDateTimeString("1570-01-02 00:00:00") |
| 120 |
|
| 121 |
def testCopyToDateTimeString(self): |
| 122 |
"""Tests the CopyToDateTimeString function.""" |
| 123 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 124 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 125 |
|
| 126 |
date_time_string = uuid_time_object.CopyToDateTimeString() |
| 127 |
self.assertEqual(date_time_string, "2012-05-16 01:11:01.6544084") |
| 128 |
|
| 129 |
uuid_time_object = uuid_time.UUIDTime() |
| 130 |
|
| 131 |
date_time_string = uuid_time_object.CopyToDateTimeString() |
| 132 |
self.assertIsNone(date_time_string) |
| 133 |
|
| 134 |
def testCopyToDateTimeStringISO8601(self): |
| 135 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 136 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 137 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 138 |
|
| 139 |
date_time_string = uuid_time_object.CopyToDateTimeStringISO8601() |
| 140 |
self.assertEqual(date_time_string, "2012-05-16T01:11:01.6544084+00:00") |
| 141 |
|
| 142 |
def testCopyToSerializableDict(self): |
| 143 |
"""Test the CopyToSerializableDict function.""" |
| 144 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 145 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 146 |
|
| 147 |
expected_serializable_dict = { |
| 148 |
"__class_name__": "UUIDTime", |
| 149 |
"__type__": "DateTimeValues", |
| 150 |
"timestamp": 135564234616544084, |
| 151 |
} |
| 152 |
serializable_dict = uuid_time_object.CopyToSerializableDict() |
| 153 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 154 |
|
| 155 |
def testGetDate(self): |
| 156 |
"""Tests the GetDate function.""" |
| 157 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 158 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 159 |
|
| 160 |
date_tuple = uuid_time_object.GetDate() |
| 161 |
self.assertEqual(date_tuple, (2012, 5, 16)) |
| 162 |
|
| 163 |
uuid_time_object = uuid_time.UUIDTime() |
| 164 |
|
| 165 |
date_tuple = uuid_time_object.GetDate() |
| 166 |
self.assertEqual(date_tuple, (None, None, None)) |
| 167 |
|
| 168 |
def testGetDateWithTimeOfDay(self): |
| 169 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 170 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 171 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 172 |
|
| 173 |
date_with_time_of_day_tuple = uuid_time_object.GetDateWithTimeOfDay() |
| 174 |
self.assertEqual(date_with_time_of_day_tuple, (2012, 5, 16, 1, 11, 1)) |
| 175 |
|
| 176 |
uuid_time_object = uuid_time.UUIDTime() |
| 177 |
|
| 178 |
date_with_time_of_day_tuple = uuid_time_object.GetDateWithTimeOfDay() |
| 179 |
self.assertEqual( |
| 180 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 181 |
) |
| 182 |
|
| 183 |
def testGetTimeOfDay(self): |
| 184 |
"""Tests the GetTimeOfDay function.""" |
| 185 |
uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") |
| 186 |
uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) |
| 187 |
|
| 188 |
time_of_day_tuple = uuid_time_object.GetTimeOfDay() |
| 189 |
self.assertEqual(time_of_day_tuple, (1, 11, 1)) |
| 190 |
|
| 191 |
uuid_time_object = uuid_time.UUIDTime() |
| 192 |
|
| 193 |
time_of_day_tuple = uuid_time_object.GetTimeOfDay() |
| 194 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 195 |
|
| 196 |
|
| 197 |
if __name__ == "__main__": |
| 198 |
unittest.main() |