Add test for DictDefault
Browse files- tests/test_dict.py +91 -0
tests/test_dict.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import unittest
|
2 |
+
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
from axolotl.utils.dict import DictDefault
|
6 |
+
|
7 |
+
|
8 |
+
class DictDefaultTest(unittest.TestCase):
|
9 |
+
def test_dict_default(self):
|
10 |
+
cfg = DictDefault(
|
11 |
+
{
|
12 |
+
"key_a": {"key_b": "value_a"},
|
13 |
+
"key_c": "value_c",
|
14 |
+
"key_d": ["value_d", "value_e"],
|
15 |
+
}
|
16 |
+
)
|
17 |
+
|
18 |
+
assert (
|
19 |
+
cfg.key_a.key_b == "value_a"
|
20 |
+
), "DictDefault should return value for existing nested keys"
|
21 |
+
|
22 |
+
assert (
|
23 |
+
cfg.key_c == "value_c"
|
24 |
+
), "DictDefault should return value for existing keys"
|
25 |
+
|
26 |
+
assert (
|
27 |
+
cfg.key_d[0] == "value_d"
|
28 |
+
), "DictDefault should return value for existing keys in list"
|
29 |
+
|
30 |
+
assert (
|
31 |
+
"value_e" in cfg.key_d,
|
32 |
+
"DictDefault should support in operator for existing keys in list",
|
33 |
+
)
|
34 |
+
|
35 |
+
def test_dict_or_operator(self):
|
36 |
+
cfg = DictDefault(
|
37 |
+
{
|
38 |
+
"key_a": {"key_b": "value_a"},
|
39 |
+
"key_c": "value_c",
|
40 |
+
"key_d": ["value_d", "value_e"],
|
41 |
+
"key_f": "value_f",
|
42 |
+
}
|
43 |
+
)
|
44 |
+
|
45 |
+
cfg = cfg | DictDefault({"key_a": {"key_b": "value_b"}, "key_f": "value_g"})
|
46 |
+
|
47 |
+
assert (
|
48 |
+
cfg.key_a.key_b == "value_b"
|
49 |
+
), "DictDefault should support OR operator for existing nested keys"
|
50 |
+
|
51 |
+
assert cfg.key_c == "value_c", "DictDefault should not delete existing key"
|
52 |
+
|
53 |
+
assert cfg.key_d == [
|
54 |
+
"value_d",
|
55 |
+
"value_e",
|
56 |
+
], "DictDefault should not overwrite existing keys in list"
|
57 |
+
|
58 |
+
assert (
|
59 |
+
cfg.key_f == "value_g"
|
60 |
+
), "DictDefault should support OR operator for existing key"
|
61 |
+
|
62 |
+
def test_dict_missingkey(self):
|
63 |
+
cfg = DictDefault({})
|
64 |
+
|
65 |
+
assert cfg.random_key is None, "DictDefault should return None for missing keys"
|
66 |
+
|
67 |
+
def test_dict_nested_missingparentkey(self):
|
68 |
+
"""
|
69 |
+
Due to subclassing Dict, DictDefault will error if we try to access a nested key whose parent key does not exist.
|
70 |
+
"""
|
71 |
+
cfg = DictDefault({})
|
72 |
+
|
73 |
+
with pytest.raises(
|
74 |
+
AttributeError,
|
75 |
+
match=r"'NoneType' object has no attribute 'another_random_key'",
|
76 |
+
):
|
77 |
+
cfg.random_key.another_random_key
|
78 |
+
|
79 |
+
def test_dict_shorthand_assignment(self):
|
80 |
+
"""
|
81 |
+
Shorthand assignment is said to not be supported if subclassed. However, their example raises error instead of None.
|
82 |
+
This test ensures that it is supported for current implementation.
|
83 |
+
|
84 |
+
Ref: https://github.com/mewwts/addict#default-values
|
85 |
+
"""
|
86 |
+
|
87 |
+
cfg = DictDefault({"key_a": {"key_b": "value_a"}})
|
88 |
+
|
89 |
+
cfg.key_a.key_b = "value_b"
|
90 |
+
|
91 |
+
assert cfg.key_a.key_b == "value_b", "Shorthand assignment should be supported"
|