Exemple #1
0
        if type_.__name__ in success_map.keys():
            expected = success_map[type_.__name__]
            if callable(expected):
                expected = expected(value)
            node = type_(value)
            assert node._value() == expected
        else:
            with pytest.raises(ValidationError):
                type_(value)


@pytest.mark.parametrize(
    "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(
    "node_type", [BooleanNode, EnumNode, FloatNode, IntegerNode, StringNode, AnyNode]
)
@pytest.mark.parametrize(
    "enum_type, values, success_map",
    [
Exemple #2
0
def test_is_dict(cfg: Any, expected: bool) -> None:
    assert OmegaConf.is_dict(cfg) == expected


@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 "???",
Exemple #3
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),
Exemple #4
0
        assert OmegaConf.is_optional(cfg, "node")
    with warns(UserWarning):
        assert OmegaConf.is_optional("not_a_node")


@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,
        )),
    ],