Feature/fix unit tests (#1129)

This commit is contained in:
Peter Kuehne 2020-07-14 12:11:58 +01:00 committed by GitHub
parent c8a4eb426c
commit 2012c769f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 52 deletions

View file

@ -104,7 +104,7 @@ def alphanumeric(value):
raise Invalid("string value is None") raise Invalid("string value is None")
value = str(value) value = str(value)
if not value.isalnum(): if not value.isalnum():
raise Invalid("string value is not alphanumeric") raise Invalid(f"{value} is not alphanumeric")
return value return value

View file

@ -24,15 +24,18 @@ class EsphomeError(Exception):
class HexInt(int): class HexInt(int):
def __str__(self): def __str__(self):
if 0 <= self <= 255: value = self
return f"0x{self:02X}" sign = "-" if value < 0 else ""
return f"0x{self:X}" value = abs(value)
if 0 <= value <= 255:
return f"{sign}0x{value:02X}"
return f"{sign}0x{value:X}"
class IPAddress: class IPAddress:
def __init__(self, *args): def __init__(self, *args):
if len(args) != 4: if len(args) != 4:
raise ValueError("IPAddress must consist up 4 items") raise ValueError("IPAddress must consist of 4 items")
self.args = args self.args = args
def __str__(self): def __str__(self):

View file

@ -9,22 +9,24 @@ from esphome.config_validation import Invalid
from esphome.core import Lambda, HexInt from esphome.core import Lambda, HexInt
def test_check_not_tamplatable__invalid(): def test_check_not_templatable__invalid():
with pytest.raises(Invalid, match="This option is not templatable!"): with pytest.raises(Invalid, match="This option is not templatable!"):
config_validation.check_not_templatable(Lambda("")) config_validation.check_not_templatable(Lambda(""))
@given(one_of( @pytest.mark.parametrize("value", ("foo", 1, "D12", False))
booleans(),
integers(),
text(alphabet=string.ascii_letters + string.digits)),
)
def test_alphanumeric__valid(value): def test_alphanumeric__valid(value):
actual = config_validation.alphanumeric(value) actual = config_validation.alphanumeric(value)
assert actual == str(value) assert actual == str(value)
@pytest.mark.parametrize("value", ("£23", "Foo!"))
def test_alphanumeric__invalid(value):
with pytest.raises(Invalid):
actual = config_validation.alphanumeric(value)
@given(value=text(alphabet=string.ascii_lowercase + string.digits + "_")) @given(value=text(alphabet=string.ascii_lowercase + string.digits + "_"))
def test_valid_name__valid(value): def test_valid_name__valid(value):
actual = config_validation.valid_name(value) actual = config_validation.valid_name(value)
@ -110,4 +112,3 @@ def hex_int__valid(value):
assert isinstance(actual, HexInt) assert isinstance(actual, HexInt)
assert actual == value assert actual == value

View file

@ -121,7 +121,7 @@ def test_walk_files(fixture_path):
actual = list(helpers.walk_files(path)) actual = list(helpers.walk_files(path))
# Ensure paths start with the root # Ensure paths start with the root
assert all(p.startswith(path.as_posix()) for p in actual) assert all(p.startswith(str(path)) for p in actual)
class Test_write_file_if_changed: class Test_write_file_if_changed: