| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Tests for the OLE Automation date implementation.""" |
| 3 |
|
| 4 |
import decimal |
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfdatetime import ole_automation_date |
| 8 |
|
| 9 |
|
| 10 |
class OLEAutomationDateEpochTest(unittest.TestCase): |
| 11 |
"""Tests for the OLE Automation date epoch.""" |
| 12 |
|
| 13 |
def testInitialize(self): |
| 14 |
"""Tests the __init__ function.""" |
| 15 |
ole_automation_date_epoch = ole_automation_date.OLEAutomationDateEpoch() |
| 16 |
self.assertIsNotNone(ole_automation_date_epoch) |
| 17 |
|
| 18 |
|
| 19 |
class OLEAutomationDateTest(unittest.TestCase): |
| 20 |
"""Tests for the OLE Automation date.""" |
| 21 |
|
| 22 |
# pylint: disable=protected-access |
| 23 |
|
| 24 |
def testProperties(self): |
| 25 |
"""Tests the properties.""" |
| 26 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 27 |
timestamp=43044.480556 |
| 28 |
) |
| 29 |
self.assertEqual(ole_automation_date_object.timestamp, 43044.480556) |
| 30 |
|
| 31 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate() |
| 32 |
self.assertIsNone(ole_automation_date_object.timestamp) |
| 33 |
|
| 34 |
def testGetNormalizedTimestamp(self): |
| 35 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 36 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 37 |
timestamp=43044.480556 |
| 38 |
) |
| 39 |
expected_normalized_timestamp = decimal.Decimal("1509881520.038400194607675076") |
| 40 |
normalized_timestamp = ole_automation_date_object._GetNormalizedTimestamp() |
| 41 |
self.assertEqual(normalized_timestamp, expected_normalized_timestamp) |
| 42 |
|
| 43 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 44 |
time_zone_offset=60, timestamp=43044.480556 |
| 45 |
) |
| 46 |
expected_normalized_timestamp = decimal.Decimal("1509877920.038400194607675076") |
| 47 |
normalized_timestamp = ole_automation_date_object._GetNormalizedTimestamp() |
| 48 |
self.assertEqual(normalized_timestamp, expected_normalized_timestamp) |
| 49 |
|
| 50 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 51 |
timestamp=43044.480556 |
| 52 |
) |
| 53 |
ole_automation_date_object.time_zone_offset = 60 |
| 54 |
|
| 55 |
expected_normalized_timestamp = decimal.Decimal("1509877920.038400194607675076") |
| 56 |
normalized_timestamp = ole_automation_date_object._GetNormalizedTimestamp() |
| 57 |
self.assertEqual(normalized_timestamp, expected_normalized_timestamp) |
| 58 |
|
| 59 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate() |
| 60 |
|
| 61 |
normalized_timestamp = ole_automation_date_object._GetNormalizedTimestamp() |
| 62 |
self.assertIsNone(normalized_timestamp) |
| 63 |
|
| 64 |
def testCopyFromDateTimeString(self): |
| 65 |
"""Tests the CopyFromDateTimeString function.""" |
| 66 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate() |
| 67 |
|
| 68 |
ole_automation_date_object.CopyFromDateTimeString("2017-11-05") |
| 69 |
self.assertEqual(ole_automation_date_object._timestamp, 43044.0) |
| 70 |
self.assertEqual(ole_automation_date_object._time_zone_offset, None) |
| 71 |
|
| 72 |
ole_automation_date_object.CopyFromDateTimeString("2017-11-05 11:32:00") |
| 73 |
self.assertEqual(ole_automation_date_object._timestamp, 43044.48055555555) |
| 74 |
self.assertEqual(ole_automation_date_object._time_zone_offset, None) |
| 75 |
|
| 76 |
ole_automation_date_object.CopyFromDateTimeString("2017-11-05 11:32:00.546875") |
| 77 |
self.assertEqual(ole_automation_date_object._timestamp, 43044.480561885124) |
| 78 |
self.assertEqual(ole_automation_date_object._time_zone_offset, None) |
| 79 |
|
| 80 |
ole_automation_date_object.CopyFromDateTimeString( |
| 81 |
"2017-11-05 11:32:00.546875-01:00" |
| 82 |
) |
| 83 |
self.assertEqual(ole_automation_date_object._timestamp, 43044.480561885124) |
| 84 |
self.assertEqual(ole_automation_date_object._time_zone_offset, -60) |
| 85 |
|
| 86 |
ole_automation_date_object.CopyFromDateTimeString( |
| 87 |
"2017-11-05 11:32:00.546875+01:00" |
| 88 |
) |
| 89 |
self.assertEqual(ole_automation_date_object._timestamp, 43044.480561885124) |
| 90 |
self.assertEqual(ole_automation_date_object._time_zone_offset, 60) |
| 91 |
|
| 92 |
ole_automation_date_object.CopyFromDateTimeString("1900-01-01 00:00:00") |
| 93 |
self.assertEqual(ole_automation_date_object._timestamp, 2.0) |
| 94 |
self.assertEqual(ole_automation_date_object._time_zone_offset, None) |
| 95 |
|
| 96 |
def testCopyToDateTimeString(self): |
| 97 |
"""Tests the CopyToDateTimeString function.""" |
| 98 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 99 |
timestamp=43044.480556 |
| 100 |
) |
| 101 |
date_time_string = ole_automation_date_object.CopyToDateTimeString() |
| 102 |
self.assertEqual(date_time_string, "2017-11-05 11:32:00.038400") |
| 103 |
|
| 104 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate() |
| 105 |
|
| 106 |
date_time_string = ole_automation_date_object.CopyToDateTimeString() |
| 107 |
self.assertIsNone(date_time_string) |
| 108 |
|
| 109 |
def testCopyToDateTimeStringISO8601(self): |
| 110 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 111 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 112 |
timestamp=43044.480556 |
| 113 |
) |
| 114 |
date_time_string = ole_automation_date_object.CopyToDateTimeStringISO8601() |
| 115 |
self.assertEqual(date_time_string, "2017-11-05T11:32:00.038400+00:00") |
| 116 |
|
| 117 |
def testCopyToSerializableDict(self): |
| 118 |
"""Test the CopyToSerializableDict function.""" |
| 119 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 120 |
timestamp=43044.480556 |
| 121 |
) |
| 122 |
expected_serializable_dict = { |
| 123 |
"__class_name__": "OLEAutomationDate", |
| 124 |
"__type__": "DateTimeValues", |
| 125 |
"timestamp": 43044.480556, |
| 126 |
} |
| 127 |
serializable_dict = ole_automation_date_object.CopyToSerializableDict() |
| 128 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 129 |
|
| 130 |
def testGetDate(self): |
| 131 |
"""Tests the GetDate function.""" |
| 132 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 133 |
timestamp=43044.480556 |
| 134 |
) |
| 135 |
date_tuple = ole_automation_date_object.GetDate() |
| 136 |
self.assertEqual(date_tuple, (2017, 11, 5)) |
| 137 |
|
| 138 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate() |
| 139 |
|
| 140 |
date_tuple = ole_automation_date_object.GetDate() |
| 141 |
self.assertEqual(date_tuple, (None, None, None)) |
| 142 |
|
| 143 |
def testGetDateWithTimeOfDay(self): |
| 144 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 145 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 146 |
timestamp=43044.480556 |
| 147 |
) |
| 148 |
date_with_time_of_day_tuple = ole_automation_date_object.GetDateWithTimeOfDay() |
| 149 |
self.assertEqual(date_with_time_of_day_tuple, (2017, 11, 5, 11, 32, 0)) |
| 150 |
|
| 151 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate() |
| 152 |
|
| 153 |
date_with_time_of_day_tuple = ole_automation_date_object.GetDateWithTimeOfDay() |
| 154 |
self.assertEqual( |
| 155 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 156 |
) |
| 157 |
|
| 158 |
def testGetTimeOfDay(self): |
| 159 |
"""Tests the GetTimeOfDay function.""" |
| 160 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate( |
| 161 |
timestamp=43044.480556 |
| 162 |
) |
| 163 |
time_of_day_tuple = ole_automation_date_object.GetTimeOfDay() |
| 164 |
self.assertEqual(time_of_day_tuple, (11, 32, 0)) |
| 165 |
|
| 166 |
ole_automation_date_object = ole_automation_date.OLEAutomationDate() |
| 167 |
|
| 168 |
time_of_day_tuple = ole_automation_date_object.GetTimeOfDay() |
| 169 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 170 |
|
| 171 |
|
| 172 |
if __name__ == "__main__": |
| 173 |
unittest.main() |