-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathtest_basic_ops_list.py
393 lines (304 loc) · 9.17 KB
/
test_basic_ops_list.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import re
import pytest
from omegaconf import OmegaConf, UntypedNode, ListConfig, DictConfig
from . import IllegalType, does_not_raise
def test_list_value():
c = OmegaConf.create("a: [1,2]")
assert {"a": [1, 2]} == c
def test_list_of_dicts():
v = [dict(key1="value1"), dict(key2="value2")]
c = OmegaConf.create(v)
assert c[0].key1 == "value1"
assert c[1].key2 == "value2"
def test_pretty_list():
c = OmegaConf.create(["item1", "item2", dict(key3="value3")])
expected = """- item1
- item2
- key3: value3
"""
assert expected == c.pretty()
assert OmegaConf.create(c.pretty()) == c
def test_list_get_with_default():
c = OmegaConf.create([None, "???", "found"])
assert c.get(0, "default_value") == "default_value"
assert c.get(1, "default_value") == "default_value"
assert c.get(2, "default_value") == "found"
def test_iterate_list():
c = OmegaConf.create([1, 2])
items = [x for x in c]
assert items[0] == 1
assert items[1] == 2
def test_items_with_interpolation():
c = OmegaConf.create(["foo", "${0}"])
assert c == ["foo", "foo"]
def test_list_pop():
c = OmegaConf.create([1, 2, 3, 4])
assert c.pop(0) == 1
assert c.pop() == 4
assert c == [2, 3]
with pytest.raises(IndexError):
c.pop(100)
def test_in_list():
c = OmegaConf.create([10, 11, dict(a=12)])
assert 10 in c
assert 11 in c
assert dict(a=12) in c
assert "blah" not in c
def test_list_config_with_list():
c = OmegaConf.create([])
assert isinstance(c, ListConfig)
def test_list_config_with_tuple():
c = OmegaConf.create(())
assert isinstance(c, ListConfig)
def test_items_on_list():
c = OmegaConf.create([1, 2])
with pytest.raises(AttributeError):
c.items()
def test_list_enumerate():
src = ["a", "b", "c", "d"]
c = OmegaConf.create(src)
for i, v in enumerate(c):
assert src[i] == v
assert v is not None
src[i] = None
for v in src:
assert v is None
def test_list_delitem():
c = OmegaConf.create([1, 2, 3])
assert c == [1, 2, 3]
del c[0]
assert c == [2, 3]
with pytest.raises(IndexError):
del c[100]
def test_list_len():
c = OmegaConf.create([1, 2])
assert len(c) == 2
@pytest.mark.parametrize(
"parent, index, value, expected",
[
([10, 11], 0, ["a", "b"], [["a", "b"], 11]),
([None], 0, {"foo": "bar"}, [{"foo": "bar"}]),
({}, "foo", ["a", "b"], {"foo": ["a", "b"]}),
({}, "foo", ("a", "b"), {"foo": ["a", "b"]}),
],
)
def test_assign(parent, index, value, expected):
c = OmegaConf.create(parent)
c[index] = value
assert c == expected
def test_nested_list_assign_illegal_value():
with pytest.raises(ValueError, match=re.escape("key a[0]")):
c = OmegaConf.create(dict(a=[None]))
c.a[0] = IllegalType()
def test_list_append():
c = OmegaConf.create([])
c.append(1)
c.append(2)
c.append({})
c.append([])
assert isinstance(c[2], DictConfig)
assert isinstance(c[3], ListConfig)
assert c == [1, 2, {}, []]
def test_pretty_without_resolve():
c = OmegaConf.create([100, "${0}"])
# without resolve, references are preserved
c2 = OmegaConf.create(c.pretty(resolve=False))
c2[0] = 1000
assert c2[1] == 1000
def test_pretty_with_resolve():
c = OmegaConf.create([100, "${0}"])
# with resolve, references are not preserved.
c2 = OmegaConf.create(c.pretty(resolve=True))
c2[0] = 1000
assert c[1] == 100
def test_index_slice():
c = OmegaConf.create([10, 11, 12, 13])
assert c[1:3] == [11, 12]
def test_index_slice2():
c = OmegaConf.create([10, 11, 12, 13])
assert c[0:3:2] == [10, 12]
def test_negative_index():
c = OmegaConf.create([10, 11, 12, 13])
assert c[-1] == 13
def test_list_dir():
c = OmegaConf.create([1, 2, 3])
assert ["0", "1", "2"] == dir(c)
def test_getattr():
c = OmegaConf.create(["a", "b", "c"])
assert getattr(c, "0") == "a"
assert getattr(c, "1") == "b"
assert getattr(c, "2") == "c"
with pytest.raises(AttributeError):
getattr(c, "anything")
def test_insert():
c = OmegaConf.create(["a", "b", "c"])
c.insert(1, 100)
assert c == ["a", 100, "b", "c"]
@pytest.mark.parametrize(
"src, append, result",
[
([], [], []),
([1, 2], [3], [1, 2, 3]),
([1, 2], ("a", "b", "c"), [1, 2, "a", "b", "c"]),
],
)
def test_extend(src, append, result):
src = OmegaConf.create(src)
src.extend(append)
assert src == result
@pytest.mark.parametrize(
"src, remove, result, expectation",
[
([10], 10, [], does_not_raise()),
([], "oops", None, pytest.raises(ValueError)),
([0, dict(a="blah"), 10], dict(a="blah"), [0, 10], does_not_raise()),
([1, 2, 1, 2], 2, [1, 1, 2], does_not_raise()),
],
)
def test_remove(src, remove, result, expectation):
with expectation:
src = OmegaConf.create(src)
src.remove(remove)
assert src == result
@pytest.mark.parametrize("src", [[], [1, 2, 3], [None, dict(foo="bar")]])
@pytest.mark.parametrize("num_clears", [1, 2])
def test_clear(src, num_clears):
src = OmegaConf.create(src)
for i in range(num_clears):
src.clear()
assert src == []
@pytest.mark.parametrize(
"src, item, expected_index, expectation",
[
([], 20, -1, pytest.raises(ValueError)),
([10, 20], 10, 0, does_not_raise()),
([10, 20], 20, 1, does_not_raise()),
],
)
def test_index(src, item, expected_index, expectation):
with expectation:
src = OmegaConf.create(src)
assert src.index(item) == expected_index
@pytest.mark.parametrize(
"src, item, count",
[([], 10, 0), ([10], 10, 1), ([10, 2, 10], 10, 2), ([10, 2, 10], None, 0)],
)
def test_count(src, item, count):
src = OmegaConf.create(src)
assert src.count(item) == count
@pytest.mark.parametrize("src", [[], [1, 2], ["a", "b", "c"]])
def test_copy(src):
src = OmegaConf.create(src)
cp = src.copy()
assert id(src) != id(cp)
assert src == cp
def test_sort():
c = OmegaConf.create(["bbb", "aa", "c"])
c.sort()
assert ["aa", "bbb", "c"] == c
c.sort(reverse=True)
assert ["c", "bbb", "aa"] == c
c.sort(key=len)
assert ["c", "aa", "bbb"] == c
c.sort(key=len, reverse=True)
assert ["bbb", "aa", "c"] == c
@pytest.mark.parametrize(
"l1,l2",
[
# empty list
([], []),
# simple list
(["a", 12, "15"], ["a", 12, "15"]),
# raw vs any
([1, 2, 12], [1, 2, UntypedNode(12)]),
# nested empty dict
([12, dict()], [12, dict()]),
# nested dict
([12, dict(c=10)], [12, dict(c=10)]),
# nested list
([1, 2, 3, [10, 20, 30]], [1, 2, 3, [10, 20, 30]]),
# nested list with any
([1, 2, 3, [1, 2, UntypedNode(3)]], [1, 2, 3, [1, 2, UntypedNode(3)]],),
],
)
def test_list_eq(l1, l2):
c1 = OmegaConf.create(l1)
c2 = OmegaConf.create(l2)
def eq(a, b):
assert a == b
assert b == a
assert not a != b
assert not b != a
eq(c1, c2)
eq(c1, l1)
eq(c2, l2)
@pytest.mark.parametrize("l1,l2", [([10, "${0}"], [10, 10])])
def test_list_eq_with_interpolation(l1, l2):
c1 = OmegaConf.create(l1)
c2 = OmegaConf.create(l2)
def eq(a, b):
assert a == b
assert b == a
assert not a != b
assert not b != a
eq(c1, c2)
@pytest.mark.parametrize(
"input1, input2",
[
([], [10]),
([10], [11]),
([12], [UntypedNode(13)]),
([12, dict()], [13, dict()]),
([12, dict(c=10)], [13, dict(c=10)]),
([12, [1, 2, 3]], [12, [10, 2, 3]]),
([12, [1, 2, UntypedNode(3)]], [12, [1, 2, UntypedNode(30)]]),
],
)
def test_list_not_eq(input1, input2):
c1 = OmegaConf.create(input1)
c2 = OmegaConf.create(input2)
def neq(a, b):
assert a != b
assert b != a
assert not a == b
assert not b == a
neq(c1, c2)
def test_insert_throws_not_changing_list():
c = OmegaConf.create([])
with pytest.raises(ValueError):
c.insert(0, IllegalType())
assert len(c) == 0
assert c == []
def test_append_throws_not_changing_list():
c = OmegaConf.create([])
with pytest.raises(ValueError):
c.append(IllegalType())
assert len(c) == 0
assert c == []
def test_hash():
c1 = OmegaConf.create([10])
c2 = OmegaConf.create([10])
assert hash(c1) == hash(c2)
c2[0] = 20
assert hash(c1) != hash(c2)
@pytest.mark.parametrize(
"list1, list2, expected",
[
([], [], []),
([1, 2], [3, 4], [1, 2, 3, 4]),
(["x", 2, "${0}"], [5, 6, 7], ["x", 2, "x", 5, 6, 7]),
],
)
class TestListAdd:
def test_list_plus(self, list1, list2, expected):
list1 = OmegaConf.create(list1)
list2 = OmegaConf.create(list2)
expected = OmegaConf.create(expected)
ret = list1 + list2
assert ret == expected
def test_list_plus_eq(self, list1, list2, expected):
list1 = OmegaConf.create(list1)
list2 = OmegaConf.create(list2)
expected = OmegaConf.create(expected)
list1 += list2
assert list1 == expected