This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
86 lines (73 loc) · 4.21 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Create your tests here.
from unittest import TestCase
from external.types import NamedTupleFactory
import json
from external.timeutils import iso_strptime, iso_utcz_strftime
import datetime
import pytz
class NamedTupleFactoryTest(TestCase):
def setUp(self):
super(NamedTupleFactoryTest, self).setUp()
self.simple_type_factory = NamedTupleFactory("SimpleType", ["a","b"])
self.nested_type_factory = NamedTupleFactory("NestedType", ["c", "simple"],
decoders={"simple": self.simple_type_factory})
self.date_type_factory = NamedTupleFactory("DateType", ["e","t"],
decoders={"t": iso_strptime},
encoders={"t": iso_utcz_strftime})
self.complex_type_factory = NamedTupleFactory("ComplexNestedType", ["f", "complex"],
decoders={"complex": self.date_type_factory})
def test_that_a_dictionary_can_be_converted_into_a_namedtuple(self):
some_dict = {"a": 1, "b": 2}
my_instance = self.simple_type_factory(some_dict)
self.assertEqual(my_instance.a, 1)
self.assertEqual(my_instance.b, 2)
def test_that_unspecified_keys_are_discarded(self):
some_dict = {"a": 1, "b": 2, "c": 3}
my_instance = self.simple_type_factory(some_dict)
self.assertEqual(my_instance.a, 1)
self.assertEqual(my_instance.b, 2)
self.assertFalse(hasattr(my_instance, "c"))
def test_that_nested_types_are_correctly_mapped(self):
some_dict = {"c": 3, "simple": {"a": 1, "b": 2}}
my_instance = self.nested_type_factory(some_dict)
self.assertEqual(my_instance.c, 3)
self.assertEqual(my_instance.simple.a, 1)
self.assertEqual(my_instance.simple.b, 2)
def test_that_json_serialized_values_are_compact(self):
some_dict = {"c": 3, "simple": {"a": 1, "b": 2}}
my_instance = self.nested_type_factory(some_dict)
serialized = json.dumps(self.nested_type_factory.to_python(my_instance))
original_serialized = json.dumps(some_dict)
self.assertEqual(len(serialized), 11)
self.assertLess(len(serialized), len(original_serialized))
def test_that_nested_types_can_be_reconstituted_from_json(self):
some_dict = {"c": 3, "simple": {"a": 1, "b": 2}}
my_instance = self.nested_type_factory(some_dict)
serialized = json.dumps(self.nested_type_factory.to_python(my_instance))
deserialized = json.loads(serialized)
reconstituted = self.nested_type_factory(deserialized)
self.assertEqual(my_instance, reconstituted)
def test_that_complex_types_can_be_decoded(self):
some_dict = {"e": 1, "t": "2019-02-26T00:00:01Z"}
my_instance = self.date_type_factory.from_python(some_dict)
self.assertEqual(my_instance.e, 1)
self.assertEqual(my_instance.t, datetime.datetime(2019, 2, 26, 0, 0, 1, tzinfo=pytz.timezone('utc')))
def test_that_complex_types_can_be_encoded(self):
some_dict = {"e": 1, "t": "2019-02-26T00:00:01Z"}
my_instance = self.date_type_factory.from_python(some_dict)
serializable = self.date_type_factory.to_python(my_instance)
self.assertEqual(serializable.e, 1)
self.assertEqual(serializable.t, "2019-02-26T00:00:01Z")
def test_that_complex_nested_types_can_be_decoded(self):
some_dict = {"f": 2, "complex":{"e": 1, "t": "2019-02-26T00:00:01Z"}}
my_instance = self.complex_type_factory.from_python(some_dict)
self.assertEqual(my_instance.f, 2)
self.assertEqual(my_instance.complex.e, 1)
self.assertEqual(my_instance.complex.t, datetime.datetime(2019, 2, 26, 0, 0, 1, tzinfo=pytz.timezone('utc')))
def test_that_complex_nested_types_can_be_decoded_and_re_encoded(self):
some_dict = {"f": 2, "complex":{"e": 1, "t": "2019-02-26T00:00:01Z"}}
decoded = self.complex_type_factory.from_python(some_dict)
serializable = self.complex_type_factory.to_python(decoded)
self.assertEqual(serializable.f, 2)
self.assertEqual(serializable.complex.e, 1)
self.assertEqual(serializable.complex.t, "2019-02-26T00:00:01Z")