2020-03-12 22:27:22 +01:00
|
|
|
import pytest
|
|
|
|
import string
|
|
|
|
|
|
|
|
from hypothesis import given, example
|
2021-03-07 20:03:16 +01:00
|
|
|
from hypothesis.strategies import one_of, text, integers, builds
|
2020-03-12 22:27:22 +01:00
|
|
|
|
|
|
|
from esphome import config_validation
|
|
|
|
from esphome.config_validation import Invalid
|
|
|
|
from esphome.core import Lambda, HexInt
|
|
|
|
|
|
|
|
|
2020-07-14 13:11:58 +02:00
|
|
|
def test_check_not_templatable__invalid():
|
2020-03-12 22:27:22 +01:00
|
|
|
with pytest.raises(Invalid, match="This option is not templatable!"):
|
|
|
|
config_validation.check_not_templatable(Lambda(""))
|
|
|
|
|
|
|
|
|
2020-07-14 13:11:58 +02:00
|
|
|
@pytest.mark.parametrize("value", ("foo", 1, "D12", False))
|
2020-03-12 22:27:22 +01:00
|
|
|
def test_alphanumeric__valid(value):
|
|
|
|
actual = config_validation.alphanumeric(value)
|
|
|
|
|
|
|
|
assert actual == str(value)
|
|
|
|
|
|
|
|
|
2020-07-14 13:11:58 +02:00
|
|
|
@pytest.mark.parametrize("value", ("£23", "Foo!"))
|
|
|
|
def test_alphanumeric__invalid(value):
|
|
|
|
with pytest.raises(Invalid):
|
2021-03-07 20:03:16 +01:00
|
|
|
config_validation.alphanumeric(value)
|
2020-07-14 13:11:58 +02:00
|
|
|
|
|
|
|
|
2021-04-08 14:26:01 +02:00
|
|
|
@given(value=text(alphabet=string.ascii_lowercase + string.digits + "-_"))
|
2020-03-12 22:27:22 +01:00
|
|
|
def test_valid_name__valid(value):
|
|
|
|
actual = config_validation.valid_name(value)
|
|
|
|
|
|
|
|
assert actual == value
|
|
|
|
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
@pytest.mark.parametrize("value", ("foo bar", "FooBar", "foo::bar"))
|
2020-03-12 22:27:22 +01:00
|
|
|
def test_valid_name__invalid(value):
|
|
|
|
with pytest.raises(Invalid):
|
|
|
|
config_validation.valid_name(value)
|
|
|
|
|
|
|
|
|
|
|
|
@given(one_of(integers(), text()))
|
|
|
|
def test_string__valid(value):
|
|
|
|
actual = config_validation.string(value)
|
|
|
|
|
|
|
|
assert actual == str(value)
|
|
|
|
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
@pytest.mark.parametrize("value", ({}, [], True, False, None))
|
2020-03-12 22:27:22 +01:00
|
|
|
def test_string__invalid(value):
|
|
|
|
with pytest.raises(Invalid):
|
|
|
|
config_validation.string(value)
|
|
|
|
|
|
|
|
|
|
|
|
@given(text())
|
|
|
|
def test_strict_string__valid(value):
|
|
|
|
actual = config_validation.string_strict(value)
|
|
|
|
|
|
|
|
assert actual == value
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("value", (None, 123))
|
|
|
|
def test_string_string__invalid(value):
|
|
|
|
with pytest.raises(Invalid, match="Must be string, got"):
|
|
|
|
config_validation.string_strict(value)
|
|
|
|
|
|
|
|
|
2021-11-23 09:21:14 +01:00
|
|
|
@given(builds(lambda v: "mdi:" + v, text(alphabet=string.ascii_letters + string.digits + "-_", min_size=1, max_size=20)))
|
2020-03-12 22:27:22 +01:00
|
|
|
@example("")
|
|
|
|
def test_icon__valid(value):
|
|
|
|
actual = config_validation.icon(value)
|
|
|
|
|
|
|
|
assert actual == value
|
|
|
|
|
|
|
|
|
|
|
|
def test_icon__invalid():
|
2021-11-23 09:21:14 +01:00
|
|
|
with pytest.raises(Invalid, match="Icons must match the format "):
|
2020-03-12 22:27:22 +01:00
|
|
|
config_validation.icon("foo")
|
|
|
|
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
@pytest.mark.parametrize("value", ("True", "YES", "on", "enAblE", True))
|
2020-03-12 22:27:22 +01:00
|
|
|
def test_boolean__valid_true(value):
|
|
|
|
assert config_validation.boolean(value) is True
|
|
|
|
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
@pytest.mark.parametrize("value", ("False", "NO", "off", "disAblE", False))
|
2020-03-12 22:27:22 +01:00
|
|
|
def test_boolean__valid_false(value):
|
|
|
|
assert config_validation.boolean(value) is False
|
|
|
|
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
@pytest.mark.parametrize("value", (None, 1, 0, "foo"))
|
2020-03-12 22:27:22 +01:00
|
|
|
def test_boolean__invalid(value):
|
|
|
|
with pytest.raises(Invalid, match="Expected boolean value"):
|
|
|
|
config_validation.boolean(value)
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: ensure_list
|
|
|
|
@given(integers())
|
|
|
|
def hex_int__valid(value):
|
|
|
|
actual = config_validation.hex_int(value)
|
|
|
|
|
|
|
|
assert isinstance(actual, HexInt)
|
|
|
|
assert actual == value
|