-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathtest_utils_collections.py
More file actions
265 lines (203 loc) · 8.73 KB
/
Copy pathtest_utils_collections.py
File metadata and controls
265 lines (203 loc) · 8.73 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
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
"""
Tests for custom collections classes.
"""
from __future__ import annotations
import copy
import pytest
from openff.toolkit.utils.collections import ValidatedDict, ValidatedList
class TestValidatedMixin:
def test_pickle(self):
"""Test pickle roundtripping"""
# TODO: implement this test
pass
class TestValidatedList(TestValidatedMixin):
"""Test suite for the ValidatedList class."""
def test_validators(self):
"""Validators of ValidatedList are called correctly."""
def is_positive(value):
if value <= 0:
raise TypeError("value is not positive")
# The constructor should check all elements.
with pytest.raises(TypeError, match="value is not positive"):
ValidatedList([1, -2], validator=is_positive)
vlist = ValidatedList([1, 2, 3], validator=is_positive)
# __setitem__()
with pytest.raises(TypeError, match="value is not positive"):
vlist[2] = -1
with pytest.raises(TypeError, match="value is not positive"):
vlist[0:2] = [2, 3, -1]
# append()
with pytest.raises(TypeError, match="value is not positive"):
vlist.append(-4)
# extend() and __iadd__()
with pytest.raises(TypeError, match="value is not positive"):
vlist.extend([6, -1])
with pytest.raises(TypeError, match="value is not positive"):
vlist += [6, -1]
# insert()
with pytest.raises(TypeError, match="value is not positive"):
vlist.insert(1, -3)
def test_converters(self):
"""Custom converters of ValidatedList are called correctly."""
# All elements are converted on construction.
vlist = ValidatedList([1, 2.0, "3"], converter=int)
assert vlist == [1, 2, 3]
# __setitem__()
vlist[2] = "4"
assert vlist[2] == 4
vlist[0:3] = ["2", "3", 4]
assert vlist == [2, 3, 4]
# append()
vlist.append("5")
assert vlist[3] == 5
# extend() and __iadd__()
vlist.extend([6, "7"])
assert vlist[5] == 7
vlist += ["8", 9]
assert vlist[6] == 8
# insert()
vlist.insert(5, "10")
assert vlist[5] == 10
def test_validators_and_converters(self):
"""Custom converters of ValidatedList are called correctly."""
def is_positive(value):
if value <= 0:
raise TypeError("value is not positive")
# Validators are run after converters.
vlist = ValidatedList([1, 2, -3], converter=abs, validator=is_positive)
assert vlist == [1, 2, 3]
# __setitem__
vlist[2] = -1
assert vlist[2] == 1
with pytest.raises(TypeError, match="value is not positive"):
vlist[2] = 0
vlist[0:3] = [2, 3, -1]
assert vlist == [2, 3, 1]
with pytest.raises(TypeError, match="value is not positive"):
vlist[0:3] = [2, 3, 0]
# append()
vlist.append(-4)
assert vlist[-1] == 4
with pytest.raises(TypeError, match="value is not positive"):
vlist.append(0)
# extend() and __iadd__()
vlist.extend([6, -1])
assert vlist[-2:] == [6, 1]
with pytest.raises(TypeError, match="value is not positive"):
vlist.extend([6, 0])
vlist += [6, -2]
assert vlist[-2:] == [6, 2]
with pytest.raises(TypeError, match="value is not positive"):
vlist += [6, 0]
# insert()
vlist.insert(1, -3)
assert vlist[1] == 3
with pytest.raises(TypeError, match="value is not positive"):
vlist.insert(1, 0)
def test_multiple_converters(self):
"""Multiple converters of ValidatedList are called in order."""
vlist = ValidatedList([1, 2, -3], converter=[abs, str])
assert vlist == ["1", "2", "3"]
def test_multiple_validators(self):
"""Multiple converters of ValidatedList are called in order."""
def is_positive(value):
if value <= 0:
raise TypeError("value must be positive")
def is_odd(value):
if value % 2 == 0:
raise TypeError("value must be odd")
with pytest.raises(TypeError, match="value must be positive"):
ValidatedList([-1, -3], validator=[is_positive, is_odd])
with pytest.raises(TypeError, match="value must be odd"):
ValidatedList([2, 4], validator=[is_positive, is_odd])
def test_copy(self):
"""A copy of a ValidatedList returns another ValidatedList."""
vlist = ValidatedList([1, 2, 3])
assert isinstance(vlist.copy(), ValidatedList)
assert isinstance(copy.copy(vlist), ValidatedList)
assert isinstance(copy.deepcopy(vlist), ValidatedList)
def test_slice(self):
"""A slice of a ValidatedList returns another ValidatedList."""
vlist = ValidatedList([1, 2, 3])
assert isinstance(vlist[:2], ValidatedList)
class TestValidatedDict(TestValidatedMixin):
"""Test suite for the ValidatedDict class."""
def test_validators(self):
"""Validators of ValidatedDict are called correctly."""
def is_positive(value):
if value <= 0:
raise TypeError("value is not positive")
# The constructor should check all elements.
with pytest.raises(TypeError, match="value is not positive"):
ValidatedDict({"a": 1, "b": -2}, validator=is_positive)
d = ValidatedDict({"x": 1, "y": 2, "z": 3}, validator=is_positive)
# __setitem__()
with pytest.raises(TypeError, match="value is not positive"):
d["me!"] = -1
# update()
with pytest.raises(TypeError, match="value is not positive"):
d.update((("moe", 2), ("larry", 3), ("curly", -4)))
with pytest.raises(TypeError, match="value is not positive"):
d.update({"moe": 2, "larry": 3, "curly": -4})
with pytest.raises(TypeError, match="exactly one positional"):
d.update({"moe": 2}, {"larry": 3}, {"curly": -4})
with pytest.raises(TypeError, match="not accept named"):
d.update({"moe": 2}, moe=2, larry=3, curly=-4)
def test_converters(self):
"""Custom converters of ValidatedDict are called correctly."""
# All elements are converted on construction.
d = ValidatedDict({"x": 1, "y": 2.0, "z": "3"}, converter=int)
assert d == {"x": 1, "y": 2, "z": 3}
# __setitem__()
d["y"] = "4"
assert d["y"] == 4
d["w"] = "-20"
assert d["w"] == -20
# update()
d.update({6: "7"})
assert d[6] == 7
d.update(((6, "8"), ("shemp", "-30")))
assert d[6] == 8
assert d["shemp"] == -30
def test_validators_and_converters(self):
"""Custom converters of ValidatedDict are called correctly."""
def is_positive(value):
if value <= 0:
raise TypeError("value is not positive")
# Validators are run after converters.
d = ValidatedDict({"a": 1, "b": 2, "c": -3}, converter=abs, validator=is_positive)
assert d == {"a": 1, "b": 2, "c": 3}
# __setitem__
d[2] = -1
assert d[2] == 1
with pytest.raises(TypeError, match="value is not positive"):
d[2] = 0
# update()
d.update({"x": 6, "y": -1})
assert d["y"] == 1
with pytest.raises(TypeError, match="value is not positive"):
d.update({6: 0})
d.update((("x", 6), ("y", -1)))
assert d["y"] == 1
def test_multiple_converters(self):
"""Multiple converters of ValidatedDict are called in order."""
d = ValidatedDict({"u": 1, "v": 2, "w": -3}, converter=[abs, str])
assert d == {"u": "1", "v": "2", "w": "3"}
def test_multiple_validators(self):
"""Multiple converters of ValidatedDict are called in order."""
def is_positive(value):
if value <= 0:
raise TypeError("value must be positive")
def is_odd(value):
if value % 2 == 0:
raise TypeError("value must be odd")
with pytest.raises(TypeError, match="value must be positive"):
ValidatedDict({"first": -1, "second": -3}, validator=[is_positive, is_odd])
with pytest.raises(TypeError, match="value must be odd"):
ValidatedDict({"first": 2, "second": 4}, validator=[is_positive, is_odd])
def test_copy(self):
"""A copy of a ValidatedDict returns another ValidatedDict."""
d = ValidatedDict({"a": 1, "b": 2, "c": 3})
assert isinstance(d.copy(), ValidatedDict)
assert isinstance(copy.copy(d), ValidatedDict)
assert isinstance(copy.deepcopy(d), ValidatedDict)