This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+41 -33
View File
@@ -10,44 +10,47 @@ import homeassistant.util.dt as dt_util
def test_sanitize_filename():
"""Test sanitize_filename."""
assert util.sanitize_filename("test") == 'test'
assert util.sanitize_filename("/test") == 'test'
assert util.sanitize_filename("..test") == 'test'
assert util.sanitize_filename("\\test") == 'test'
assert util.sanitize_filename("\\../test") == 'test'
assert util.sanitize_filename("test") == "test"
assert util.sanitize_filename("/test") == "test"
assert util.sanitize_filename("..test") == "test"
assert util.sanitize_filename("\\test") == "test"
assert util.sanitize_filename("\\../test") == "test"
def test_sanitize_path():
"""Test sanitize_path."""
assert util.sanitize_path("test/path") == 'test/path'
assert util.sanitize_path("~test/path") == 'test/path'
assert util.sanitize_path("~/../test/path") == '//test/path'
assert util.sanitize_path("test/path") == "test/path"
assert util.sanitize_path("~test/path") == "test/path"
assert util.sanitize_path("~/../test/path") == "//test/path"
def test_slugify():
"""Test slugify."""
assert util.slugify("T-!@#$!#@$!$est") == 't_est'
assert util.slugify("Test More") == 'test_more'
assert util.slugify("Test_(More)") == 'test_more'
assert util.slugify("Tèst_Mörê") == 'test_more'
assert util.slugify("B8:27:EB:00:00:00") == 'b8_27_eb_00_00_00'
assert util.slugify("test.com") == 'test_com'
assert util.slugify("greg_phone - exp_wayp1") == 'greg_phone_exp_wayp1'
assert util.slugify("We are, we are, a... Test Calendar") == \
'we_are_we_are_a_test_calendar'
assert util.slugify("Tèst_äöüß_ÄÖÜ") == 'test_aouss_aou'
assert util.slugify("影師嗎") == 'ying_shi_ma'
assert util.slugify("けいふぉんと") == 'keihuonto'
assert util.slugify("T-!@#$!#@$!$est") == "t_est"
assert util.slugify("Test More") == "test_more"
assert util.slugify("Test_(More)") == "test_more"
assert util.slugify("Tèst_Mörê") == "test_more"
assert util.slugify("B8:27:EB:00:00:00") == "b8_27_eb_00_00_00"
assert util.slugify("test.com") == "test_com"
assert util.slugify("greg_phone - exp_wayp1") == "greg_phone_exp_wayp1"
assert (
util.slugify("We are, we are, a... Test Calendar")
== "we_are_we_are_a_test_calendar"
)
assert util.slugify("Tèst_äöüß_ÄÖÜ") == "test_aouss_aou"
assert util.slugify("影師嗎") == "ying_shi_ma"
assert util.slugify("けいふぉんと") == "keihuonto"
def test_repr_helper():
"""Test repr_helper."""
assert util.repr_helper("A") == 'A'
assert util.repr_helper(5) == '5'
assert util.repr_helper(True) == 'True'
assert util.repr_helper({"test": 1}) == 'test=1'
assert util.repr_helper(datetime(1986, 7, 9, 12, 0, 0)) == \
'1986-07-09T12:00:00+00:00'
assert util.repr_helper("A") == "A"
assert util.repr_helper(5) == "5"
assert util.repr_helper(True) == "True"
assert util.repr_helper({"test": 1}) == "test=1"
assert (
util.repr_helper(datetime(1986, 7, 9, 12, 0, 0)) == "1986-07-09T12:00:00+00:00"
)
def test_convert():
@@ -62,12 +65,13 @@ def test_convert():
def test_ensure_unique_string():
"""Test ensure_unique_string."""
assert util.ensure_unique_string("Beer", ["Beer", "Beer_2"]) == 'Beer_3'
assert util.ensure_unique_string("Beer", ["Wine", "Soda"]) == 'Beer'
assert util.ensure_unique_string("Beer", ["Beer", "Beer_2"]) == "Beer_3"
assert util.ensure_unique_string("Beer", ["Wine", "Soda"]) == "Beer"
def test_ordered_enum():
"""Test the ordered enum class."""
class TestEnum(util.OrderedEnum):
"""Test enum that can be ordered."""
@@ -144,14 +148,14 @@ def test_throttle():
assert len(calls1) == 2
assert len(calls2) == 1
with patch('homeassistant.util.utcnow', return_value=plus3):
with patch("homeassistant.util.utcnow", return_value=plus3):
test_throttle1()
test_throttle2()
assert len(calls1) == 2
assert len(calls2) == 1
with patch('homeassistant.util.utcnow', return_value=plus5):
with patch("homeassistant.util.utcnow", return_value=plus5):
test_throttle1()
test_throttle2()
@@ -161,6 +165,7 @@ def test_throttle():
def test_throttle_per_instance():
"""Test that the throttle method is done per instance of a class."""
class Tester:
"""A tester class for the throttle."""
@@ -175,6 +180,7 @@ def test_throttle_per_instance():
def test_throttle_on_method():
"""Test that throttle works when wrapping a method."""
class Tester:
"""A tester class for the throttle."""
@@ -191,6 +197,7 @@ def test_throttle_on_method():
def test_throttle_on_two_method():
"""Test that throttle works when wrapping two methods."""
class Tester:
"""A test class for the throttle."""
@@ -210,10 +217,10 @@ def test_throttle_on_two_method():
assert tester.goodbye()
@patch.object(util, 'random')
@patch.object(util, "random")
def test_get_random_string(mock_random):
"""Test get random string."""
results = ['A', 'B', 'C']
results = ["A", "B", "C"]
def mock_choice(choices):
return results.pop(0)
@@ -222,11 +229,12 @@ def test_get_random_string(mock_random):
generator.choice.side_effect = mock_choice
mock_random.SystemRandom.return_value = generator
assert util.get_random_string(length=3) == 'ABC'
assert util.get_random_string(length=3) == "ABC"
async def test_throttle_async():
"""Test Throttle decorator with async method."""
@util.Throttle(timedelta(seconds=2))
async def test_method():
"""Only first call should return a value."""