-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
executable file
·275 lines (192 loc) · 8.38 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
'''
python -m unittest tests.py
'''
import unittest
import attridict
class TestAttriDict(unittest.TestCase):
def test_attridict(self):
# test growth through assignment
att = attridict()
self.assertEqual(att, {})
att.one = 111
self.assertEqual(att.one, 111)
self.assertEqual(att, {"one": 111})
att.two = {"three": 333}
self.assertEqual(att.two, {"three": 333})
self.assertEqual(att, {"one": 111, "two": {"three": 333}})
att.two.four = {"five": 555, "six": 666}
self.assertEqual(att.two.four.five, 555)
self.assertEqual(att.two.four.six, 666)
self.assertEqual(att, {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}})
def test_attridict2(self):
# test conversion from dict
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
self.assertEqual(att.one, 111)
self.assertEqual(att.two, {"three": 333, "four": {"five": 555, "six": 666}})
self.assertEqual(att.two.three, 333)
self.assertEqual(att.two.four, {"five": 555, "six": 666})
self.assertEqual(att.two.four.five, 555)
self.assertEqual(att.two.four.six, 666)
# test reassignment of assigned values
att.two = 222
self.assertEqual(att.two, 222)
att.three = {"four": 444, "five": 555}
self.assertEqual(att.three, {"four": 444, "five": 555})
self.assertEqual(att, {"one": 111, "two": 222, "three": {"four": 444, "five": 555}})
def test_attridict3(self):
# test value conservativity of converted dict
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
self.assertEqual(att, data)
self.assertEqual(att.one, data["one"])
self.assertEqual(att.two, data["two"])
self.assertEqual(att.two.three, data["two"]["three"])
self.assertEqual(att.two.four, data["two"]["four"])
self.assertEqual(att.two.four.five, data["two"]["four"]["five"])
self.assertEqual(att.two.four.six, data["two"]['four']["six"])
def test_attridict5(self):
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
att.three = {"four": 444, "five": 565}
del att
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
self.assertEqual(data.__len__(), att.__len__())
self.assertEqual(data["two"].__len__(), att.two.__len__())
self.assertEqual(data["two"]["four"].__len__(), att.two.four.__len__())
def test_attridict6(self):
# test equality of attridict and initialized dict
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
self.assertEqual(att, data)
def test_mutable(self):
# test mutability of an attridict object
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
att_mutable = att
att.two = 222
self.assertEqual(att, att_mutable)
self.assertIs(att, att_mutable)
def test_copy(self):
# test attridict copy is not equal to original
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
att_copy = att.copy()
self.assertIsNot(att, att_copy)
att.two = 222
self.assertNotEqual(att, att_copy)
self.assertEqual(att_copy, data)
self.assertEqual(att_copy.two, {"three": 333, "four": {"five": 555, "six": 666}})
def test_to_dict(self):
# test to_dict() returns dict
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
att_dict = att.to_dict()
self.assertEqual(data, att_dict)
self.assertEqual(type(att_dict), dict)
self.assertEqual(type(att_dict["two"]), dict)
self.assertEqual(type(att_dict["two"]["four"]), dict)
def test_attridict10(self):
# test del deletes attribute
data = {"one": 111, "two": {"three": 333, "four": {"five": 555, "six": 666}}}
att = attridict(data)
del att.two.four.six
self.assertEqual(att, {"one": 111, "two": {"three": 333, "four": {"five": 555}}})
def test_attridict11(self):
data = {}
data["me"] = data
att = attridict(data)
self.assertEqual(att, data)
self.assertEqual(att.me, data)
self.assertEqual(att.me.me, data)
def test_attridict12(self):
one = {"one": "me"}
two = {"two": "me"}
one["two"] = two
two["one"] = one
att_one = attridict(one)
att_two = attridict(two)
self.assertEqual(att_one.one, "me")
self.assertEqual(att_one.two, two)
self.assertEqual(att_one.two, att_two)
self.assertEqual(att_two.two, "me")
self.assertEqual(att_two.one, one)
self.assertEqual(att_two.one, att_one)
def test_attridict14(self):
data = {"one": 111, "two": [1,2,3], "three": {4,5,6}, "four": (7,8,9)}
att = attridict(data)
self.assertEqual(att, data)
self.assertEqual(att.two, [1,2,3])
self.assertEqual(att.three, {4,5,6})
self.assertEqual(att.four, (7,8,9))
def test_call(self):
# test object call with key argument returns corresponding value
data = {"one": 111, "two": {"three": 333}}
att = attridict(data)
self.assertEqual(att("one"), 111)
self.assertEqual(att("two"), {"three": 333})
self.assertEqual(att.two("three"), 333)
def test_add(self):
# test adding dict object and attridict object together
data1 = {"one": 111, "two": 222}
att1 = attridict(data1)
data2 = {"three": 333, "four": 444}
att2 = attridict(data2)
self.assertEqual(att1 + data2, {"one": 111, "two": 222, "three": 333, "four": 444})
self.assertEqual(data1 + att2, {"one": 111, "two": 222, "three": 333, "four": 444})
self.assertEqual(att1 + att2, {"one": 111, "two": 222, "three": 333, "four": 444})
def test_kwargs(self):
data = {"one": 111, "two": 222}
att = attridict(data, three=333, four=444)
self.assertEqual(att, {"one":111, "two":222, "three": 333, "four": 444})
def test_kwargs_overwrite(self):
# test keyword argument overwrites original argument
data = {"one": 111, "two": 222}
att = attridict(data, two=333)
self.assertEqual(att, {"one": 111, "two": 333})
def test_getattr(self):
# test getattr function returns expected output
data = {"one": 111, "two": 222}
att = attridict(data)
self.assertEqual(getattr(att, "one"), 111)
self.assertEqual(getattr(att, "one", 333), 111)
self.assertEqual(getattr(att, "three", 333), 333)
def test_pickle(self):
# test that the object can be pickled
import pickle
data = {"one": 111, "two": 222}
att = attridict(data)
try:
pickled_att = pickle.dumps(att)
unpickled_att = pickle.loads(pickled_att)
self.assertEqual(att, unpickled_att)
except Exception as e:
self.fail(e)
def test_yaml_serialization(self):
# test that an attridict object can be serialized with yaml
import yaml
data = {"one": 111, "two": 222}
att = attridict(data)
try:
yaml_serialized_att = yaml.dump(att)
yaml_att = yaml.load(yaml_serialized_att, Loader=yaml.FullLoader)
self.assertEqual(att, yaml_att)
self.assertEqual(type(att), type(yaml_att))
except Exception as e:
self.fail(e)
def test_yaml_safe_serialization(self):
# test that an attridict object can be safely serialized with yaml
import yaml
data = {"one": 111, "two": 222}
att = attridict(data)
try:
yaml_serialized_att = yaml.safe_dump(att)
yaml_att = yaml.safe_load(yaml_serialized_att)
self.assertEqual(data, yaml_att)
self.assertEqual(type(data), type(yaml_att))
except Exception as e:
self.fail(e)
if __name__ == "__main__":
unittest.main()