File size: 6,836 Bytes
e40d9d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from typing import Any, Callable, Dict, List, Union

import pytest
from pytest import raises

from omegaconf import DictConfig, ListConfig, OmegaConf, ReadonlyConfigError


@pytest.mark.parametrize(  # type: ignore
    "src, func, expectation",
    [
        pytest.param(
            {},
            lambda c: c.__setitem__("a", 1),
            raises(ReadonlyConfigError, match="a"),
            id="dict_setitem",
        ),
        pytest.param(
            {"a": {"b": {"c": 1}}},
            lambda c: c.__getattr__("a").__getattr__("b").__setitem__("c", 1),
            raises(ReadonlyConfigError, match="a.b.c"),
            id="dict_nested_setitem",
        ),
        pytest.param(
            {},
            lambda c: OmegaConf.update(c, "a.b", 10, merge=True),
            raises(ReadonlyConfigError, match="a"),
            id="dict_update",
        ),
        pytest.param(
            {"a": 10},
            lambda c: c.__setattr__("a", 1),
            raises(ReadonlyConfigError, match="a"),
            id="dict_setattr",
        ),
        pytest.param(
            {"a": 10},
            lambda c: c.pop("a"),
            raises(ReadonlyConfigError, match="a"),
            id="dict_pop",
        ),
        pytest.param(
            {"a": 10},
            lambda c: c.__delitem__("a"),
            raises(ReadonlyConfigError, match="a"),
            id="dict_delitem",
        ),
        # list
        pytest.param(
            [],
            lambda c: c.__setitem__(0, 1),
            raises(ReadonlyConfigError, match="0"),
            id="list_setitem",
        ),
        pytest.param(
            [],
            lambda c: OmegaConf.update(c, "0.b", 10, merge=True),
            raises(ReadonlyConfigError, match="[0]"),
            id="list_update",
        ),
        pytest.param(
            [10], lambda c: c.pop(), raises(ReadonlyConfigError), id="list_pop"
        ),
        pytest.param(
            [0],
            lambda c: c.__delitem__(0),
            raises(ReadonlyConfigError, match="[0]"),
            id="list_delitem",
        ),
    ],
)
def test_readonly(
    src: Union[Dict[str, Any], List[Any]], func: Callable[[Any], Any], expectation: Any
) -> None:
    c = OmegaConf.create(src)
    OmegaConf.set_readonly(c, True)
    with expectation:
        func(c)
    assert c == src


@pytest.mark.parametrize("src", [{}, []])  # type: ignore
def test_readonly_flag(src: Union[Dict[str, Any], List[Any]]) -> None:
    c = OmegaConf.create(src)
    assert not OmegaConf.is_readonly(c)
    OmegaConf.set_readonly(c, True)
    assert OmegaConf.is_readonly(c)
    OmegaConf.set_readonly(c, False)
    assert not OmegaConf.is_readonly(c)
    OmegaConf.set_readonly(c, None)
    assert not OmegaConf.is_readonly(c)


def test_readonly_nested_list() -> None:
    c = OmegaConf.create([[1]])
    assert isinstance(c, ListConfig)
    assert not OmegaConf.is_readonly(c)
    assert not OmegaConf.is_readonly(c[0])
    OmegaConf.set_readonly(c, True)
    assert OmegaConf.is_readonly(c)
    assert OmegaConf.is_readonly(c[0])
    OmegaConf.set_readonly(c, False)
    assert not OmegaConf.is_readonly(c)
    assert not OmegaConf.is_readonly(c[0])
    OmegaConf.set_readonly(c, None)
    assert not OmegaConf.is_readonly(c)
    assert not OmegaConf.is_readonly(c[0])
    OmegaConf.set_readonly(c[0], True)
    assert not OmegaConf.is_readonly(c)
    assert OmegaConf.is_readonly(c[0])


def test_readonly_list_insert() -> None:
    c = OmegaConf.create([])
    OmegaConf.set_readonly(c, True)
    with raises(ReadonlyConfigError, match="[0]"):
        c.insert(0, 10)
    assert c == []


def test_readonly_list_insert_deep() -> None:
    src: List[Dict[str, Any]] = [dict(a=[dict(b=[])])]
    c = OmegaConf.create(src)
    assert isinstance(c, ListConfig)
    OmegaConf.set_readonly(c, True)
    with raises(ReadonlyConfigError, match=re.escape("[0].a[0].b[0]")):
        c[0].a[0].b.insert(0, 10)
    assert c == src


def test_readonly_list_append() -> None:
    c = OmegaConf.create([])
    OmegaConf.set_readonly(c, True)
    with raises(ReadonlyConfigError, match="[0]"):
        c.append(10)
    assert c == []


def test_readonly_list_change_item() -> None:
    c = OmegaConf.create([1, 2, 3])
    assert isinstance(c, ListConfig)
    OmegaConf.set_readonly(c, True)
    with raises(ReadonlyConfigError, match="[1]"):
        c[1] = 10
    assert c == [1, 2, 3]


def test_readonly_list_pop() -> None:
    c = OmegaConf.create([1, 2, 3])
    assert isinstance(c, ListConfig)
    OmegaConf.set_readonly(c, True)
    with raises(ReadonlyConfigError, match="[1]"):
        c.pop(1)
    assert c == [1, 2, 3]


def test_readonly_list_del() -> None:
    c = OmegaConf.create([1, 2, 3])
    assert isinstance(c, ListConfig)
    OmegaConf.set_readonly(c, True)
    with raises(ReadonlyConfigError, match="[1]"):
        del c[1]
    assert c == [1, 2, 3]


def test_readonly_list_sort() -> None:
    c = OmegaConf.create([3, 1, 2])
    assert isinstance(c, ListConfig)
    OmegaConf.set_readonly(c, True)
    with raises(ReadonlyConfigError):
        c.sort()
    assert c == [3, 1, 2]


def test_readonly_from_cli() -> None:
    c = OmegaConf.create({"foo": {"bar": [1]}})
    assert isinstance(c, DictConfig)
    OmegaConf.set_readonly(c, True)
    cli = OmegaConf.from_dotlist(["foo.bar=[2]"])
    cfg2 = OmegaConf.merge(c, cli)
    assert OmegaConf.is_readonly(c)
    assert OmegaConf.is_readonly(cfg2)


@pytest.mark.parametrize(  # type: ignore
    "cfg1, cfg2",
    [
        pytest.param({"foo": {"bar": 10}}, {"foo": {"bar": 20}}, id="override_value"),
        pytest.param({"foo": {"bar": 10}}, {"foo": {"yup": 20}}, id="adding_key"),
        pytest.param({"a": 1}, {"b": 2}, id="adding_key"),
        pytest.param({"a": 1}, OmegaConf.create({"b": 2}), id="adding_key"),
    ],
)
def test_merge_with_readonly(cfg1: Dict[str, Any], cfg2: Dict[str, Any]) -> None:
    c = OmegaConf.create(cfg1)
    OmegaConf.set_readonly(c, True)
    with raises(ReadonlyConfigError):
        c.merge_with(cfg2)


@pytest.mark.parametrize(  # type: ignore
    "readonly_key, cfg1, cfg2, expected",
    [
        pytest.param(
            "",
            {"foo": {"bar": 10}},
            {"foo": {}},
            {"foo": {"bar": 10}},
            id="merge_empty_dict",
        ),
        pytest.param(
            "foo",
            {"foo": {"bar": 10}},
            {"xyz": 10},
            {"foo": {"bar": 10}, "xyz": 10},
            id="merge_different_node",
        ),
    ],
)
def test_merge_with_readonly_nop(
    readonly_key: str,
    cfg1: Dict[str, Any],
    cfg2: Dict[str, Any],
    expected: Dict[str, Any],
) -> None:
    c = OmegaConf.create(cfg1)
    OmegaConf.set_readonly(OmegaConf.select(c, readonly_key), True)
    c.merge_with(cfg2)
    assert c == OmegaConf.create(expected)