-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
199 lines (148 loc) · 4.6 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
import pickle
from io import BytesIO
import pytest
from dictdot import dictdot, _is_valid_dot_key, _build_path
@pytest.fixture
def d():
return dictdot()
@pytest.fixture
def data():
return dictdot(
{
"foo": 1,
"bar": {
"fee": 2,
},
"baz": [
{
"foo": 1,
"bar": 2,
},
],
}
)
# `find` functionality.
def test_find_all_keys(data):
assert list(data.find(build_paths=False)) == [
("foo",),
("bar",),
("bar", "fee"),
("baz",),
("baz", 0),
("baz", 0, "foo"),
("baz", 0, "bar"),
]
def test_find_keys_and_values(data):
# Find every key equal to "foo".
assert list(data.find(key="foo")) == [".foo", ".baz[0].foo"]
# Find every value equal to 2.
assert list(data.find(value=2)) == [".bar.fee", ".baz[0].bar"]
# Both key and value must evaluate to True.
assert list(data.find(key="bar", value=2)) == [".baz[0].bar"]
assert list(data.find(key="bar", value=1)) == []
def test_find_with_max_depth(data):
assert list(data.find(max_depth=0)) == []
# Find every key equal to "foo" with depth at most 2.
assert (
[".foo"]
== list(data.find(key="foo", max_depth=1))
== list(data.find(key="foo", max_depth=2))
)
# Main class.
def test_assignment_and_access(d):
# Assign and get values either by key or attribute.
d["foo"] = d.bar = 3.14
assert d.foo == d["bar"]
def test_non_existing_key(d):
# Non-existing key returns None.
assert d.foo is None
with pytest.raises(KeyError):
d["foo"]
def test_deletion(d):
# Delete items by attribute name.
d.bar = 42
del d.bar
assert "bar" not in d.keys()
assert d.bar is None
with pytest.raises(KeyError):
d["bar"]
def test_builtin_attributes(d):
# Builtin methods are not overridden.
d.items = "foo"
assert d["items"] != d.items
assert hasattr(d.items, "__call__")
d._convert = "bar"
assert d["_convert"] != d._convert
assert hasattr(d._convert, "__call__")
def test_nested_dicts(data):
# Nested dicts are also dictdot.
assert type(data.bar) is dictdot
assert data.foo == data.baz[0].foo
def test_nested_dicts_after_init(d):
# Even when added after initialization. Non-dict values are not modified.
d.foo = [{"bar": 2}, 2]
assert d.foo[0].bar == d.foo[1]
def test_keys_with_special_characters(d):
# Keys with special characters can still be accessed with "_".
d["test-key"] = "hyphen"
assert d.test_key == "hyphen"
def test_order_of_keys(d):
# Order matters.
d["test.key"] = "dot"
d["test_key"] = "underscore"
assert d.test_key == "underscore"
assert d["test.key"] == "dot"
del d.test_key
assert d.test_key == "dot"
def test_recursive_dicts(data):
# Recursive dicts still work.
data.baz.append(data)
assert data is data.baz[-1] is data.baz[-1].baz[-1]
def test_copy(data):
# copy() returns a dictdot.
data2 = data.copy()
assert type(data2) is dictdot
assert data2 == data
assert data2 is not data
def test_convert_to_dict(data):
# Nested dicts are also converted.
data2 = data.as_dict()
assert data2 == data
assert type(data2) is dict
assert type(data2["bar"]) is dict
assert type(data2["baz"]) is list
assert type(data2["baz"][0]) is dict
def test_pickling(data):
f = BytesIO()
pickle.dump(data, f)
f.seek(0)
data2 = pickle.load(f)
assert type(data2) is dictdot
assert data2 == data
assert data2 is not data
f2 = BytesIO()
pickle.dump(data2, f2)
f2.seek(0)
data3 = pickle.load(f2)
assert type(data3) is dictdot
assert data3 == data
assert data3 is not data
def test_dict_init():
# Can be initialized the same way as dict.
a = ["foo", "bar", "baz"]
b = range(3)
d = {"foo": 0, "bar": 1, "baz": 2}
assert d == dict(zip(a, b)) == dictdot(zip(a, b)) == dictdot(foo=0, bar=1, baz=2)
# Helper functions.
def test_is_valid_dot_key():
assert _is_valid_dot_key("_key")
assert not _is_valid_dot_key(None)
assert not _is_valid_dot_key(".")
assert not _is_valid_dot_key("0key")
assert not _is_valid_dot_key("$key$")
assert not _is_valid_dot_key("as_dict")
assert not _is_valid_dot_key("copy")
def test_build_path():
assert _build_path(["foo", "bar"]) == ".foo.bar"
assert _build_path(["_convert", 1, "bar"]) == '["_convert"][1].bar'
assert _build_path(['"quoted"', "'single'"]) == '["\\"quoted\\""]["\'single\'"]'