Improve condition validation error msg (#32135)
This commit is contained in:
parent
270758417b
commit
ca01e9a537
2 changed files with 70 additions and 10 deletions
|
@ -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})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue