File: test_struct.py

package info (click to toggle)
python-omegaconf 2.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,244 kB
  • sloc: python: 26,413; makefile: 38; sh: 11
file content (71 lines) | stat: -rw-r--r-- 1,960 bytes parent folder | download | duplicates (3)
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
import re
from typing import Any, Dict

from pytest import mark, raises

from omegaconf import OmegaConf
from omegaconf.errors import ConfigKeyError


def test_struct_default() -> None:
    c = OmegaConf.create()
    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 raises(AttributeError, match=re.escape("a.foo")):
        # noinspection PyStatementEffect
        c.a.foo.bar


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

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


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


@mark.parametrize("in_base, in_merged", [({}, {"a": 10})])
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 raises(ConfigKeyError):
        OmegaConf.merge(base, merged)


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


@mark.parametrize("cfg", [{}, OmegaConf.create({}, flags={"struct": True})])
def test_struct_dict_get(cfg: Any) -> None:
    assert cfg.get("z") is None


def test_struct_dict_assign() -> None:
    cfg = OmegaConf.create({"a": {}})
    OmegaConf.set_struct(cfg, True)
    cfg.a = {"b": 10}
    assert cfg.a == {"b": 10}