File size: 4,593 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 |
import re
from textwrap import dedent
from typing import Any
import pytest
from omegaconf import DictConfig, EnumNode, ListConfig, OmegaConf, _utils
from . import Enum1, User
@pytest.mark.parametrize( # type: ignore
"input_, expected",
[
(["item1", "item2", {"key3": "value3"}], "- item1\n- item2\n- key3: value3\n"),
({"hello": "world", "list": [1, 2]}, "hello: world\nlist:\n- 1\n- 2\n"),
],
)
def test_to_yaml(input_: Any, expected: str) -> None:
c = OmegaConf.create(input_)
assert expected == OmegaConf.to_yaml(c)
assert OmegaConf.create(OmegaConf.to_yaml(c)) == c
@pytest.mark.parametrize( # type: ignore
"input_, expected",
[
(["item一", "item二", dict(key三="value三")], "- item一\n- item二\n- key三: value三\n"),
(dict(你好="世界", list=[1, 2]), "你好: 世界\nlist:\n- 1\n- 2\n"),
],
)
def test_to_yaml_unicode(input_: Any, expected: str) -> None:
c = OmegaConf.create(input_)
assert expected == OmegaConf.to_yaml(c)
assert OmegaConf.create(OmegaConf.to_yaml(c)) == c
@pytest.mark.parametrize( # type: ignore
"input_, expected, type_",
[
(["1", 1], "- '1'\n- 1\n", int),
(["10e2", "1.0", 1.0], "- '10e2'\n- '1.0'\n- 1.0\n", float),
(_utils.YAML_BOOL_TYPES, None, bool),
],
)
def test_to_yaml_string_primitive_types_list(
input_: Any, expected: str, type_: type
) -> None:
if type_ == bool:
for t in input_:
c = OmegaConf.create([t, 1])
expected = "- '%s'\n- 1\n" % t
assert OmegaConf.to_yaml(c) == expected
else:
c = OmegaConf.create(input_)
assert OmegaConf.to_yaml(c) == expected
@pytest.mark.parametrize( # type: ignore
"input_, expected, type_",
[
({"b": "1", "a": 1}, "b: '1'\na: 1\n", int),
({"b": "10e2", "a": "1.0", "c": 1.0}, "b: '10e2'\na: '1.0'\nc: 1.0\n", float),
(_utils.YAML_BOOL_TYPES, None, bool),
],
)
def test_to_yaml_string_primitive_types_dict(
input_: Any, expected: str, type_: type
) -> None:
if type_ == bool:
for t in input_:
c = OmegaConf.create({"b": t, "a": 1})
assert OmegaConf.to_yaml(c) == "b: '%s'\na: 1\n" % t
else:
c = OmegaConf.create(input_)
assert OmegaConf.to_yaml(c) == expected
@pytest.mark.parametrize( # type: ignore
"input_, resolve, expected",
[
(dict(a1="${ref}", ref="bar"), True, "bar"),
(dict(a1="${ref}", ref="bar"), False, "changed"),
([100, "${0}"], True, 100),
([100, "${0}"], False, 1000),
],
)
def test_to_yaml_resolve(input_: Any, resolve: bool, expected: int) -> None:
c = OmegaConf.create(input_)
# without resolve, references are preserved
yaml_str = OmegaConf.to_yaml(c, resolve=resolve)
c2 = OmegaConf.create(yaml_str)
assert isinstance(c2, ListConfig) or isinstance(c2, DictConfig)
if isinstance(c2, DictConfig):
assert c2.a1 == "bar"
c2.ref = "changed"
assert c2.a1 == expected
else:
c2[0] = 1000
assert c2[1] == expected
def test_to_yaml_sort_keys() -> None:
c = OmegaConf.create({"b": 2, "a": 1})
# keys are not sorted by default
assert OmegaConf.to_yaml(c) == "b: 2\na: 1\n"
c = OmegaConf.create({"b": 2, "a": 1})
assert OmegaConf.to_yaml(c, sort_keys=True) == "a: 1\nb: 2\n"
def test_to_yaml_with_enum() -> None:
cfg = OmegaConf.create()
assert isinstance(cfg, DictConfig)
cfg.foo = EnumNode(Enum1)
cfg.foo = Enum1.FOO
expected = """foo: FOO
"""
s = OmegaConf.to_yaml(cfg)
assert s == expected
assert (
OmegaConf.merge({"foo": EnumNode(Enum1, value="???")}, OmegaConf.create(s))
== cfg
)
def test_pretty_deprecated() -> None:
c = OmegaConf.create({"foo": "bar"})
with pytest.warns(
expected_warning=UserWarning,
match=re.escape(
dedent(
"""\
cfg.pretty() is deprecated and will be removed in a future version.
Use OmegaConf.to_yaml(cfg)
""",
)
),
):
assert c.pretty() == "foo: bar\n"
@pytest.mark.parametrize( # type: ignore
"user",
[
User(name="Bond", age=7),
OmegaConf.structured(User(name="Bond", age=7)),
{"name": "Bond", "age": 7},
],
)
def test_structured_configs(user: User) -> None:
expected = dedent(
"""\
name: Bond
age: 7
"""
)
assert OmegaConf.to_yaml(user) == expected
|