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