Fix zwave_js config validation for values (#127972)

This commit is contained in:
Raman Gupta 2024-10-09 01:35:41 -04:00 committed by Franck Nijhof
parent 66c2fe091b
commit ed445d20b9
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
2 changed files with 25 additions and 19 deletions

View file

@ -34,6 +34,8 @@ def boolean(value: Any) -> bool:
VALUE_SCHEMA = vol.Any(
boolean,
float,
int,
vol.Coerce(int),
vol.Coerce(float),
BITMASK_SCHEMA,

View file

@ -1,27 +1,31 @@
"""Test the Z-Wave JS config validation helpers."""
from typing import Any
import pytest
import voluptuous as vol
from homeassistant.components.zwave_js.config_validation import boolean
from homeassistant.components.zwave_js.config_validation import VALUE_SCHEMA, boolean
def test_boolean_validation() -> None:
"""Test boolean config validator."""
# test bool
assert boolean(True)
assert not boolean(False)
# test strings
assert boolean("TRUE")
assert not boolean("FALSE")
assert boolean("ON")
assert not boolean("NO")
# ensure 1's and 0's don't get converted to bool
@pytest.mark.parametrize(
("test_cases", "expected_value"),
[
([True, "true", "yes", "on", "ON", "enable"], True),
([False, "false", "no", "off", "NO", "disable"], False),
([1.1, "1.1"], 1.1),
([1.0, "1.0"], 1.0),
([1, "1"], 1),
],
)
def test_validation(test_cases: list[Any], expected_value: Any) -> None:
"""Test config validation."""
for case in test_cases:
assert VALUE_SCHEMA(case) == expected_value
@pytest.mark.parametrize("value", ["invalid", "1", "0", 1, 0])
def test_invalid_boolean_validation(value: str | int) -> None:
"""Test invalid cases for boolean config validator."""
with pytest.raises(vol.Invalid):
boolean("1")
with pytest.raises(vol.Invalid):
boolean("0")
with pytest.raises(vol.Invalid):
boolean(1)
with pytest.raises(vol.Invalid):
boolean(0)
boolean(value)