Exemple #1
0
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_with_enum() -> None:
    cfg = OmegaConf.create()
    assert isinstance(cfg, DictConfig)
    cfg.foo = EnumNode(Enum1)
    cfg.foo = Enum1.FOO

    expected = """foo: Enum1.FOO
"""
    assert cfg.pretty() == expected
Exemple #3
0
    assert OmegaConf.is_interpolation(cfg, key) == inter


# for each type Node type : int, bool, str, float, Color (enum) and User (@dataclass), DictConfig, ListConfig
#   for each MISSING, None, Optional and interpolation:
@pytest.mark.parametrize(
    "node_type, values",
    [
        (BooleanNode, [True, False]),
        (FloatNode, [3.1415]),
        (IntegerNode, [42]),
        (StringNode, ["hello"]),
        # EnumNode
        (
            lambda value, is_optional, key=None: EnumNode(
                enum_type=Color, value=value, is_optional=is_optional, key=key
            ),
            [Color.RED],
        ),
        # DictConfig
        (
            lambda value, is_optional, key=None: DictConfig(
                is_optional=is_optional, content=value, key=key),
            [{}, {
                "foo": "bar"
            }],
        ),
        # ListConfig
        (
            lambda value, is_optional, key=None: ListConfig(
                is_optional=is_optional, content=value, key=key),
Exemple #4
0
        (BooleanNode, False, False),
        (BooleanNode, "N", False),
        (BooleanNode, "false", False),
        (BooleanNode, "No", False),
        (BooleanNode, "Off", False),
        (BooleanNode, None, None),
        (BooleanNode, "0", False),
        (BooleanNode, 0, False),
        # any
        (AnyNode, 3, 3),
        (AnyNode, 3.14, 3.14),
        (AnyNode, False, False),
        (AnyNode, Color.RED, Color.RED),
        (AnyNode, None, None),
        # Enum node
        (lambda v: EnumNode(enum_type=Color, value=v), Color.RED, Color.RED),
        (lambda v: EnumNode(enum_type=Color, value=v), "Color.RED", Color.RED),
        (lambda v: EnumNode(enum_type=Color, value=v), "RED", Color.RED),
        (lambda v: EnumNode(enum_type=Color, value=v), 1, Color.RED),
    ],
)
def test_valid_inputs(type_: type, input_: Any, output_: Any) -> None:
    node = type_(input_)
    assert node == output_
    assert node == node
    assert not (node != output_)
    assert not (node != node)
    assert str(node) == str(output_)


# testing invalid conversions
Exemple #5
0
@fixture
def resolver() -> Any:
    yield OmegaConfUserResolver()


@mark.parametrize(
    ("obj", "expected"),
    [
        # nodes
        param(AnyNode(10), {}, id="any:10"),
        param(StringNode("foo"), {}, id="str:foo"),
        param(IntegerNode(10), {}, id="int:10"),
        param(FloatNode(3.14), {}, id="float:3.14"),
        param(BooleanNode(True), {}, id="bool:True"),
        param(BytesNode(b"binary"), {}, id="bytes:binary"),
        param(EnumNode(enum_type=Color, value=Color.RED), {},
              id="Color:Color.RED"),
        # nodes are never returning a dictionary
        param(AnyNode("${foo}", parent=DictConfig({"foo": 10})), {},
              id="any:inter_10"),
        # DictConfig
        param(DictConfig({"a": 10}), {"a": AnyNode(10)}, id="dict"),
        param(
            DictConfig({
                "a": 10,
                "b": "${a}"
            }),
            {
                "a": AnyNode(10),
                "b": AnyNode("${a}")
            },
Exemple #6
0
@pytest.mark.parametrize(  # type: ignore
    "is_optional", [True, False])
@pytest.mark.parametrize(  # type: ignore
    "fac",
    [
        (lambda is_optional, missing: StringNode(
            value="foo" if not missing else "???", is_optional=is_optional)),
        (lambda is_optional, missing: IntegerNode(
            value=10 if not missing else "???", is_optional=is_optional)),
        (lambda is_optional, missing: FloatNode(
            value=10 if not missing else "???", is_optional=is_optional)),
        (lambda is_optional, missing: BooleanNode(
            value=True if not missing else "???", is_optional=is_optional)),
        (lambda is_optional, missing: EnumNode(
            enum_type=Color,
            value=Color.RED if not missing else "???",
            is_optional=is_optional,
        )),
        (lambda is_optional, missing: ListConfig(content=[1, 2, 3]
                                                 if not missing else "???",
                                                 is_optional=is_optional)),
        (lambda is_optional, missing: DictConfig(
            content={"foo": "bar"} if not missing else "???",
            is_optional=is_optional,
        )),
        (lambda is_optional, missing: DictConfig(
            ref_type=ConcretePlugin,
            content=ConcretePlugin() if not missing else "???",
            is_optional=is_optional,
        )),
    ],
Exemple #7
0
@mark.parametrize("is_none", [True, False])
@mark.parametrize(
    "fac",
    [
        (lambda none: StringNode(value="foo" if not none else None,
                                 is_optional=True)),
        (lambda none: IntegerNode(value=10 if not none else None,
                                  is_optional=True)),
        (lambda none: FloatNode(value=10 if not none else None,
                                is_optional=True)),
        (lambda none: BooleanNode(value=True if not none else None,
                                  is_optional=True)),
        (lambda none: EnumNode(
            enum_type=Color,
            value=Color.RED if not none else None,
            is_optional=True,
        )),
        (lambda none: ListConfig(content=[1, 2, 3] if not none else None,
                                 is_optional=True)),
        (lambda none: DictConfig(content={"foo": "bar"} if not none else None,
                                 is_optional=True)),
        (lambda none: DictConfig(
            ref_type=ConcretePlugin,
            content=ConcretePlugin() if not none else None,
            is_optional=True,
        )),
    ],
)
def test_is_none(fac: Any, is_none: bool) -> None:
    obj = fac(is_none)
            if callable(expected):
                expected = expected(value)
            node = type_(value)
            assert node.value() == expected
        else:
            with pytest.raises(ValidationError):
                type_(value)


@pytest.mark.parametrize(  # type: ignore
    "node,value",
    [
        (IntegerNode(), "foo"),
        (BooleanNode(), "foo"),
        (FloatNode(), "foo"),
        (EnumNode(enum_type=Enum1), "foo"),
    ],
)
def test_illegal_assignment(node: ValueNode, value: Any) -> None:
    with pytest.raises(ValidationError):
        node.set_value(value)


@pytest.mark.parametrize(  # type: ignore
    "node_type",
    [BooleanNode, EnumNode, FloatNode, IntegerNode, StringNode, AnyNode])
@pytest.mark.parametrize(  # type: ignore
    "enum_type, values, success_map",
    [(
        Enum1,
        (Enum1.FOO, "Enum1.FOO", "FOO", 1),
Exemple #9
0
@fixture
def resolver() -> Any:
    yield OmegaConfUserResolver()


@mark.parametrize(
    ("obj", "expected"),
    [
        # nodes
        param(AnyNode(10), {}, id="any:10"),
        param(StringNode("foo"), {}, id="str:foo"),
        param(IntegerNode(10), {}, id="int:10"),
        param(FloatNode(3.14), {}, id="float:3.14"),
        param(BooleanNode(True), {}, id="bool:True"),
        param(EnumNode(enum_type=Color, value=Color.RED), {}, id="Color:Color.RED"),
        # nodes are never returning a dictionary
        param(AnyNode("${foo}", parent=DictConfig({"foo": 10})), {}, id="any:inter_10"),
        # DictConfig
        param(DictConfig({"a": 10}), {"a": AnyNode(10)}, id="dict"),
        param(
            DictConfig({"a": 10, "b": "${a}"}),
            {"a": AnyNode(10), "b": AnyNode("${a}")},
            id="dict:interpolation_value",
        ),
        param(
            DictConfig({"a": 10, "b": "${zzz}"}),
            {"a": AnyNode(10), "b": AnyNode("${zzz}")},
            id="dict:interpolation_value_error",
        ),
        param(
Exemple #10
0
 (BooleanNode, "N", False),
 (BooleanNode, "false", False),
 (BooleanNode, "No", False),
 (BooleanNode, "Off", False),
 (BooleanNode, None, None),
 (BooleanNode, "0", False),
 (BooleanNode, 0, False),
 # any
 (AnyNode, 3, 3),
 (AnyNode, 3.14, 3.14),
 (AnyNode, False, False),
 (AnyNode, b"\xf0\xf1\xf2", b"\xf0\xf1\xf2"),
 (AnyNode, Color.RED, Color.RED),
 (AnyNode, None, None),
 # Enum node
 (lambda v: EnumNode(enum_type=Color, value=v), Color.RED, Color.RED),
 (lambda v: EnumNode(enum_type=Color, value=v), "Color.RED", Color.RED),
 (lambda v: EnumNode(enum_type=Color, value=v), "RED", Color.RED),
 (lambda v: EnumNode(enum_type=Color, value=v), 1, Color.RED),
 # Path node
 (PathNode, "hello.txt", Path("hello.txt")),
 (PathNode, Path("hello.txt"), Path("hello.txt")),
 # Union node
 param(_build_union, "abc", "abc", id="union-str"),
 param(_build_union, 10, 10, id="union-int"),
 param(_build_union, 10.1, 10.1, id="union-float"),
 param(_build_union, float("inf"), float("inf"), id="union-inf"),
 param(_build_union,
       b"binary\xf0\xf1",
       b"binary\xf0\xf1",
       id="union-bytes"),