File: test_oc_select.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 (124 lines) | stat: -rw-r--r-- 2,704 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
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
from typing import Any

from pytest import mark

from omegaconf import OmegaConf


def test_oc_select_abs() -> None:
    cfg = OmegaConf.create(
        {
            "a0": "${k}",
            "a1": "${oc.select:k}",
            "a2": "${oc.select:k, zzz}",
            "k": 10,
        },
    )
    assert cfg.a0 == cfg.a1 == cfg.a2 == 10


def test_oc_select_missing() -> None:
    cfg = OmegaConf.create(
        {
            "a": "${oc.select:missing}",
            "b": "${oc.select:missing, default value}",
            "missing": "???",
        },
    )
    assert cfg.a is None
    assert cfg.b == "default value"


def test_oc_select_none() -> None:
    cfg = OmegaConf.create(
        {
            "a": "${oc.select:none}",
            "b": "${oc.select:none, default value}",
            "none": None,
        },
    )
    assert cfg.a is None
    assert cfg.b is None


def test_oc_select_relative() -> None:
    cfg = OmegaConf.create(
        {
            "a0": "${.k}",
            "a1": "${oc.select:.k}",
            "a2": "${oc.select:.k, zzz}",
            "k": 10,
        },
    )
    assert cfg.a0 == cfg.a1 == cfg.a2 == 10


def test_oc_nested_select_abs() -> None:
    cfg = OmegaConf.create(
        {
            "nested": {
                "a0": "${k}",
                "a1": "${oc.select:k}",
                "a2": "${oc.select:k,zzz}",
            },
            "k": 10,
        },
    )

    n = cfg.nested
    assert n.a0 == n.a1 == n.a2 == 10


def test_oc_nested_select_relative_same_level() -> None:
    cfg = OmegaConf.create(
        {
            "nested": {
                "a0": "${.k}",
                "a1": "${oc.select:.k}",
                "a2": "${oc.select:.k, zzz}",
                "k": 20,
            },
        },
    )

    n = cfg.nested
    assert n.a0 == n.a1 == n.a2 == 20


def test_oc_nested_select_relative_level_up() -> None:
    cfg = OmegaConf.create(
        {
            "nested": {
                "a0": "${..k}",
                "a1": "${oc.select:..k}",
                "a2": "${oc.select:..k, zzz}",
                "k": 20,
            },
            "k": 10,
        },
    )

    n = cfg.nested
    assert n.a0 == n.a1 == n.a2 == 10


@mark.parametrize(
    ("key", "expected"),
    [
        ("a0", 10),
        ("a1", 11),
        ("a2", None),
        ("a3", 20),
    ],
)
def test_oc_select_using_default(key: str, expected: Any) -> None:
    cfg = OmegaConf.create(
        {
            "a0": "${oc.select:zz, 10}",
            "a1": "${oc.select:.zz, 11}",
            "a2": "${oc.select:zz, null}",
            "a3": "${oc.select:zz, ${value}}",
            "value": 20,
        },
    )
    assert cfg[key] == expected