File size: 2,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
import re
from typing import Any, Optional

import pytest
from pytest import raises

from omegaconf import MissingMandatoryValue, OmegaConf

from . import does_not_raise


@pytest.mark.parametrize("struct", [True, False, None])  # type: ignore
def test_select_key_from_empty(struct: Optional[bool]) -> None:
    c = OmegaConf.create()
    OmegaConf.set_struct(c, struct)
    assert OmegaConf.select(c, "not_there") is None


@pytest.mark.parametrize(  # type: ignore
    "cfg, keys, expected, expectation",
    [
        ({}, "nope", None, does_not_raise()),
        ({}, "not.there", None, does_not_raise()),
        ({}, "still.not.there", None, does_not_raise()),
        ({"c": 1}, "c", 1, does_not_raise()),
        ({"a": {"v": 1}}, "a", {"v": 1}, does_not_raise()),
        ({"a": {"v": 1}}, "a.v", 1, does_not_raise()),
        ({"missing": "???"}, "missing", None, does_not_raise()),
        ([], "0", None, does_not_raise()),
        ([1, "2"], ("0", "1"), (1, "2"), does_not_raise()),
        (
            [1, {"a": 10, "c": ["foo", "bar"]}],
            ("0", "1.a", "1.b", "1.c.0", "1.c.1"),
            (1, 10, None, "foo", "bar"),
            does_not_raise(),
        ),
        ([1, 2, 3], "a", None, raises(TypeError)),
        (
            {"a": {"v": 1}, "b": {"v": 1}},
            "",
            {"a": {"v": 1}, "b": {"v": 1}},
            does_not_raise(),
        ),
        (
            {"dict": {"one": 1}, "foo": "one=${dict.one}"},
            "foo",
            "one=1",
            does_not_raise(),
        ),
        (
            {"dict": {"foo": "one=${one}"}, "one": 1},
            "dict.foo",
            "one=1",
            does_not_raise(),
        ),
        ({"dict": {"foo": "one=${foo:1}"}}, "dict.foo", "one=_1_", does_not_raise()),
    ],
)
def test_select(
    restore_resolvers: Any, cfg: Any, keys: Any, expected: Any, expectation: Any
) -> None:
    if not isinstance(keys, (tuple, list)):
        keys = [keys]
    if not isinstance(expected, (tuple, list)):
        expected = [expected]
    OmegaConf.register_resolver("foo", lambda x: f"_{x}_")

    c = OmegaConf.create(cfg)
    with expectation:
        for idx, key in enumerate(keys):
            assert OmegaConf.select(c, key) == expected[idx]


def test_select_from_dict() -> None:
    c = OmegaConf.create({"missing": "???"})
    with pytest.raises(MissingMandatoryValue):
        OmegaConf.select(c, "missing", throw_on_missing=True)
    assert OmegaConf.select(c, "missing", throw_on_missing=False) is None
    assert OmegaConf.select(c, "missing") is None


def test_select_deprecated() -> None:
    c = OmegaConf.create({"foo": "bar"})
    with pytest.warns(
        expected_warning=UserWarning,
        match=re.escape("select() is deprecated, use OmegaConf.select(). (Since 2.0)"),
    ):
        c.select("foo")