Improve condition validation error msg (#32135)

This commit is contained in:
Paulus Schoutsen 2020-02-24 00:59:34 -08:00 committed by GitHub
parent 270758417b
commit ca01e9a537
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 10 deletions

View file

@ -987,3 +987,36 @@ def test_uuid4_hex(caplog):
_hex = uuid.uuid4().hex
assert schema(_hex) == _hex
assert schema(_hex.upper()) == _hex
def test_key_value_schemas():
"""Test key value schemas."""
schema = vol.Schema(
cv.key_value_schemas(
"mode",
{
"number": vol.Schema({"mode": "number", "data": int}),
"string": vol.Schema({"mode": "string", "data": str}),
},
)
)
with pytest.raises(vol.Invalid) as excinfo:
schema(True)
assert str(excinfo.value) == "Expected a dictionary"
for mode in None, "invalid":
with pytest.raises(vol.Invalid) as excinfo:
schema({"mode": mode})
assert str(excinfo.value) == f"Unexpected key {mode}. Expected number, string"
with pytest.raises(vol.Invalid) as excinfo:
schema({"mode": "number", "data": "string-value"})
assert str(excinfo.value) == "expected int for dictionary value @ data['data']"
with pytest.raises(vol.Invalid) as excinfo:
schema({"mode": "string", "data": 1})
assert str(excinfo.value) == "expected str for dictionary value @ data['data']"
for mode, data in (("number", 1), ("string", "hello")):
schema({"mode": mode, "data": data})