-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_dictable.py
More file actions
214 lines (148 loc) · 4.2 KB
/
Copy pathtest_dictable.py
File metadata and controls
214 lines (148 loc) · 4.2 KB
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
import json
import numpy as np
import pytest
from pathlib import Path
from autoconf.dictable import to_dict, from_dict, register_parser
@pytest.fixture(name="array_dict")
def make_array_dict():
return {"array": [1.0], "dtype": "float64", "type": "ndarray"}
@pytest.fixture(name="array")
def make_array():
return np.array([1.0])
class ArrayImpl:
def __init__(self, array):
self.array = array
@property
def dtype(self):
return self.array.dtype
def tolist(self):
return self.array.tolist()
def __array__(self):
return self.array
def test_array_impl(array):
assert to_dict(ArrayImpl(array)) == to_dict(array)
def test_array_as_dict(array_dict, array):
assert to_dict(array) == array_dict
def test_from_dict(array_dict, array):
assert from_dict(array_dict) == array
@pytest.mark.parametrize(
"array",
[
np.array([True]),
np.array([[1.0]]),
np.array([[1.0, 2.0], [3.0, 4.0]]),
np.array([[1, 2], [3, 4]]),
],
)
def test_multiple(array):
assert (from_dict(to_dict(array)) == array).all()
def test_as_json(array):
assert from_dict(json.loads(json.dumps(to_dict(array)))) == array
def test_with_type_attribute():
float_dict = {"class_path": "float", "type": "type"}
assert to_dict(float) == float_dict
assert from_dict(float_dict) is float
def test_register_parser():
register_parser("test", lambda x: x["value"])
assert from_dict({"type": "test", "value": 1}) == 1
def test_no_type():
assert from_dict({"hi": "there"}) == {"hi": "there"}
def test_serialise_path():
path = Path("/path/to/file.json")
path_dict = to_dict(path)
assert from_dict(path_dict) == path
class Parent:
def __init__(self, parent_arg):
self.parent_arg = parent_arg
class Child(Parent):
def __init__(self, child_arg, **kwargs):
super().__init__(**kwargs)
self.child_arg = child_arg
def test_serialise_kwargs():
child = Child(
child_arg="child",
parent_arg="parent",
)
child_dict = to_dict(child)
assert child_dict == {
"arguments": {
"child_arg": "child",
"parent_arg": "parent",
},
"class_path": "test_autoconf.test_dictable.Child",
"type": "instance",
}
new_child = from_dict(child_dict)
assert new_child.child_arg == "child"
assert new_child.parent_arg == "parent"
class WithOptional:
def __init__(self, arg: int = 1):
self.arg = arg
def test_serialise_with_arg():
assert to_dict(WithOptional()) == {
"arguments": {"arg": 1},
"class_path": "test_autoconf.test_dictable.WithOptional",
"type": "instance",
}
def test_serialise_without_arg():
assert to_dict(WithOptional(), filter_args=("arg",)) == {
"arguments": {},
"class_path": "test_autoconf.test_dictable.WithOptional",
"type": "instance",
}
class C:
pass
def test_tuple():
assert from_dict(
json.loads(
json.dumps(
to_dict(
(C, C),
)
)
)
) == (C, C)
def function():
return 1
def test_function():
assert (
from_dict(
json.loads(
json.dumps(
to_dict(
function,
)
)
)
)()
== 1
)
def test_slice():
s = slice(1, 2, 3)
assert s == from_dict(to_dict(s))
def test_int_64():
i = np.int64(1)
result = from_dict(to_dict(i))
assert result == 1
assert isinstance(result, np.int64)
def test_int64_slice():
s = slice(np.int64(1), np.int64(2), np.int64(3))
assert s == from_dict(
json.loads(
json.dumps(
to_dict(s),
)
)
)
def test_compound_key():
d = {(1, 2): 1}
string = json.dumps(to_dict(d))
assert d == from_dict(json.loads(string))
class Prior:
def __eq__(self, other):
return isinstance(other, Prior)
def __hash__(self):
return hash(Prior)
def test_prior_key():
string = json.dumps(to_dict({Prior(): 1}))
assert from_dict(json.loads(string)) == {Prior(): 1}