1
|
|
|
from datetime import date, datetime, timezone |
|
|
|
|
2
|
|
|
|
3
|
|
|
import pytest |
|
|
|
|
4
|
|
|
|
5
|
|
|
from pocketutils.core.dot_dict import * |
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestDotDict: |
|
|
|
|
9
|
|
|
def test_keys(self): |
|
|
|
|
10
|
|
|
t = NestedDotDict({"a": "0", "b": 1, "c": {"c1": 8, "c2": ["abc", "xyz"]}}) |
|
|
|
|
11
|
|
|
assert list(t.keys()) == ["a", "b", "c"] |
12
|
|
|
|
13
|
|
|
def test_bad(self): |
|
|
|
|
14
|
|
|
with pytest.raises(ValueError): |
15
|
|
|
NestedDotDict({"a.b": "c"}) |
16
|
|
|
with pytest.raises(ValueError): |
17
|
|
|
NestedDotDict({"a": {"b.c": "d"}}) |
18
|
|
|
with pytest.raises(ValueError): |
19
|
|
|
# noinspection PyTypeChecker |
20
|
|
|
NestedDotDict({1: 2}) |
21
|
|
|
with pytest.raises(ValueError): |
22
|
|
|
NestedDotDict({"a.b": {5: 2}}) |
23
|
|
|
|
24
|
|
|
def test_iter(self): |
|
|
|
|
25
|
|
|
t = NestedDotDict(dict(a=dict(b=555))) |
|
|
|
|
26
|
|
|
assert len(t) == 1 |
27
|
|
|
assert list(iter(t)) == ["a"] |
28
|
|
|
assert t.items() == [("a", {"b": 555})] |
29
|
|
|
|
30
|
|
|
def test_get(self): |
|
|
|
|
31
|
|
|
t = NestedDotDict(dict(a=dict(b=dict(c="hello")))) |
|
|
|
|
32
|
|
|
assert t.get("a") == dict(b=dict(c="hello")) |
33
|
|
|
assert t.get("a.b") == dict(c="hello") |
34
|
|
|
assert t.get("a.b.c") == "hello" |
35
|
|
|
assert t["a"] == dict(b=dict(c="hello")) |
36
|
|
|
assert t["a.b"] == dict(c="hello") |
37
|
|
|
assert t["a.b.c"] == "hello" |
38
|
|
|
assert t.get("b") is None |
39
|
|
|
assert t.get("a.x") is None |
40
|
|
|
assert t.get("a.b.x") is None |
41
|
|
|
assert t.get("a.b.c.x") is None |
42
|
|
|
assert t.get("b", default="Q") == "Q" |
43
|
|
|
with pytest.raises(LookupError): |
44
|
|
|
# noinspection PyStatementEffect |
45
|
|
|
t["b"] |
|
|
|
|
46
|
|
|
with pytest.raises(LookupError): |
47
|
|
|
# noinspection PyStatementEffect |
48
|
|
|
t["a.x"] |
|
|
|
|
49
|
|
|
with pytest.raises(LookupError): |
50
|
|
|
# noinspection PyStatementEffect |
51
|
|
|
t["a.b.c.x"] |
|
|
|
|
52
|
|
|
t = NestedDotDict({"a": {"1": "hello", "2": "ell", "3": "elle"}}) |
|
|
|
|
53
|
|
|
assert t["a.1"] == "hello" |
54
|
|
|
assert t["a.2"] == "ell" |
55
|
|
|
assert t["a.3"] == "elle" |
56
|
|
|
|
57
|
|
|
def test_get_as(self): |
|
|
|
|
58
|
|
|
t = NestedDotDict( |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
"animal": "kitten", |
61
|
|
|
"size": 4.3, |
62
|
|
|
"number": 8, |
63
|
|
|
"birthday": "2020-01-17", |
64
|
|
|
"birthdayzulu": "2020-01-17Z", |
65
|
|
|
"birthdatetime": "2020-01-17T15:22:11", |
66
|
|
|
"birthdatetimezulu": "2020-01-17T15:22:11Z", |
67
|
|
|
} |
68
|
|
|
) |
69
|
|
|
assert t.get_as("animal", str) == "kitten" |
70
|
|
|
assert t.get_as("size", float) == 4.3 |
71
|
|
|
assert t.get_as("size", str) == "4.3" |
72
|
|
|
assert t.get_as("number", int) == 8 |
73
|
|
|
assert t.get_as("birthday", date) == date(2020, 1, 17) |
74
|
|
|
assert t.get_as("birthdatetime", datetime) == datetime(2020, 1, 17, 15, 22, 11) |
75
|
|
|
assert t.get_as("birthdatetimezulu", datetime) == datetime( |
76
|
|
|
2020, 1, 17, 15, 22, 11, tzinfo=timezone.utc |
77
|
|
|
) |
78
|
|
|
# parsing a date string as datetime is dangerous because it fills in 00:00:00 |
79
|
|
|
with pytest.raises(ValueError): |
80
|
|
|
assert t.get_as("birthdayzulu", datetime) |
81
|
|
|
with pytest.raises(ValueError): |
82
|
|
|
assert t.get_as("birthday", datetime) |
83
|
|
|
with pytest.raises(ValueError): |
84
|
|
|
assert t.get_as("birthdayzulu", date) |
85
|
|
|
with pytest.raises(ValueError): |
86
|
|
|
t.get_as("birthdatetime", date) |
87
|
|
|
with pytest.raises(ValueError): |
88
|
|
|
t.get_as("animal", int) |
89
|
|
|
|
90
|
|
|
def test_get_list_as(self): |
|
|
|
|
91
|
|
|
t = NestedDotDict({"kittens": ["dory", "johnson", "robin", "jack"], "ages": [3, 5, 7, 11]}) |
|
|
|
|
92
|
|
|
assert t.get_list_as("kittens", str) == ["dory", "johnson", "robin", "jack"] |
93
|
|
|
assert t.get_list_as("ages", int) == [3, 5, 7, 11] |
94
|
|
|
assert t.get_list_as("ages", str) == ["3", "5", "7", "11"] |
95
|
|
|
assert t.get_list_as("hobbies", str) is None |
96
|
|
|
with pytest.raises(ValueError): |
97
|
|
|
t.get_list_as("kittens", float) |
98
|
|
|
|
99
|
|
|
def test_req(self): |
|
|
|
|
100
|
|
|
t = NestedDotDict( |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
"kittens": ["dory", "johnson", "robin", "jack"], |
103
|
|
|
"ages": [3, 5, 7, 11], |
104
|
|
|
"carrots": "yes", |
105
|
|
|
} |
106
|
|
|
) |
107
|
|
|
assert t.req_as("carrots", str) == "yes" |
108
|
|
|
assert t.req_list_as("ages", int) == [3, 5, 7, 11] |
109
|
|
|
with pytest.raises(LookupError): |
110
|
|
|
t.req_list_as("hobbies", str) |
111
|
|
|
|
112
|
|
|
def test_nested(self): |
|
|
|
|
113
|
|
|
t = NestedDotDict( |
|
|
|
|
114
|
|
|
{"kittens": {"names": ["dory", "johnson", "robin", "jack"], "ages": [3, 5, 7, 11]}} |
115
|
|
|
) |
116
|
|
|
assert isinstance(t.get("kittens"), NestedDotDict) |
117
|
|
|
assert t.get_list_as("kittens.ages", int) == [3, 5, 7, 11] |
118
|
|
|
|
119
|
|
|
def test_leaves(self): |
|
|
|
|
120
|
|
|
t = NestedDotDict(dict(a=dict(b=1), b=2, c=dict(a=dict(a=3)))) |
|
|
|
|
121
|
|
|
assert t.leaves() == {"a.b": 1, "b": 2, "c.a.a": 3} |
122
|
|
|
|
123
|
|
|
def test_string(self): |
|
|
|
|
124
|
|
|
t = NestedDotDict(dict(a=dict(b=1), b=2, c=dict(a=dict(a=3)))) |
|
|
|
|
125
|
|
|
assert str(t) == str(t._x) |
|
|
|
|
126
|
|
|
lines = t.pretty_str().splitlines() |
127
|
|
|
assert len(lines) == 5 |
128
|
|
|
assert lines[0] == "{" |
129
|
|
|
assert lines[-1] == "}" |
130
|
|
|
assert lines[1] == ' "a.b": 1,' |
131
|
|
|
|
132
|
|
|
def test_size(self): |
|
|
|
|
133
|
|
|
t = NestedDotDict(dict(a=dict(b=1), b=2, c=dict(a=dict(a=3)))) |
|
|
|
|
134
|
|
|
assert t.n_elements_total() == 3 |
135
|
|
|
t = NestedDotDict(dict(a=dict(b=1), b=[1, 2, 3], c=dict(a=dict(a=3)))) |
|
|
|
|
136
|
|
|
assert t.n_elements_total() == 5 |
137
|
|
|
|
138
|
|
|
def test_bytes(self): |
|
|
|
|
139
|
|
|
t = NestedDotDict(dict(a=dict(b=1), b=2, c=dict(a=dict(a=3)))) |
|
|
|
|
140
|
|
|
assert t.n_bytes_total() == 84 |
141
|
|
|
t = NestedDotDict(dict(a=dict(b=1), b=[1, 2, 3], c=dict(a=dict(a=3)))) |
|
|
|
|
142
|
|
|
assert t.n_bytes_total() == 140 |
143
|
|
|
|
144
|
|
|
def test_as_exactly(self): |
|
|
|
|
145
|
|
|
t = NestedDotDict({"zoo": {"animals": "jackets"}, "what": 0.1}) |
|
|
|
|
146
|
|
|
assert t.exactly("zoo.animals", str) == "jackets" |
147
|
|
|
assert t.exactly("what", float) == 0.1 |
148
|
|
|
with pytest.raises(TypeError): |
149
|
|
|
t.exactly("what", str) |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
if __name__ == "__main__": |
153
|
|
|
pytest.main() |
154
|
|
|
|