Skip to content

Commit

Permalink
Cover more cases in test_dict_get_with_default_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
odelalleau committed Mar 2, 2021
1 parent 16734a7 commit 2f85931
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions tests/test_basic_ops_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,43 +182,46 @@ def test_scientific_notation_float() -> None:

@pytest.mark.parametrize("struct", [None, True, False])
@pytest.mark.parametrize("default_val", [4, True, False, None])
@pytest.mark.parametrize(
"d,select,key",
[
({"hello": {"a": 2}}, "", "missing"),
({"hello": {"a": 2}}, "hello", "missing"),
({"hello": "???"}, "", "hello"),
({"hello": None}, "", "hello"),
({"hello": DictConfig(is_optional=True, content=None)}, "", "hello"),
({"hello": DictConfig(content="???")}, "", "hello"),
({"hello": ListConfig(is_optional=True, content=None)}, "", "hello"),
({"hello": ListConfig(content="???")}, "", "hello"),
],
)
def test_dict_get_with_default(
d: Any, select: Any, key: Any, default_val: Any, struct: Any
) -> None:
c = OmegaConf.create(d)
c = OmegaConf.select(c, select)
OmegaConf.set_struct(c, struct)
assert c.get(key, default_val) == default_val


@pytest.mark.parametrize(
"d,exc",
[
({"hello": "${foo}"}, InterpolationKeyError),
(
{"hello": "${foo}", "foo": "???"},
InterpolationToMissingValueError,
),
({"hello": DictConfig(content="${foo}")}, InterpolationKeyError),
],
)
def test_dict_get_with_default_errors(d: Any, exc: type) -> None:
c = OmegaConf.create(d)
with pytest.raises(exc):
c.get("hello", default_value=123)
class TestGetWithDefault:
@pytest.mark.parametrize(
"d,select,key",
[
({"hello": {"a": 2}}, "", "missing"),
({"hello": {"a": 2}}, "hello", "missing"),
({"hello": "???"}, "", "hello"),
({"hello": None}, "", "hello"),
({"hello": DictConfig(is_optional=True, content=None)}, "", "hello"),
({"hello": DictConfig(content="???")}, "", "hello"),
({"hello": ListConfig(is_optional=True, content=None)}, "", "hello"),
({"hello": ListConfig(content="???")}, "", "hello"),
],
)
def test_dict_get_with_default(
self, d: Any, select: Any, key: Any, default_val: Any, struct: Optional[bool]
) -> None:
c = OmegaConf.create(d)
c = OmegaConf.select(c, select)
OmegaConf.set_struct(c, struct)
assert c.get(key, default_val) == default_val

@pytest.mark.parametrize(
"d,exc",
[
({"hello": "${foo}"}, InterpolationKeyError),
(
{"hello": "${foo}", "foo": "???"},
InterpolationToMissingValueError,
),
({"hello": DictConfig(content="${foo}")}, InterpolationKeyError),
],
)
def test_dict_get_with_default_errors(
self, d: Any, exc: type, struct: Optional[bool], default_val: Any
) -> None:
c = OmegaConf.create(d)
OmegaConf.set_struct(c, struct)
with pytest.raises(exc):
c.get("hello", default_value=123)


def test_map_expansion() -> None:
Expand Down

0 comments on commit 2f85931

Please sign in to comment.