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

@ -7,12 +7,12 @@ from esphome import helpers
@pytest.mark.parametrize("preferred_string, current_strings, expected", ( @pytest.mark.parametrize("preferred_string, current_strings, expected", (
("foo", [], "foo"), ("foo", [], "foo"),
# TODO: Should this actually start at 1? # TODO: Should this actually start at 1?
("foo", ["foo"], "foo_2"), ("foo", ["foo"], "foo_2"),
("foo", ("foo",), "foo_2"), ("foo", ("foo",), "foo_2"),
("foo", ("foo", "foo_2"), "foo_3"), ("foo", ("foo", "foo_2"), "foo_3"),
("foo", ("foo", "foo_2", "foo_2"), "foo_3"), ("foo", ("foo", "foo_2", "foo_2"), "foo_3"),
)) ))
def test_ensure_unique_string(preferred_string, current_strings, expected): def test_ensure_unique_string(preferred_string, current_strings, expected):
actual = helpers.ensure_unique_string(preferred_string, current_strings) actual = helpers.ensure_unique_string(preferred_string, current_strings)
@ -21,9 +21,9 @@ def test_ensure_unique_string(preferred_string, current_strings, expected):
@pytest.mark.parametrize("text, expected", ( @pytest.mark.parametrize("text, expected", (
("foo", "foo"), ("foo", "foo"),
("foo\nbar", "foo\nbar"), ("foo\nbar", "foo\nbar"),
("foo\nbar\neek", "foo\n bar\neek"), ("foo\nbar\neek", "foo\n bar\neek"),
)) ))
def test_indent_all_but_first_and_last(text, expected): def test_indent_all_but_first_and_last(text, expected):
actual = helpers.indent_all_but_first_and_last(text) actual = helpers.indent_all_but_first_and_last(text)
@ -32,9 +32,9 @@ def test_indent_all_but_first_and_last(text, expected):
@pytest.mark.parametrize("text, expected", ( @pytest.mark.parametrize("text, expected", (
("foo", [" foo"]), ("foo", [" foo"]),
("foo\nbar", [" foo", " bar"]), ("foo\nbar", [" foo", " bar"]),
("foo\nbar\neek", [" foo", " bar", " eek"]), ("foo\nbar\neek", [" foo", " bar", " eek"]),
)) ))
def test_indent_list(text, expected): def test_indent_list(text, expected):
actual = helpers.indent_list(text) actual = helpers.indent_list(text)
@ -43,9 +43,9 @@ def test_indent_list(text, expected):
@pytest.mark.parametrize("text, expected", ( @pytest.mark.parametrize("text, expected", (
("foo", " foo"), ("foo", " foo"),
("foo\nbar", " foo\n bar"), ("foo\nbar", " foo\n bar"),
("foo\nbar\neek", " foo\n bar\n eek"), ("foo\nbar\neek", " foo\n bar\n eek"),
)) ))
def test_indent(text, expected): def test_indent(text, expected):
actual = helpers.indent(text) actual = helpers.indent(text)
@ -54,11 +54,11 @@ def test_indent(text, expected):
@pytest.mark.parametrize("string, expected", ( @pytest.mark.parametrize("string, expected", (
("foo", '"foo"'), ("foo", '"foo"'),
("foo\nbar", '"foo\\012bar"'), ("foo\nbar", '"foo\\012bar"'),
("foo\\bar", '"foo\\134bar"'), ("foo\\bar", '"foo\\134bar"'),
('foo "bar"', '"foo \\042bar\\042"'), ('foo "bar"', '"foo \\042bar\\042"'),
('foo 🐍', '"foo \\360\\237\\220\\215"'), ('foo 🐍', '"foo \\360\\237\\220\\215"'),
)) ))
def test_cpp_string_escape(string, expected): def test_cpp_string_escape(string, expected):
actual = helpers.cpp_string_escape(string) actual = helpers.cpp_string_escape(string)
@ -83,11 +83,11 @@ def test_is_ip_address__valid(value):
@pytest.mark.parametrize("var, value, default, expected", ( @pytest.mark.parametrize("var, value, default, expected", (
("FOO", None, False, False), ("FOO", None, False, False),
("FOO", None, True, True), ("FOO", None, True, True),
("FOO", "", False, False), ("FOO", "", False, False),
("FOO", "Yes", False, True), ("FOO", "Yes", False, True),
("FOO", "123", False, True), ("FOO", "123", False, True),
)) ))
def test_get_bool_env(monkeypatch, var, value, default, expected): def test_get_bool_env(monkeypatch, var, value, default, expected):
if value is None: if value is None:
@ -101,8 +101,8 @@ def test_get_bool_env(monkeypatch, var, value, default, expected):
@pytest.mark.parametrize("value, expected", ( @pytest.mark.parametrize("value, expected", (
(None, False), (None, False),
("Yes", True) ("Yes", True)
)) ))
def test_is_hassio(monkeypatch, value, expected): def test_is_hassio(monkeypatch, value, expected):
if value is None: if value is None:
@ -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:
@ -186,18 +186,18 @@ class Test_copy_file_if_changed:
@pytest.mark.parametrize("file1, file2, expected", ( @pytest.mark.parametrize("file1, file2, expected", (
# Same file # Same file
("file-a.txt", "file-a.txt", True), ("file-a.txt", "file-a.txt", True),
# Different files, different size # Different files, different size
("file-a.txt", "file-b_1.txt", False), ("file-a.txt", "file-b_1.txt", False),
# Different files, same size # Different files, same size
("file-a.txt", "file-c.txt", False), ("file-a.txt", "file-c.txt", False),
# Same files # Same files
("file-b_1.txt", "file-b_2.txt", True), ("file-b_1.txt", "file-b_2.txt", True),
# Not a file # Not a file
("file-a.txt", "", False), ("file-a.txt", "", False),
# File doesn't exist # File doesn't exist
("file-a.txt", "file-d.txt", False), ("file-a.txt", "file-d.txt", False),
)) ))
def test_file_compare(fixture_path, file1, file2, expected): def test_file_compare(fixture_path, file1, file2, expected):
path1 = fixture_path / "helpers" / file1 path1 = fixture_path / "helpers" / file1