-
Notifications
You must be signed in to change notification settings - Fork 132
/
test_addict.py
592 lines (464 loc) · 17.1 KB
/
test_addict.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import json
import copy
import unittest
import pickle
from addict import Dict
# test whether unittests pass on child classes
class CHILD_CLASS(Dict):
child_class_attribute = 'child class attribute'
def child_instance_attribute(self):
return 'child instance attribute'
TEST_VAL = [1, 2, 3]
TEST_DICT = {'a': {'b': {'c': TEST_VAL}}}
class AbstractTestsClass(object):
dict_class = None
def test_set_one_level_item(self):
some_dict = {'a': TEST_VAL}
prop = self.dict_class()
prop['a'] = TEST_VAL
self.assertDictEqual(prop, some_dict)
def test_set_two_level_items(self):
some_dict = {'a': {'b': TEST_VAL}}
prop = self.dict_class()
prop['a']['b'] = TEST_VAL
self.assertDictEqual(prop, some_dict)
def test_set_three_level_items(self):
prop = self.dict_class()
prop['a']['b']['c'] = TEST_VAL
self.assertDictEqual(prop, TEST_DICT)
def test_set_one_level_property(self):
prop = self.dict_class()
prop.a = TEST_VAL
self.assertDictEqual(prop, {'a': TEST_VAL})
def test_set_two_level_properties(self):
prop = self.dict_class()
prop.a.b = TEST_VAL
self.assertDictEqual(prop, {'a': {'b': TEST_VAL}})
def test_set_three_level_properties(self):
prop = self.dict_class()
prop.a.b.c = TEST_VAL
self.assertDictEqual(prop, TEST_DICT)
def test_init_with_dict(self):
self.assertDictEqual(TEST_DICT, Dict(TEST_DICT))
def test_init_with_kws(self):
prop = self.dict_class(a=2, b={'a': 2}, c=[{'a': 2}])
self.assertDictEqual(prop, {'a': 2, 'b': {'a': 2}, 'c': [{'a': 2}]})
def test_init_with_tuples(self):
prop = self.dict_class((0, 1), (1, 2), (2, 3))
self.assertDictEqual(prop, {0: 1, 1: 2, 2: 3})
def test_init_with_list(self):
prop = self.dict_class([(0, 1), (1, 2), (2, 3)])
self.assertDictEqual(prop, {0: 1, 1: 2, 2: 3})
def test_init_with_generator(self):
prop = self.dict_class(((i, i + 1) for i in range(3)))
self.assertDictEqual(prop, {0: 1, 1: 2, 2: 3})
def test_init_with_tuples_and_empty_list(self):
prop = self.dict_class((0, 1), [], (2, 3))
self.assertDictEqual(prop, {0: 1, 2: 3})
def test_init_raises(self):
def init():
self.dict_class(5)
def init2():
Dict('a')
self.assertRaises(TypeError, init)
self.assertRaises(ValueError, init2)
def test_init_with_empty_stuff(self):
a = self.dict_class({})
b = self.dict_class([])
self.assertDictEqual(a, {})
self.assertDictEqual(b, {})
def test_init_with_list_of_dicts(self):
a = self.dict_class({'a': [{'b': 2}]})
self.assertIsInstance(a.a[0], self.dict_class)
self.assertEqual(a.a[0].b, 2)
def test_init_with_kwargs(self):
a = self.dict_class(a='b', c=dict(d='e', f=dict(g='h')))
self.assertEqual(a.a, 'b')
self.assertIsInstance(a.c, self.dict_class)
self.assertEqual(a.c.f.g, 'h')
self.assertIsInstance(a.c.f, self.dict_class)
def test_getitem(self):
prop = self.dict_class(TEST_DICT)
self.assertEqual(prop['a']['b']['c'], TEST_VAL)
def test_empty_getitem(self):
prop = self.dict_class()
prop.a.b.c
self.assertEqual(prop, {})
def test_getattr(self):
prop = self.dict_class(TEST_DICT)
self.assertEqual(prop.a.b.c, TEST_VAL)
def test_isinstance(self):
self.assertTrue(isinstance(self.dict_class(), dict))
def test_str(self):
prop = self.dict_class(TEST_DICT)
self.assertEqual(str(prop), str(TEST_DICT))
def test_json(self):
some_dict = TEST_DICT
some_json = json.dumps(some_dict)
prop = self.dict_class()
prop.a.b.c = TEST_VAL
prop_json = json.dumps(prop)
self.assertEqual(some_json, prop_json)
def test_delitem(self):
prop = self.dict_class({'a': 2})
del prop['a']
self.assertDictEqual(prop, {})
def test_delitem_nested(self):
prop = self.dict_class(TEST_DICT)
del prop['a']['b']['c']
self.assertDictEqual(prop, {'a': {'b': {}}})
def test_delattr(self):
prop = self.dict_class({'a': 2})
del prop.a
self.assertDictEqual(prop, {})
def test_delattr_nested(self):
prop = self.dict_class(TEST_DICT)
del prop.a.b.c
self.assertDictEqual(prop, {'a': {'b': {}}})
def test_delitem_delattr(self):
prop = self.dict_class(TEST_DICT)
del prop.a['b']
self.assertDictEqual(prop, {'a': {}})
def test_tuple_key(self):
prop = self.dict_class()
prop[(1, 2)] = 2
self.assertDictEqual(prop, {(1, 2): 2})
self.assertEqual(prop[(1, 2)], 2)
def test_set_prop_invalid(self):
prop = self.dict_class()
def set_keys():
prop.keys = 2
def set_items():
prop.items = 3
self.assertRaises(AttributeError, set_keys)
self.assertRaises(AttributeError, set_items)
self.assertDictEqual(prop, {})
def test_dir(self):
key = 'a'
prop = self.dict_class({key: 1})
dir_prop = dir(prop)
dir_dict = dir(self.dict_class)
for d in dir_dict:
self.assertTrue(d in dir_prop, d)
self.assertTrue('__methods__' not in dir_prop)
self.assertTrue('__members__' not in dir_prop)
def test_dir_with_members(self):
prop = self.dict_class({'__members__': 1})
dir(prop)
self.assertTrue('__members__' in prop.keys())
def test_to_dict(self):
nested = {'a': [{'a': 0}, 2], 'b': {}, 'c': 2}
prop = self.dict_class(nested)
regular = prop.to_dict()
self.assertDictEqual(regular, prop)
self.assertDictEqual(regular, nested)
self.assertNotIsInstance(regular, self.dict_class)
def get_attr():
regular.a = 2
self.assertRaises(AttributeError, get_attr)
def get_attr_deep():
regular['a'][0].a = 1
self.assertRaises(AttributeError, get_attr_deep)
def test_to_dict_with_tuple(self):
nested = {'a': ({'a': 0}, {2: 0})}
prop = self.dict_class(nested)
regular = prop.to_dict()
self.assertDictEqual(regular, prop)
self.assertDictEqual(regular, nested)
self.assertIsInstance(regular['a'], tuple)
self.assertNotIsInstance(regular['a'][0], self.dict_class)
def test_update(self):
old = self.dict_class()
old.child.a = 'a'
old.child.b = 'b'
old.foo = 'c'
new = self.dict_class()
new.child.b = 'b2'
new.child.c = 'c'
new.foo.bar = True
old.update(new)
reference = {'foo': {'bar': True},
'child': {'a': 'a', 'c': 'c', 'b': 'b2'}}
self.assertDictEqual(old, reference)
def test_update_with_lists(self):
org = self.dict_class()
org.a = [1, 2, {'a': 'superman'}]
someother = self.dict_class()
someother.b = [{'b': 123}]
org.update(someother)
correct = {'a': [1, 2, {'a': 'superman'}],
'b': [{'b': 123}]}
org.update(someother)
self.assertDictEqual(org, correct)
self.assertIsInstance(org.b[0], dict)
def test_update_with_kws(self):
org = self.dict_class(one=1, two=2)
someother = self.dict_class(one=3)
someother.update(one=1, two=2)
self.assertDictEqual(org, someother)
def test_update_with_args_and_kwargs(self):
expected = {'a': 1, 'b': 2}
org = self.dict_class()
org.update({'a': 3, 'b': 2}, a=1)
self.assertDictEqual(org, expected)
def test_update_with_multiple_args(self):
def update():
org.update({'a': 2}, {'a': 1})
org = self.dict_class()
self.assertRaises(TypeError, update)
def test_ior_operator(self):
old = self.dict_class()
old.child.a = 'a'
old.child.b = 'b'
old.foo = 'c'
new = self.dict_class()
new.child.b = 'b2'
new.child.c = 'c'
new.foo.bar = True
old |= new
reference = {'foo': {'bar': True},
'child': {'a': 'a', 'c': 'c', 'b': 'b2'}}
self.assertDictEqual(old, reference)
def test_ior_operator_with_lists(self):
org = self.dict_class()
org.a = [1, 2, {'a': 'superman'}]
someother = self.dict_class()
someother.b = [{'b': 123}]
org |= someother
correct = {'a': [1, 2, {'a': 'superman'}],
'b': [{'b': 123}]}
org |= someother
self.assertDictEqual(org, correct)
self.assertIsInstance(org.b[0], dict)
def test_ior_operator_with_dict(self):
org = self.dict_class(one=1, two=2)
someother = self.dict_class(one=3)
someother |= dict(one=1, two=2)
self.assertDictEqual(org, someother)
def test_or_operator(self):
old = self.dict_class()
old.child.a = 'a'
old.child.b = 'b'
old.foo = 'c'
new = self.dict_class()
new.child.b = 'b2'
new.child.c = 'c'
new.foo.bar = True
old = old | new
reference = {'foo': {'bar': True},
'child': {'a': 'a', 'c': 'c', 'b': 'b2'}}
self.assertDictEqual(old, reference)
def test_or_operator_with_lists(self):
org = self.dict_class()
org.a = [1, 2, {'a': 'superman'}]
someother = self.dict_class()
someother.b = [{'b': 123}]
org = org | someother
correct = {'a': [1, 2, {'a': 'superman'}],
'b': [{'b': 123}]}
org = org | someother
self.assertDictEqual(org, correct)
self.assertIsInstance(org.b[0], dict)
def test_ror_operator(self):
org = dict()
org['a'] = [1, 2, {'a': 'superman'}]
someother = self.dict_class()
someother.b = [{'b': 123}]
org = org | someother
correct = {'a': [1, 2, {'a': 'superman'}],
'b': [{'b': 123}]}
org = org | someother
self.assertDictEqual(org, correct)
self.assertIsInstance(org, Dict)
self.assertIsInstance(org.b[0], dict)
def test_or_operator_type_error(self):
old = self.dict_class()
with self.assertRaises(TypeError):
old | 'test'
def test_ror_operator_type_error(self):
old = self.dict_class()
with self.assertRaises(TypeError):
'test' | old
def test_hook_in_constructor(self):
a_dict = self.dict_class(TEST_DICT)
self.assertIsInstance(a_dict['a'], self.dict_class)
def test_copy(self):
class MyMutableObject(object):
def __init__(self):
self.attribute = None
foo = MyMutableObject()
foo.attribute = True
a = self.dict_class()
a.child.immutable = 42
a.child.mutable = foo
b = a.copy()
# immutable object should not change
b.child.immutable = 21
self.assertEqual(a.child.immutable, 21)
# mutable object should change
b.child.mutable.attribute = False
self.assertEqual(a.child.mutable.attribute, b.child.mutable.attribute)
# changing child of b should not affect a
b.child = "new stuff"
self.assertTrue(isinstance(a.child, self.dict_class))
def test_deepcopy(self):
class MyMutableObject(object):
def __init__(self):
self.attribute = None
foo = MyMutableObject()
foo.attribute = True
a = self.dict_class()
a.child.immutable = 42
a.child.mutable = foo
b = copy.deepcopy(a)
# immutable object should not change
b.child.immutable = 21
self.assertEqual(a.child.immutable, 42)
# mutable object should not change
b.child.mutable.attribute = False
self.assertTrue(a.child.mutable.attribute)
# changing child of b should not affect a
b.child = "new stuff"
self.assertTrue(isinstance(a.child, self.dict_class))
def test_deepcopy2(self):
class MyMutableObject(object):
def __init__(self):
self.attribute = None
foo = MyMutableObject()
foo.attribute = True
a = self.dict_class()
a.child.immutable = 42
a.child.mutable = foo
b = a.deepcopy()
# immutable object should not change
b.child.immutable = 21
self.assertEqual(a.child.immutable, 42)
# mutable object should not change
b.child.mutable.attribute = False
self.assertTrue(a.child.mutable.attribute)
# changing child of b should not affect a
b.child = "new stuff"
self.assertTrue(isinstance(a.child, self.dict_class))
def test_pickle(self):
a = self.dict_class(TEST_DICT)
self.assertEqual(a, pickle.loads(pickle.dumps(a)))
def test_add_on_empty_dict(self):
d = self.dict_class()
d.x.y += 1
self.assertEqual(d.x.y, 1)
def test_add_on_non_empty_dict(self):
d = self.dict_class()
d.x.y = 'defined'
with self.assertRaises(TypeError):
d.x += 1
def test_add_on_non_empty_value(self):
d = self.dict_class()
d.x.y = 1
d.x.y += 1
self.assertEqual(d.x.y, 2)
def test_add_on_unsupported_type(self):
d = self.dict_class()
d.x.y = 'str'
with self.assertRaises(TypeError):
d.x.y += 1
def test_init_from_zip(self):
keys = ['a']
values = [42]
items = zip(keys, values)
d = self.dict_class(items)
self.assertEqual(d.a, 42)
def test_setdefault_simple(self):
d = self.dict_class()
d.setdefault('a', 2)
self.assertEqual(d.a, 2)
d.setdefault('a', 3)
self.assertEqual(d.a, 2)
d.setdefault('c', []).append(2)
self.assertEqual(d.c, [2])
def test_setdefault_nested(self):
d = self.dict_class()
d.one.setdefault('two', [])
self.assertEqual(d.one.two, [])
d.one.setdefault('three', []).append(3)
self.assertEqual(d.one.three, [3])
def test_parent_key_item(self):
a = self.dict_class()
try:
a['keys']['x'] = 1
except AttributeError as e:
self.fail(e)
try:
a[1].x = 3
except Exception as e:
self.fail(e)
self.assertEqual(a, {'keys': {'x': 1}, 1: {'x': 3}})
def test_parent_key_prop(self):
a = self.dict_class()
try:
a.y.x = 1
except AttributeError as e:
self.fail(e)
self.assertEqual(a, {'y': {'x': 1}})
def test_top_freeze_against_top_key(self):
"Test that d.freeze() produces KeyError on d.missing."
d = self.dict_class()
self.assertEqual(d.missing, {})
d.freeze()
with self.assertRaises(KeyError):
d.missing
d.unfreeze()
self.assertEqual(d.missing, {})
def test_top_freeze_against_nested_key(self):
"Test that d.freeze() produces KeyError on d.inner.missing."
d = self.dict_class()
d.inner.present = TEST_VAL
self.assertIn("inner", d)
self.assertEqual(d.inner.missing, {})
d.freeze()
with self.assertRaises(KeyError):
d.inner.missing
with self.assertRaises(KeyError):
d.missing
d.unfreeze()
self.assertEqual(d.inner.missing, {})
self.assertEqual(d.missing, {})
def test_nested_freeze_against_top_level(self):
"Test that d.inner.freeze() leaves top-level `d` unfrozen."
d = self.dict_class()
d.inner.present = TEST_VAL
self.assertEqual(d.inner.present, TEST_VAL)
self.assertEqual(d.inner.missing, {})
self.assertEqual(d.missing, {})
d.inner.freeze()
with self.assertRaises(KeyError):
d.inner.missing # d.inner is frozen
self.assertEqual(d.missing, {}) # but not `d` itself
d.inner.unfreeze()
self.assertEqual(d.inner.missing, {})
def test_top_freeze_disallows_new_key_addition(self):
"Test that d.freeze() disallows adding new keys in d."
d = self.dict_class({"oldKey": None})
d.freeze()
d.oldKey = TEST_VAL # Can set pre-existing key.
self.assertEqual(d.oldKey, TEST_VAL)
with self.assertRaises(KeyError):
d.newKey = TEST_VAL # But can't add a new key.
self.assertNotIn("newKey", d)
d.unfreeze()
d.newKey = TEST_VAL
self.assertEqual(d.newKey, TEST_VAL)
class DictTests(unittest.TestCase, AbstractTestsClass):
dict_class = Dict
class ChildDictTests(unittest.TestCase, AbstractTestsClass):
dict_class = CHILD_CLASS
"""
Allow for these test cases to be run from the command line
via `python test_addict.py`
"""
if __name__ == '__main__':
test_classes = (DictTests, ChildDictTests)
loader = unittest.TestLoader()
runner = unittest.TextTestRunner(verbosity=2)
for class_ in test_classes:
loaded_tests = loader.loadTestsFromTestCase(class_)
runner.run(loaded_tests)