mirror of
https://github.com/esphome/esphome.git
synced 2025-09-02 03:12:20 +01:00
add-black (#1593)
* Add black Update pre commit Update pre commit add empty line * Format with black
This commit is contained in:
committed by
GitHub
parent
2b60b0f1fa
commit
69879920eb
@@ -8,13 +8,16 @@ from esphome import core, const
|
||||
|
||||
|
||||
class TestHexInt:
|
||||
@pytest.mark.parametrize("value, expected", (
|
||||
(1, "0x01"),
|
||||
(255, "0xFF"),
|
||||
(128, "0x80"),
|
||||
(256, "0x100"),
|
||||
(-1, "-0x01"), # TODO: this currently fails
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"value, expected",
|
||||
(
|
||||
(1, "0x01"),
|
||||
(255, "0xFF"),
|
||||
(128, "0x80"),
|
||||
(256, "0x100"),
|
||||
(-1, "-0x01"), # TODO: this currently fails
|
||||
),
|
||||
)
|
||||
def test_str(self, value, expected):
|
||||
target = core.HexInt(value)
|
||||
|
||||
@@ -68,18 +71,14 @@ class TestMACAddress:
|
||||
assert actual.text == "0xDEADBEEF00FFULL"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", (
|
||||
1, 2, -1, 0, 1.0, -1.0, 42.0009, -42.0009
|
||||
))
|
||||
@pytest.mark.parametrize("value", (1, 2, -1, 0, 1.0, -1.0, 42.0009, -42.0009))
|
||||
def test_is_approximately_integer__in_range(value):
|
||||
actual = core.is_approximately_integer(value)
|
||||
|
||||
assert actual is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", (
|
||||
42.01, -42.01, 1.5
|
||||
))
|
||||
@pytest.mark.parametrize("value", (42.01, -42.01, 1.5))
|
||||
def test_is_approximately_integer__not_in_range(value):
|
||||
actual = core.is_approximately_integer(value)
|
||||
|
||||
@@ -87,26 +86,29 @@ def test_is_approximately_integer__not_in_range(value):
|
||||
|
||||
|
||||
class TestTimePeriod:
|
||||
@pytest.mark.parametrize("kwargs, expected", (
|
||||
({}, {}),
|
||||
({"microseconds": 1}, {"microseconds": 1}),
|
||||
({"microseconds": 1.0001}, {"microseconds": 1}),
|
||||
({"milliseconds": 2}, {"milliseconds": 2}),
|
||||
({"milliseconds": 2.0001}, {"milliseconds": 2}),
|
||||
({"milliseconds": 2.01}, {"milliseconds": 2, "microseconds": 10}),
|
||||
({"seconds": 3}, {"seconds": 3}),
|
||||
({"seconds": 3.0001}, {"seconds": 3}),
|
||||
({"seconds": 3.01}, {"seconds": 3, "milliseconds": 10}),
|
||||
({"minutes": 4}, {"minutes": 4}),
|
||||
({"minutes": 4.0001}, {"minutes": 4}),
|
||||
({"minutes": 4.1}, {"minutes": 4, "seconds": 6}),
|
||||
({"hours": 5}, {"hours": 5}),
|
||||
({"hours": 5.0001}, {"hours": 5}),
|
||||
({"hours": 5.1}, {"hours": 5, "minutes": 6}),
|
||||
({"days": 6}, {"days": 6}),
|
||||
({"days": 6.0001}, {"days": 6}),
|
||||
({"days": 6.1}, {"days": 6, "hours": 2, "minutes": 24}),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs, expected",
|
||||
(
|
||||
({}, {}),
|
||||
({"microseconds": 1}, {"microseconds": 1}),
|
||||
({"microseconds": 1.0001}, {"microseconds": 1}),
|
||||
({"milliseconds": 2}, {"milliseconds": 2}),
|
||||
({"milliseconds": 2.0001}, {"milliseconds": 2}),
|
||||
({"milliseconds": 2.01}, {"milliseconds": 2, "microseconds": 10}),
|
||||
({"seconds": 3}, {"seconds": 3}),
|
||||
({"seconds": 3.0001}, {"seconds": 3}),
|
||||
({"seconds": 3.01}, {"seconds": 3, "milliseconds": 10}),
|
||||
({"minutes": 4}, {"minutes": 4}),
|
||||
({"minutes": 4.0001}, {"minutes": 4}),
|
||||
({"minutes": 4.1}, {"minutes": 4, "seconds": 6}),
|
||||
({"hours": 5}, {"hours": 5}),
|
||||
({"hours": 5.0001}, {"hours": 5}),
|
||||
({"hours": 5.1}, {"hours": 5, "minutes": 6}),
|
||||
({"days": 6}, {"days": 6}),
|
||||
({"days": 6.0001}, {"days": 6}),
|
||||
({"days": 6.1}, {"days": 6, "hours": 2, "minutes": 24}),
|
||||
),
|
||||
)
|
||||
def test_init(self, kwargs, expected):
|
||||
target = core.TimePeriod(**kwargs)
|
||||
|
||||
@@ -118,26 +120,29 @@ class TestTimePeriod:
|
||||
with pytest.raises(ValueError, match="Maximum precision is microseconds"):
|
||||
core.TimePeriod(microseconds=1.1)
|
||||
|
||||
@pytest.mark.parametrize("kwargs, expected", (
|
||||
({}, "0s"),
|
||||
({"microseconds": 1}, "1us"),
|
||||
({"microseconds": 1.0001}, "1us"),
|
||||
({"milliseconds": 2}, "2ms"),
|
||||
({"milliseconds": 2.0001}, "2ms"),
|
||||
({"milliseconds": 2.01}, "2010us"),
|
||||
({"seconds": 3}, "3s"),
|
||||
({"seconds": 3.0001}, "3s"),
|
||||
({"seconds": 3.01}, "3010ms"),
|
||||
({"minutes": 4}, "4min"),
|
||||
({"minutes": 4.0001}, "4min"),
|
||||
({"minutes": 4.1}, "246s"),
|
||||
({"hours": 5}, "5h"),
|
||||
({"hours": 5.0001}, "5h"),
|
||||
({"hours": 5.1}, "306min"),
|
||||
({"days": 6}, "6d"),
|
||||
({"days": 6.0001}, "6d"),
|
||||
({"days": 6.1}, "8784min"),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs, expected",
|
||||
(
|
||||
({}, "0s"),
|
||||
({"microseconds": 1}, "1us"),
|
||||
({"microseconds": 1.0001}, "1us"),
|
||||
({"milliseconds": 2}, "2ms"),
|
||||
({"milliseconds": 2.0001}, "2ms"),
|
||||
({"milliseconds": 2.01}, "2010us"),
|
||||
({"seconds": 3}, "3s"),
|
||||
({"seconds": 3.0001}, "3s"),
|
||||
({"seconds": 3.01}, "3010ms"),
|
||||
({"minutes": 4}, "4min"),
|
||||
({"minutes": 4.0001}, "4min"),
|
||||
({"minutes": 4.1}, "246s"),
|
||||
({"hours": 5}, "5h"),
|
||||
({"hours": 5.0001}, "5h"),
|
||||
({"hours": 5.1}, "306min"),
|
||||
({"days": 6}, "6d"),
|
||||
({"days": 6.0001}, "6d"),
|
||||
({"days": 6.1}, "8784min"),
|
||||
),
|
||||
)
|
||||
def test_str(self, kwargs, expected):
|
||||
target = core.TimePeriod(**kwargs)
|
||||
|
||||
@@ -145,61 +150,59 @@ class TestTimePeriod:
|
||||
|
||||
assert actual == expected
|
||||
|
||||
@pytest.mark.parametrize("comparison, other, expected", (
|
||||
("__eq__", core.TimePeriod(microseconds=900), False),
|
||||
("__eq__", core.TimePeriod(milliseconds=1), True),
|
||||
("__eq__", core.TimePeriod(microseconds=1100), False),
|
||||
("__eq__", 1000, NotImplemented),
|
||||
("__eq__", "1000", NotImplemented),
|
||||
("__eq__", True, NotImplemented),
|
||||
("__eq__", object(), NotImplemented),
|
||||
("__eq__", None, NotImplemented),
|
||||
|
||||
("__ne__", core.TimePeriod(microseconds=900), True),
|
||||
("__ne__", core.TimePeriod(milliseconds=1), False),
|
||||
("__ne__", core.TimePeriod(microseconds=1100), True),
|
||||
("__ne__", 1000, NotImplemented),
|
||||
("__ne__", "1000", NotImplemented),
|
||||
("__ne__", True, NotImplemented),
|
||||
("__ne__", object(), NotImplemented),
|
||||
("__ne__", None, NotImplemented),
|
||||
|
||||
("__lt__", core.TimePeriod(microseconds=900), False),
|
||||
("__lt__", core.TimePeriod(milliseconds=1), False),
|
||||
("__lt__", core.TimePeriod(microseconds=1100), True),
|
||||
("__lt__", 1000, NotImplemented),
|
||||
("__lt__", "1000", NotImplemented),
|
||||
("__lt__", True, NotImplemented),
|
||||
("__lt__", object(), NotImplemented),
|
||||
("__lt__", None, NotImplemented),
|
||||
|
||||
("__gt__", core.TimePeriod(microseconds=900), True),
|
||||
("__gt__", core.TimePeriod(milliseconds=1), False),
|
||||
("__gt__", core.TimePeriod(microseconds=1100), False),
|
||||
("__gt__", 1000, NotImplemented),
|
||||
("__gt__", "1000", NotImplemented),
|
||||
("__gt__", True, NotImplemented),
|
||||
("__gt__", object(), NotImplemented),
|
||||
("__gt__", None, NotImplemented),
|
||||
|
||||
("__le__", core.TimePeriod(microseconds=900), False),
|
||||
("__le__", core.TimePeriod(milliseconds=1), True),
|
||||
("__le__", core.TimePeriod(microseconds=1100), True),
|
||||
("__le__", 1000, NotImplemented),
|
||||
("__le__", "1000", NotImplemented),
|
||||
("__le__", True, NotImplemented),
|
||||
("__le__", object(), NotImplemented),
|
||||
("__le__", None, NotImplemented),
|
||||
|
||||
("__ge__", core.TimePeriod(microseconds=900), True),
|
||||
("__ge__", core.TimePeriod(milliseconds=1), True),
|
||||
("__ge__", core.TimePeriod(microseconds=1100), False),
|
||||
("__ge__", 1000, NotImplemented),
|
||||
("__ge__", "1000", NotImplemented),
|
||||
("__ge__", True, NotImplemented),
|
||||
("__ge__", object(), NotImplemented),
|
||||
("__ge__", None, NotImplemented),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"comparison, other, expected",
|
||||
(
|
||||
("__eq__", core.TimePeriod(microseconds=900), False),
|
||||
("__eq__", core.TimePeriod(milliseconds=1), True),
|
||||
("__eq__", core.TimePeriod(microseconds=1100), False),
|
||||
("__eq__", 1000, NotImplemented),
|
||||
("__eq__", "1000", NotImplemented),
|
||||
("__eq__", True, NotImplemented),
|
||||
("__eq__", object(), NotImplemented),
|
||||
("__eq__", None, NotImplemented),
|
||||
("__ne__", core.TimePeriod(microseconds=900), True),
|
||||
("__ne__", core.TimePeriod(milliseconds=1), False),
|
||||
("__ne__", core.TimePeriod(microseconds=1100), True),
|
||||
("__ne__", 1000, NotImplemented),
|
||||
("__ne__", "1000", NotImplemented),
|
||||
("__ne__", True, NotImplemented),
|
||||
("__ne__", object(), NotImplemented),
|
||||
("__ne__", None, NotImplemented),
|
||||
("__lt__", core.TimePeriod(microseconds=900), False),
|
||||
("__lt__", core.TimePeriod(milliseconds=1), False),
|
||||
("__lt__", core.TimePeriod(microseconds=1100), True),
|
||||
("__lt__", 1000, NotImplemented),
|
||||
("__lt__", "1000", NotImplemented),
|
||||
("__lt__", True, NotImplemented),
|
||||
("__lt__", object(), NotImplemented),
|
||||
("__lt__", None, NotImplemented),
|
||||
("__gt__", core.TimePeriod(microseconds=900), True),
|
||||
("__gt__", core.TimePeriod(milliseconds=1), False),
|
||||
("__gt__", core.TimePeriod(microseconds=1100), False),
|
||||
("__gt__", 1000, NotImplemented),
|
||||
("__gt__", "1000", NotImplemented),
|
||||
("__gt__", True, NotImplemented),
|
||||
("__gt__", object(), NotImplemented),
|
||||
("__gt__", None, NotImplemented),
|
||||
("__le__", core.TimePeriod(microseconds=900), False),
|
||||
("__le__", core.TimePeriod(milliseconds=1), True),
|
||||
("__le__", core.TimePeriod(microseconds=1100), True),
|
||||
("__le__", 1000, NotImplemented),
|
||||
("__le__", "1000", NotImplemented),
|
||||
("__le__", True, NotImplemented),
|
||||
("__le__", object(), NotImplemented),
|
||||
("__le__", None, NotImplemented),
|
||||
("__ge__", core.TimePeriod(microseconds=900), True),
|
||||
("__ge__", core.TimePeriod(milliseconds=1), True),
|
||||
("__ge__", core.TimePeriod(microseconds=1100), False),
|
||||
("__ge__", 1000, NotImplemented),
|
||||
("__ge__", "1000", NotImplemented),
|
||||
("__ge__", True, NotImplemented),
|
||||
("__ge__", object(), NotImplemented),
|
||||
("__ge__", None, NotImplemented),
|
||||
),
|
||||
)
|
||||
def test_comparison(self, comparison, other, expected):
|
||||
target = core.TimePeriod(microseconds=1000)
|
||||
|
||||
@@ -238,19 +241,19 @@ class TestLambda:
|
||||
"it.strftime(64, 0, ",
|
||||
"my_font",
|
||||
"",
|
||||
", TextAlign::TOP_CENTER, \"%H:%M:%S\", ",
|
||||
', TextAlign::TOP_CENTER, "%H:%M:%S", ',
|
||||
"esptime",
|
||||
".",
|
||||
"now());\nit.printf(64, 16, ",
|
||||
"my_font2",
|
||||
"",
|
||||
", TextAlign::TOP_CENTER, \"%.1f°C (%.1f%%)\", ",
|
||||
', TextAlign::TOP_CENTER, "%.1f°C (%.1f%%)", ',
|
||||
"office_tmp",
|
||||
".",
|
||||
"state, ",
|
||||
"office_hmd",
|
||||
".",
|
||||
"state);\n \nint x = 4; "
|
||||
"state);\n \nint x = 4; ",
|
||||
]
|
||||
|
||||
def test_requires_ids(self):
|
||||
@@ -296,24 +299,33 @@ class TestID:
|
||||
def target(self):
|
||||
return core.ID(None, is_declaration=True, type="binary_sensor::Example")
|
||||
|
||||
@pytest.mark.parametrize("id, is_manual, expected", (
|
||||
("foo", None, True),
|
||||
(None, None, False),
|
||||
("foo", True, True),
|
||||
("foo", False, False),
|
||||
(None, True, True),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"id, is_manual, expected",
|
||||
(
|
||||
("foo", None, True),
|
||||
(None, None, False),
|
||||
("foo", True, True),
|
||||
("foo", False, False),
|
||||
(None, True, True),
|
||||
),
|
||||
)
|
||||
def test_init__resolve_is_manual(self, id, is_manual, expected):
|
||||
target = core.ID(id, is_manual=is_manual)
|
||||
|
||||
assert target.is_manual == expected
|
||||
|
||||
@pytest.mark.parametrize("registered_ids, expected", (
|
||||
([], "binary_sensor_example"),
|
||||
(["binary_sensor_example"], "binary_sensor_example_2"),
|
||||
(["foo"], "binary_sensor_example"),
|
||||
(["binary_sensor_example", "foo", "binary_sensor_example_2"], "binary_sensor_example_3"),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"registered_ids, expected",
|
||||
(
|
||||
([], "binary_sensor_example"),
|
||||
(["binary_sensor_example"], "binary_sensor_example_2"),
|
||||
(["foo"], "binary_sensor_example"),
|
||||
(
|
||||
["binary_sensor_example", "foo", "binary_sensor_example_2"],
|
||||
"binary_sensor_example_3",
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_resolve(self, target, registered_ids, expected):
|
||||
actual = target.resolve(registered_ids)
|
||||
|
||||
@@ -326,18 +338,23 @@ class TestID:
|
||||
actual = target.copy()
|
||||
|
||||
assert actual is not target
|
||||
assert all(getattr(actual, n) == getattr(target, n)
|
||||
for n in ("id", "is_declaration", "type", "is_manual"))
|
||||
assert all(
|
||||
getattr(actual, n) == getattr(target, n)
|
||||
for n in ("id", "is_declaration", "type", "is_manual")
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize("comparison, other, expected", (
|
||||
("__eq__", core.ID(id="foo"), True),
|
||||
("__eq__", core.ID(id="bar"), False),
|
||||
("__eq__", 1000, NotImplemented),
|
||||
("__eq__", "1000", NotImplemented),
|
||||
("__eq__", True, NotImplemented),
|
||||
("__eq__", object(), NotImplemented),
|
||||
("__eq__", None, NotImplemented),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"comparison, other, expected",
|
||||
(
|
||||
("__eq__", core.ID(id="foo"), True),
|
||||
("__eq__", core.ID(id="bar"), False),
|
||||
("__eq__", 1000, NotImplemented),
|
||||
("__eq__", "1000", NotImplemented),
|
||||
("__eq__", True, NotImplemented),
|
||||
("__eq__", object(), NotImplemented),
|
||||
("__eq__", None, NotImplemented),
|
||||
),
|
||||
)
|
||||
def test_comparison(self, comparison, other, expected):
|
||||
target = core.ID(id="foo")
|
||||
|
||||
@@ -384,14 +401,17 @@ class TestDocumentRange:
|
||||
|
||||
|
||||
class TestDefine:
|
||||
@pytest.mark.parametrize("name, value, prop, expected", (
|
||||
("ANSWER", None, "as_build_flag", "-DANSWER"),
|
||||
("ANSWER", None, "as_macro", "#define ANSWER"),
|
||||
("ANSWER", None, "as_tuple", ("ANSWER", None)),
|
||||
("ANSWER", 42, "as_build_flag", "-DANSWER=42"),
|
||||
("ANSWER", 42, "as_macro", "#define ANSWER 42"),
|
||||
("ANSWER", 42, "as_tuple", ("ANSWER", 42)),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"name, value, prop, expected",
|
||||
(
|
||||
("ANSWER", None, "as_build_flag", "-DANSWER"),
|
||||
("ANSWER", None, "as_macro", "#define ANSWER"),
|
||||
("ANSWER", None, "as_tuple", ("ANSWER", None)),
|
||||
("ANSWER", 42, "as_build_flag", "-DANSWER=42"),
|
||||
("ANSWER", 42, "as_macro", "#define ANSWER 42"),
|
||||
("ANSWER", 42, "as_tuple", ("ANSWER", 42)),
|
||||
),
|
||||
)
|
||||
def test_properties(self, name, value, prop, expected):
|
||||
target = core.Define(name, value)
|
||||
|
||||
@@ -399,18 +419,21 @@ class TestDefine:
|
||||
|
||||
assert actual == expected
|
||||
|
||||
@pytest.mark.parametrize("comparison, other, expected", (
|
||||
("__eq__", core.Define(name="FOO", value=42), True),
|
||||
("__eq__", core.Define(name="FOO", value=13), False),
|
||||
("__eq__", core.Define(name="FOO"), False),
|
||||
("__eq__", core.Define(name="BAR", value=42), False),
|
||||
("__eq__", core.Define(name="BAR"), False),
|
||||
("__eq__", 1000, NotImplemented),
|
||||
("__eq__", "1000", NotImplemented),
|
||||
("__eq__", True, NotImplemented),
|
||||
("__eq__", object(), NotImplemented),
|
||||
("__eq__", None, NotImplemented),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"comparison, other, expected",
|
||||
(
|
||||
("__eq__", core.Define(name="FOO", value=42), True),
|
||||
("__eq__", core.Define(name="FOO", value=13), False),
|
||||
("__eq__", core.Define(name="FOO"), False),
|
||||
("__eq__", core.Define(name="BAR", value=42), False),
|
||||
("__eq__", core.Define(name="BAR"), False),
|
||||
("__eq__", 1000, NotImplemented),
|
||||
("__eq__", "1000", NotImplemented),
|
||||
("__eq__", True, NotImplemented),
|
||||
("__eq__", object(), NotImplemented),
|
||||
("__eq__", None, NotImplemented),
|
||||
),
|
||||
)
|
||||
def test_comparison(self, comparison, other, expected):
|
||||
target = core.Define(name="FOO", value=42)
|
||||
|
||||
@@ -420,12 +443,15 @@ class TestDefine:
|
||||
|
||||
|
||||
class TestLibrary:
|
||||
@pytest.mark.parametrize("name, value, prop, expected", (
|
||||
("mylib", None, "as_lib_dep", "mylib"),
|
||||
("mylib", None, "as_tuple", ("mylib", None)),
|
||||
("mylib", "1.2.3", "as_lib_dep", "mylib@1.2.3"),
|
||||
("mylib", "1.2.3", "as_tuple", ("mylib", "1.2.3")),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"name, value, prop, expected",
|
||||
(
|
||||
("mylib", None, "as_lib_dep", "mylib"),
|
||||
("mylib", None, "as_tuple", ("mylib", None)),
|
||||
("mylib", "1.2.3", "as_lib_dep", "mylib@1.2.3"),
|
||||
("mylib", "1.2.3", "as_tuple", ("mylib", "1.2.3")),
|
||||
),
|
||||
)
|
||||
def test_properties(self, name, value, prop, expected):
|
||||
target = core.Library(name, value)
|
||||
|
||||
@@ -433,16 +459,19 @@ class TestLibrary:
|
||||
|
||||
assert actual == expected
|
||||
|
||||
@pytest.mark.parametrize("comparison, other, expected", (
|
||||
("__eq__", core.Library(name="libfoo", version="1.2.3"), True),
|
||||
("__eq__", core.Library(name="libfoo", version="1.2.4"), False),
|
||||
("__eq__", core.Library(name="libbar", version="1.2.3"), False),
|
||||
("__eq__", 1000, NotImplemented),
|
||||
("__eq__", "1000", NotImplemented),
|
||||
("__eq__", True, NotImplemented),
|
||||
("__eq__", object(), NotImplemented),
|
||||
("__eq__", None, NotImplemented),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"comparison, other, expected",
|
||||
(
|
||||
("__eq__", core.Library(name="libfoo", version="1.2.3"), True),
|
||||
("__eq__", core.Library(name="libfoo", version="1.2.4"), False),
|
||||
("__eq__", core.Library(name="libbar", version="1.2.3"), False),
|
||||
("__eq__", 1000, NotImplemented),
|
||||
("__eq__", "1000", NotImplemented),
|
||||
("__eq__", True, NotImplemented),
|
||||
("__eq__", object(), NotImplemented),
|
||||
("__eq__", None, NotImplemented),
|
||||
),
|
||||
)
|
||||
def test_comparison(self, comparison, other, expected):
|
||||
target = core.Library(name="libfoo", version="1.2.3")
|
||||
|
||||
|
Reference in New Issue
Block a user