From fc4ebe56173af3398944e7e6f6a45a99385e19b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pere=20D=C3=ADaz?= Date: Thu, 23 Jul 2020 23:23:23 +0200 Subject: [PATCH] tests --- tests/test_basic_ops_dict.py | 65 ------------------ tests/test_basic_ops_list.py | 64 +----------------- tests/test_nodes.py | 23 ++----- tests/test_to_yaml.py | 123 +++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 147 deletions(-) create mode 100644 tests/test_to_yaml.py diff --git a/tests/test_basic_ops_dict.py b/tests/test_basic_ops_dict.py index 20bbe18df..ba3fd4a1b 100644 --- a/tests/test_basic_ops_dict.py +++ b/tests/test_basic_ops_dict.py @@ -119,52 +119,6 @@ def test_subscript_set_with_dot_warning_suppressed(recwarn: Any, mocker: Any) -> assert len(recwarn) == 0 -def test_to_yaml_dict() -> None: - c = OmegaConf.create(dict(hello="world", list=[1, 2])) - expected = """hello: world -list: -- 1 -- 2 -""" - assert expected == OmegaConf.to_yaml(c) - assert OmegaConf.create(OmegaConf.to_yaml(c)) == c - - -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_dict_unicode() -> None: - c = OmegaConf.create(dict(你好="世界", list=[1, 2])) - expected = """你好: 世界 -list: -- 1 -- 2 -""" - assert expected == OmegaConf.to_yaml(c) - assert OmegaConf.create(OmegaConf.to_yaml(c)) == c - - -def test_to_yaml_strings_float() -> None: - c = OmegaConf.create({"b": "10e2", "a": "1.0", "c": 1.0}) - assert OmegaConf.to_yaml(c) == "b: '10e2'\na: '1.0'\nc: 1.0\n" - - -def test_to_yaml_string_boolean() -> None: - for t in _utils.YAML_BOOL_TYPES: - c = OmegaConf.create({"b": t, "a": 1}) - assert OmegaConf.to_yaml(c) == "b: '%s'\na: 1\n" % t - - -def test_to_yaml_string_int() -> None: - c = OmegaConf.create({"b": "1", "a": 1}) - assert OmegaConf.to_yaml(c) == "b: '1'\na: 1\n" - - def test_default_value() -> None: c = OmegaConf.create() assert c.missing_key or "a default value" == "a default value" @@ -522,25 +476,6 @@ def test_assign_dict_in_dict() -> None: assert isinstance(c.foo, DictConfig) -def test_to_yaml_without_resolve() -> None: - c = OmegaConf.create(dict(a1="${ref}", ref="bar")) - # without resolve, references are preserved - c2 = OmegaConf.create(OmegaConf.to_yaml(c, resolve=False)) - assert isinstance(c2, DictConfig) - assert c2.a1 == "bar" - c2.ref = "changed" - assert c2.a1 == "changed" - - -def test_to_yaml_with_resolve() -> None: - c = OmegaConf.create(dict(a1="${ref}", ref="bar")) - c2 = OmegaConf.create(OmegaConf.to_yaml(c, resolve=True)) - assert isinstance(c2, DictConfig) - assert c2.a1 == "bar" - c2.ref = "changed" - assert c2.a1 == "bar" - - def test_instantiate_config_fails() -> None: with pytest.raises(TypeError): BaseContainer() # type: ignore diff --git a/tests/test_basic_ops_list.py b/tests/test_basic_ops_list.py index b54510eef..9e0f875f1 100644 --- a/tests/test_basic_ops_list.py +++ b/tests/test_basic_ops_list.py @@ -4,7 +4,7 @@ import pytest -from omegaconf import AnyNode, ListConfig, OmegaConf, _utils +from omegaconf import AnyNode, ListConfig, OmegaConf from omegaconf.errors import ( ConfigKeyError, ConfigTypeError, @@ -30,49 +30,6 @@ def test_list_of_dicts() -> None: assert c[1].key2 == "value2" -def test_to_yaml_list() -> None: - c = OmegaConf.create(["item1", "item2", dict(key3="value3")]) - expected = """- item1 -- item2 -- key3: value3 -""" - assert expected == OmegaConf.to_yaml(c) - assert OmegaConf.create(OmegaConf.to_yaml(c)) == c - - -def test_to_yaml_list_unicode() -> None: - c = OmegaConf.create(["item一", "item二", dict(key三="value三")]) - expected = """- item一 -- item二 -- key三: value三 -""" - assert expected == OmegaConf.to_yaml(c) - assert OmegaConf.create(OmegaConf.to_yaml(c)) == c - - -def test_to_yaml_strings_float() -> None: - c = OmegaConf.create(["10e2", "1.0", 1.0]) - expected = """- '10e2' -- '1.0' -- 1.0 -""" - assert OmegaConf.to_yaml(c) == expected - - -def test_to_yaml_string_boolean() -> None: - for t in _utils.YAML_BOOL_TYPES: - print(t) - c = OmegaConf.create([t, 1]) - expected = "- '%s'\n- 1\n" % t - assert OmegaConf.to_yaml(c) == expected - - -def test_to_yaml_string_int() -> None: - c = OmegaConf.create(["1", 1]) - expected = "- '1'\n- 1\n" - assert OmegaConf.to_yaml(c) == expected - - def test_list_get_with_default() -> None: c = OmegaConf.create([None, "???", "found"]) assert c.get(0, "default_value") == "default_value" @@ -230,25 +187,6 @@ def test_list_append() -> None: validate_list_keys(c) -def test_to_yaml_without_resolve() -> None: - c = OmegaConf.create([100, "${0}"]) - # without resolve, references are preserved - yaml_str = OmegaConf.to_yaml(c, resolve=False) - c2 = OmegaConf.create(yaml_str) - assert isinstance(c2, ListConfig) - c2[0] = 1000 - assert c2[1] == 1000 - - -def test_to_yaml_with_resolve() -> None: - c = OmegaConf.create([100, "${0}"]) - # with resolve, references are not preserved. - c2 = OmegaConf.create(OmegaConf.to_yaml(c, resolve=True)) - assert isinstance(c2, ListConfig) - c2[0] = 1000 - assert c[1] == 100 - - @pytest.mark.parametrize( # type: ignore "index, expected", [(slice(1, 3), [11, 12]), (slice(0, 3, 2), [10, 12]), (-1, 13)] ) diff --git a/tests/test_nodes.py b/tests/test_nodes.py index a8479770c..70e52720b 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -354,31 +354,16 @@ def test_legal_assignment_enum( node_type(enum_type) -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( """ - pretty() is deprecated, use OmegaConf.to_yaml() and resolve - now defaults to True (Since 2.0.1) - """ + pretty() is deprecated and will be removed in a future version. + Use OmegaConf.to_yaml. Please note that the default value for + resolve has changed to True. + """, ), ): assert c.pretty() == "foo: bar\n" diff --git a/tests/test_to_yaml.py b/tests/test_to_yaml.py new file mode 100644 index 000000000..a8d1f85f9 --- /dev/null +++ b/tests/test_to_yaml.py @@ -0,0 +1,123 @@ +from typing import Any + +import pytest + +from omegaconf import DictConfig, EnumNode, ListConfig, OmegaConf, _utils + +from . import Enum1 + + +@pytest.mark.parametrize( # type: ignore + "input_, expected", + [ + (["item1", "item2", dict(key3="value3")], "- item1\n- item2\n- key3: value3\n"), + (dict(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 + )