generated from pwwang/pyparam
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_diot.py
520 lines (387 loc) · 11.6 KB
/
test_diot.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
import pytest
from copy import deepcopy
from argparse import Namespace
from collections import OrderedDict
from diot import Diot, CamelDiot, SnakeDiot, OrderedDiot, DiotFrozenError
from diot.diot import FrozenDiot, nest
@pytest.mark.parametrize(
"value, types, dest_type, expected, expectedtype",
[
({"a": 1}, [], dict, {"a": 1}, 'dict'),
({"a": 1}, [list], dict, {"a": 1}, 'dict'),
([{"a": 1}], [list], dict, [{"a": 1}], 'list'),
({"a": 1}, [dict], OrderedDict, {"a": 1}, 'OrderedDict'),
],
)
def test_nest(value, types, dest_type, expected, expectedtype):
out = nest(value, types, dest_type, True)
assert out == expected
assert type(out).__name__ == expectedtype
def test_safe():
diot = Diot(a__b=1)
assert diot.a__b == 1
assert diot["a__b"] == 1
diot.a_b = 2
assert diot.a_b == 2
diot["a_@_c"] = 3
assert diot.a__c == 3
diot2 = eval(repr(diot))
assert diot2.a_b == 2
assert diot2.a__c == 3
assert diot2["a_@_c"] == 3
with pytest.raises(KeyError):
Diot({"a__b": 1, "a_@_b": 2})
with pytest.raises(KeyError):
diot["a_@b"] = 1
assert diot.__diot__["keymaps"] == {
"a__b": "a__b",
"a_b": "a_b",
"a__c": "a_@_c",
}
diot = Diot(
a={"b": {"c": 1}, "d": ({"e": 2}, {"f": 3}), "g": lambda: True},
diot_nest=True,
diot_transform="safe",
)
assert diot.a.b.c == 1
assert diot.a.d[0].e == 2
assert diot.a.d[1].f == 3
assert diot.a.g()
diot = Diot(
a=Diot(
{
"b": {"c": 1},
"d": ({"e": 2}, {"f": 3}),
"g": lambda: True,
"h": Diot(a=1, b=2),
}
),
diot_nest=Diot,
)
assert diot.a.h.a == 1
assert diot.a.h.b == 2
assert diot.a.g()
with pytest.raises(AttributeError):
diot.a.b.c.x
with pytest.raises(AttributeError):
diot.a.d[0].e.x
with pytest.raises(AttributeError):
diot.a.d[1].f.x
diot = Diot({"": 1})
assert diot[""] == 1
assert diot.pop("") == 1
assert diot == {}
diot.update({"": 1})
assert diot[""] == 1
diot = Diot({"__": 1})
assert diot.__ == 1
def test_camel():
diot = CamelDiot(a_b=1)
assert diot.aB == 1
def test_snake():
diot = SnakeDiot(oneTwo=1)
assert diot.one_two == 1
def test_ordered():
diot = OrderedDiot([("c", 1), ("b", 2), ("a", 3)])
assert list(diot.keys()) == ["c", "b", "a"]
diot.insert(0, "x", 9)
assert list(diot.items()) == [("x", 9), ("c", 1), ("b", 2), ("a", 3)]
diot._ = 8
assert list(diot.keys()) == ["x", "c", "b", "a", "_"]
assert list(diot.values()) == [9, 1, 2, 3, 8]
dt = OrderedDiot(OrderedDict([("b", 1), ("a", 2), ("c", 3)]))
assert list(dt.keys()) == ["b", "a", "c"]
x = dt.pop("b")
assert x == 1
assert list(dt.keys()) == ["a", "c"]
x = dt.pop("x", 10)
assert x == 10
assert list(dt.keys()) == ["a", "c"]
def test_upper_lower():
dt = Diot(a=1, diot_transform="upper")
assert dt.A == dt["a"] == dt["A"] == 1
dt = Diot(A=1, diot_transform="lower")
assert dt.a == dt["a"] == dt["A"] == 1
def test_nest_diot():
dt = Diot(a={"b": {"c": [{"d": 1}]}})
assert isinstance(dt.a, Diot)
assert isinstance(dt.a.b, Diot)
assert isinstance(dt.a.b.c[0], Diot)
assert dt.a.b.c[0].d == 1
def test_unicode_key():
dt = Diot({"a键值b": 1})
assert dt.a_b == 1
def test_bytes_key():
dt = Diot({b"a_@_b": 1})
assert dt.a__b == 1
def test_to_dict():
dt = Diot(a={"b": {"c": [{"d": 1}], "e": ({"f": 2},)}})
assert isinstance(dt.a, Diot)
assert isinstance(dt.a.b, Diot)
assert isinstance(dt.a.b.c[0], Diot)
assert isinstance(dt.a.b.e[0], Diot)
assert dt.a.b.c[0].d == 1
assert dt.a.b.e[0].f == 2
d = dt.dict()
assert not isinstance(d["a"], Diot)
assert not isinstance(d["a"]["b"], Diot)
assert not isinstance(d["a"]["b"]["c"][0], Diot)
assert not isinstance(d["a"]["b"]["e"][0], Diot)
assert d == {"a": {"b": {"c": [{"d": 1}], "e": ({"f": 2},)}}}
def test_deepcopy():
dt = Diot(a={"b": {"c": [{"d": 1}], "e": ({"f": 2},)}})
dt2 = deepcopy(dt)
assert dt == dt2
assert dt is not dt2
assert dt.a is not dt2.a
assert dt.a.b is not dt2.a.b
assert dt.a.b.c is not dt2.a.b.c
assert dt.a.b.c[0] is not dt2.a.b.c[0]
assert dt.a.b.e is not dt2.a.b.e
assert dt.a.b.e[0] is not dt2.a.b.e[0]
def test_trydeepcopy():
def tryDeepCopy(obj, _recurvise=True):
"""
Try do deepcopy an object. If fails, just do a shallow copy.
@params:
obj (any): The object
_recurvise (bool): A flag to avoid deep recursion
@returns:
The copied object
"""
if _recurvise and isinstance(obj, dict):
# do a shallow copy first
# we don't start with an empty dictionary, because obj may be
# an object from a class extended from dict
ret = obj.copy()
for key, value in obj.items():
ret[key] = tryDeepCopy(value, False)
return ret
if _recurvise and isinstance(obj, list):
ret = obj[:]
for i, value in enumerate(obj):
ret[i] = tryDeepCopy(value, False)
return ret
try:
return deepcopy(obj)
except TypeError:
return obj
dt = Diot(a=Diot(b=Diot(c=1)))
dt3 = tryDeepCopy(dt)
assert dt3 == dt
def test_ordereddiot_insert():
od = OrderedDiot()
od.insert(0, "c", "d")
assert od == {"c": "d"}
od.insert(0, ("a", "b"))
assert od == {"a": "b", "c": "d"}
assert list(od.keys()) == ["a", "c"]
od.insert(None, "x", "y")
assert list(od.keys()) == ["a", "c", "x"]
assert od.x == "y"
del od.x
od.insert_before("a", "e", "f")
assert list(od.keys()) == ["e", "a", "c"]
assert od.e == "f"
od.insert_after("a", ("g", "h"))
assert list(od.keys()) == ["e", "a", "g", "c"]
assert od.g == "h"
od2 = OrderedDiot()
od2.a1 = "b1"
od2.c1 = "d1"
od.insert(-1, od2)
assert list(od.keys()) == ["e", "a", "g", "a1", "c1", "c"]
assert od.a1 == "b1"
assert od.c1 == "d1"
od3 = OrderedDiot()
od3.a2 = "b2"
od3.c2 = "d2"
od.insert_before("c", od3)
assert list(od.keys()) == ["e", "a", "g", "a1", "c1", "a2", "c2", "c"]
assert od.a2 == "b2"
assert od.c2 == "d2"
od4 = OrderedDiot()
od4.a3 = "b3"
od4.c3 = "d3"
od.insert_after("a2", od4)
assert list(od.keys()) == [
"e",
"a",
"g",
"a1",
"c1",
"a2",
"a3",
"c3",
"c2",
"c",
]
assert od.a3 == "b3"
assert od.c3 == "d3"
with pytest.raises(KeyError):
od.insert_before("Nosuchkey", "x", "y")
with pytest.raises(KeyError):
od.insert_after("Nosuchkey", "x", "y")
with pytest.raises(KeyError):
od.insert_before("c", "c", "y") # key exists
with pytest.raises(KeyError):
od.insert_after("c", "c", "y") # key exists
with pytest.raises(ValueError):
od.insert(0, od4, 1)
with pytest.raises(ValueError):
od.insert(0, ("m", 2), 1)
with pytest.raises(ValueError):
od.insert(0, ("m", 1, 2))
with pytest.raises(ValueError):
od.insert_before("a", od4, 1)
with pytest.raises(ValueError):
od.insert_before("a", ("m", 2), 1)
with pytest.raises(ValueError):
od.insert_after("a", od4, 1)
with pytest.raises(ValueError):
od.insert_after("a", ("m", 2), 1)
def test_od_iter():
od = OrderedDiot([("b", 1), ("a", 2)])
assert list(od) == ["b", "a"]
it = iter(od)
assert next(it) == "b"
assert next(it) == "a"
od.__diot__["orderedkeys"] = ["a", "b"]
assert list(od) == ["a", "b"]
it = iter(od)
assert next(it) == "a"
assert next(it) == "b"
def test_or_ior():
a = Diot({"data": 2, "count": 5})
b = Diot(data=2, count=5) # noqa: F841
c = a | {"data": 3}
assert c == {"data": 3, "count": 5}
c = a | [("data", 3)]
assert c == {"data": 3, "count": 5}
a |= {"data": 3}
assert a == {"data": 3, "count": 5}
with pytest.raises(TypeError):
a | 1
od = OrderedDiot([("b", 1), ("a", 2)])
od |= {"a": 1, "b": 2}
assert od.__diot__["orderedkeys"] == ["b", "a"]
assert od.a == 1
assert od.b == 2
def tform(key):
return key * 2
def test_pickle():
from pickle import loads, dumps
a = Diot(a=1, diot_transform=tform)
assert a.a == 1
assert a.aa == 1
pickled = dumps(a)
b = loads(pickled) # noqa: F841
assert a.a == 1
assert a.aa == 1
def test_from_namespace():
ns = Namespace(a=1, b=2)
d = Diot.from_namespace(ns)
assert len(d) == 2
assert d.a == 1
assert d.b == 2
# recursive
ns.c = Namespace()
d2 = Diot.from_namespace(ns, recursive=True)
assert len(d2) == 3
assert isinstance(d2.c, Diot)
d3 = Diot.from_namespace(ns, recursive=False)
assert isinstance(d3.c, Namespace)
def test_keywords():
d = Diot(a=1, get=2)
assert d.a == 1
assert d["get"] == 2
assert callable(d.get)
def test_od_copy():
od = OrderedDiot()
od.i = 0
od2 = od.copy()
assert od2.i == 0
od2.j = 1
od3 = od.copy()
assert "j" not in od3
def test_frozen_modify():
d = Diot(a=1, b=2, diot_frozen=True)
with pytest.raises(DiotFrozenError):
d.a = 2
with pytest.raises(DiotFrozenError):
d["a"] = 2
with pytest.raises(DiotFrozenError):
d.pop("a")
with pytest.raises(DiotFrozenError):
d.popitem()
with pytest.raises(DiotFrozenError):
d.update({})
with pytest.raises(DiotFrozenError):
d.update_recursively({})
with pytest.raises(DiotFrozenError):
d |= {}
with pytest.raises(DiotFrozenError):
del d["item"]
with pytest.raises(DiotFrozenError):
d.setdefault("c", 3)
with pytest.raises(DiotFrozenError):
d.clear()
d2 = d.copy()
with pytest.raises(DiotFrozenError):
d2.clear()
d2.unfreeze()
d2.c = 3
assert d2.c == 3
d2.freeze()
with pytest.raises(DiotFrozenError):
d2.d = 4
def test_cameldiot_repr():
d = CamelDiot(a_b=1)
assert d.aB == 1
assert repr(d) == "CamelDiot({'a_b': 1})"
def test_snakediot_repr():
d = SnakeDiot(aB=1)
assert d.a_b == 1
assert repr(d) == "SnakeDiot({'aB': 1})"
def test_ordereddiot_repr():
d = OrderedDiot(a_b=1)
assert d.a_b == 1
assert repr(d) == "OrderedDiot([('a_b', 1)])"
def test_unfreeze_recursive():
d = Diot({"a": {"b": 1}})
d.freeze(True)
d.unfreeze()
d.c = 2
assert d.a == {"b": 1}
assert d.c == 2
with pytest.raises(DiotFrozenError):
d.a.x = 1
d.unfreeze(True)
d.a.x = 1
def test_frozen_diot():
d = FrozenDiot(a=1, b=2)
with pytest.raises(DiotFrozenError):
d.c = 3
assert repr(d) == "FrozenDiot({'a': 1, 'b': 2})"
with d.thaw():
d.c = 3
assert d.c == 3
with pytest.raises(DiotFrozenError):
d.c = 4
def test_missing_handler():
d = Diot(a=1, b=2, diot_missing=None)
assert d.c is None
d = Diot(a=1, b=2, diot_missing=RuntimeError)
with pytest.raises(RuntimeError):
d["c"]
d = Diot(a=1, b=2, diot_missing=RuntimeError("abc"))
with pytest.raises(RuntimeError, match="abc"):
d["c"]
d = Diot(a=1, b=2, diot_missing=lambda key, diot: diot.a + diot.b)
assert d.c == 3
assert "c" not in d
def test_diot_construct_ignores_none():
d = Diot(None)
assert d == {}
d = Diot(None, a=1)
assert d == {"a": 1}