Debian Codegraph @

tests/factory.py

raw
1 #!/usr/bin/env python3
2 """Tests for the date and time values factory."""
3
4 import unittest
5
6 from dfdatetime import interface
7 from dfdatetime import factory
8
9
10 class TestDateTimeValues(interface.DateTimeValues):
11 """Date and time values for testing."""
12
13 # pylint: disable=redundant-returns-doc
14
15 def _GetNormalizedTimestamp(self):
16 """Retrieves the normalized timestamp.
17
18 Returns:
19 decimal.Decimal: normalized timestamp, which contains the number of
20 seconds since January 1, 1970 00:00:00 and a fraction of second used
21 for increased precision, or None if the normalized timestamp cannot be
22 determined.
23 """
24 return None
25
26 def CopyFromDateTimeString(self, time_string):
27 """Copies a date time value from a date and time string.
28
29 Args:
30 time_string (str): date and time value formatted as:
31 YYYY-MM-DD hh:mm:ss.######[+-]##:##
32
33 Where # are numeric digits ranging from 0 to 9 and the seconds
34 fraction can be either 3 or 6 digits. The time of day, seconds
35 fraction and time zone offset are optional. The default time zone
36 is UTC.
37
38 Raises:
39 ValueError: if the time string is invalid or not supported.
40 """
41 return
42
43 def CopyToDateTimeString(self):
44 """Copies the date time value to a date and time string.
45
46 Returns:
47 str: date and time value formatted as: "YYYY-MM-DD hh:mm:ss.######" or
48 None if the timestamp cannot be copied to a date and time string.
49 """
50 return None
51
52 def CopyToSerializableDict(self):
53 """Copies the date time value to a serializable dictionary.
54
55 Returns:
56 dict[str, object]: serializable dictionary.
57 """
58 return {
59 "__class_name__": type(self).__name__,
60 "__type__": "DateTimeValues",
61 }
62
63
64 class FactoryTest(unittest.TestCase):
65 """Tests the date and time values factory."""
66
67 def testDateTimeValuesRegistration(self):
68 """Tests the Register and DeregisterDateTimeValues functions."""
69 # pylint: disable=protected-access
70 number_of_date_time_values_types = len(factory.Factory._date_time_values_types)
71
72 factory.Factory.RegisterDateTimeValues(TestDateTimeValues)
73 self.assertEqual(
74 len(factory.Factory._date_time_values_types),
75 number_of_date_time_values_types + 1,
76 )
77
78 with self.assertRaises(KeyError):
79 factory.Factory.RegisterDateTimeValues(TestDateTimeValues)
80
81 factory.Factory.DeregisterDateTimeValues(TestDateTimeValues)
82 self.assertEqual(
83 len(factory.Factory._date_time_values_types),
84 number_of_date_time_values_types,
85 )
86
87 with self.assertRaises(KeyError):
88 factory.Factory.DeregisterDateTimeValues(TestDateTimeValues)
89
90 def testNewDateTimeValues(self):
91 """Tests the NewDateTimeValues function."""
92 test_date_time_values = factory.Factory.NewDateTimeValues(
93 "Filetime", timestamp=0x01CB3A623D0A17CE
94 )
95
96 self.assertIsNotNone(test_date_time_values)
97
98
99 if __name__ == "__main__":
100 unittest.main()