File size: 1,724 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
import re
from typing import Any, Dict

import pytest

from omegaconf import OmegaConf
from omegaconf.errors import ConfigKeyError


def test_struct_default() -> None:
    c = OmegaConf.create()
    assert c.not_found is None
    assert OmegaConf.is_struct(c) is None


def test_struct_set_on_dict() -> None:
    c = OmegaConf.create({"a": {}})
    OmegaConf.set_struct(c, True)
    # Throwing when it hits foo, so exception key is a.foo and not a.foo.bar
    with pytest.raises(AttributeError, match=re.escape("a.foo")):
        # noinspection PyStatementEffect
        c.a.foo.bar


def test_struct_set_on_nested_dict() -> None:
    c = OmegaConf.create(dict(a=dict(b=10)))
    OmegaConf.set_struct(c, True)
    with pytest.raises(AttributeError):
        # noinspection PyStatementEffect
        c.foo

    assert "a" in c
    assert c.a.b == 10
    with pytest.raises(AttributeError, match=re.escape("a.foo")):
        # noinspection PyStatementEffect
        c.a.foo


def test_merge_dotlist_into_struct() -> None:
    c = OmegaConf.create(dict(a=dict(b=10)))
    OmegaConf.set_struct(c, True)
    with pytest.raises(AttributeError, match=re.escape("foo")):
        c.merge_with_dotlist(["foo=1"])


@pytest.mark.parametrize("in_base, in_merged", [(dict(), dict(a=10))])  # type: ignore
def test_merge_config_with_struct(
    in_base: Dict[str, Any], in_merged: Dict[str, Any]
) -> None:
    base = OmegaConf.create(in_base)
    merged = OmegaConf.create(in_merged)
    OmegaConf.set_struct(base, True)
    with pytest.raises(ConfigKeyError):
        OmegaConf.merge(base, merged)


def test_struct_contain_missing() -> None:
    c = OmegaConf.create(dict())
    OmegaConf.set_struct(c, True)
    assert "foo" not in c