Collection of core tests improvements (#33757)

* Collection of core tests improvements

* Added some more

* Fix aiohttp client response release
This commit is contained in:
Franck Nijhof 2020-04-07 18:33:23 +02:00 committed by GitHub
parent 1f7803c541
commit 60bc517d01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 302 additions and 343 deletions

View file

@ -18,55 +18,57 @@ from tests.common import get_test_home_assistant, mock_registry
def test_generate_entity_id_requires_hass_or_ids():
"""Ensure we require at least hass or current ids."""
fmt = "test.{}"
with pytest.raises(ValueError):
entity.generate_entity_id(fmt, "hello world")
entity.generate_entity_id("test.{}", "hello world")
def test_generate_entity_id_given_keys():
"""Test generating an entity id given current ids."""
fmt = "test.{}"
assert (
entity.generate_entity_id(
fmt, "overwrite hidden true", current_ids=["test.overwrite_hidden_true"]
"test.{}",
"overwrite hidden true",
current_ids=["test.overwrite_hidden_true"],
)
== "test.overwrite_hidden_true_2"
)
assert (
entity.generate_entity_id(
fmt, "overwrite hidden true", current_ids=["test.another_entity"]
"test.{}", "overwrite hidden true", current_ids=["test.another_entity"]
)
== "test.overwrite_hidden_true"
)
def test_async_update_support(hass):
async def test_async_update_support(hass):
"""Test async update getting called."""
sync_update = []
async_update = []
class AsyncEntity(entity.Entity):
"""A test entity."""
entity_id = "sensor.test"
def update(self):
"""Update entity."""
sync_update.append([1])
ent = AsyncEntity()
ent.hass = hass
hass.loop.run_until_complete(ent.async_update_ha_state(True))
await ent.async_update_ha_state(True)
assert len(sync_update) == 1
assert len(async_update) == 0
@asyncio.coroutine
def async_update_func():
async def async_update_func():
"""Async update."""
async_update.append(1)
ent.async_update = async_update_func
hass.loop.run_until_complete(ent.async_update_ha_state(True))
await ent.async_update_ha_state(True)
assert len(sync_update) == 1
assert len(async_update) == 1
@ -123,13 +125,11 @@ class TestHelpersEntity:
assert state.attributes.get(ATTR_DEVICE_CLASS) == "test_class"
@asyncio.coroutine
def test_warn_slow_update(hass):
async def test_warn_slow_update(hass):
"""Warn we log when entity update takes a long time."""
update_call = False
@asyncio.coroutine
def async_update():
async def async_update():
"""Mock async update."""
nonlocal update_call
update_call = True
@ -140,7 +140,7 @@ def test_warn_slow_update(hass):
mock_entity.async_update = async_update
with patch.object(hass.loop, "call_later", MagicMock()) as mock_call:
yield from mock_entity.async_update_ha_state(True)
await mock_entity.async_update_ha_state(True)
assert mock_call.called
assert len(mock_call.mock_calls) == 2
@ -154,13 +154,11 @@ def test_warn_slow_update(hass):
assert update_call
@asyncio.coroutine
def test_warn_slow_update_with_exception(hass):
async def test_warn_slow_update_with_exception(hass):
"""Warn we log when entity update takes a long time and trow exception."""
update_call = False
@asyncio.coroutine
def async_update():
async def async_update():
"""Mock async update."""
nonlocal update_call
update_call = True
@ -172,7 +170,7 @@ def test_warn_slow_update_with_exception(hass):
mock_entity.async_update = async_update
with patch.object(hass.loop, "call_later", MagicMock()) as mock_call:
yield from mock_entity.async_update_ha_state(True)
await mock_entity.async_update_ha_state(True)
assert mock_call.called
assert len(mock_call.mock_calls) == 2
@ -186,13 +184,11 @@ def test_warn_slow_update_with_exception(hass):
assert update_call
@asyncio.coroutine
def test_warn_slow_device_update_disabled(hass):
async def test_warn_slow_device_update_disabled(hass):
"""Disable slow update warning with async_device_update."""
update_call = False
@asyncio.coroutine
def async_update():
async def async_update():
"""Mock async update."""
nonlocal update_call
update_call = True
@ -203,19 +199,17 @@ def test_warn_slow_device_update_disabled(hass):
mock_entity.async_update = async_update
with patch.object(hass.loop, "call_later", MagicMock()) as mock_call:
yield from mock_entity.async_device_update(warning=False)
await mock_entity.async_device_update(warning=False)
assert not mock_call.called
assert update_call
@asyncio.coroutine
def test_async_schedule_update_ha_state(hass):
async def test_async_schedule_update_ha_state(hass):
"""Warn we log when entity update takes a long time and trow exception."""
update_call = False
@asyncio.coroutine
def async_update():
async def async_update():
"""Mock async update."""
nonlocal update_call
update_call = True
@ -226,7 +220,7 @@ def test_async_schedule_update_ha_state(hass):
mock_entity.async_update = async_update
mock_entity.async_schedule_update_ha_state(True)
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert update_call is True
@ -236,6 +230,8 @@ async def test_async_async_request_call_without_lock(hass):
updates = []
class AsyncEntity(entity.Entity):
"""Test entity."""
def __init__(self, entity_id):
"""Initialize Async test entity."""
self.entity_id = entity_id
@ -271,6 +267,8 @@ async def test_async_async_request_call_with_lock(hass):
test_semaphore = asyncio.Semaphore(1)
class AsyncEntity(entity.Entity):
"""Test entity."""
def __init__(self, entity_id, lock):
"""Initialize Async test entity."""
self.entity_id = entity_id
@ -319,6 +317,8 @@ async def test_async_parallel_updates_with_zero(hass):
test_lock = asyncio.Event()
class AsyncEntity(entity.Entity):
"""Test entity."""
def __init__(self, entity_id, count):
"""Initialize Async test entity."""
self.entity_id = entity_id
@ -354,6 +354,8 @@ async def test_async_parallel_updates_with_zero_on_sync_update(hass):
test_lock = threading.Event()
class AsyncEntity(entity.Entity):
"""Test entity."""
def __init__(self, entity_id, count):
"""Initialize Async test entity."""
self.entity_id = entity_id
@ -393,6 +395,8 @@ async def test_async_parallel_updates_with_one(hass):
test_semaphore = asyncio.Semaphore(1)
class AsyncEntity(entity.Entity):
"""Test entity."""
def __init__(self, entity_id, count):
"""Initialize Async test entity."""
self.entity_id = entity_id
@ -467,6 +471,8 @@ async def test_async_parallel_updates_with_two(hass):
test_semaphore = asyncio.Semaphore(2)
class AsyncEntity(entity.Entity):
"""Test entity."""
def __init__(self, entity_id, count):
"""Initialize Async test entity."""
self.entity_id = entity_id
@ -474,11 +480,10 @@ async def test_async_parallel_updates_with_two(hass):
self._count = count
self.parallel_updates = test_semaphore
@asyncio.coroutine
def async_update(self):
async def async_update(self):
"""Test update."""
updates.append(self._count)
yield from test_lock.acquire()
await test_lock.acquire()
ent_1 = AsyncEntity("sensor.test_1", 1)
ent_2 = AsyncEntity("sensor.test_2", 2)
@ -529,15 +534,14 @@ async def test_async_parallel_updates_with_two(hass):
test_lock.release()
@asyncio.coroutine
def test_async_remove_no_platform(hass):
async def test_async_remove_no_platform(hass):
"""Test async_remove method when no platform set."""
ent = entity.Entity()
ent.hass = hass
ent.entity_id = "test.test"
yield from ent.async_update_ha_state()
await ent.async_update_ha_state()
assert len(hass.states.async_entity_ids()) == 1
yield from ent.async_remove()
await ent.async_remove()
assert len(hass.states.async_entity_ids()) == 0
@ -686,6 +690,8 @@ async def test_warn_slow_write_state_custom_component(hass, caplog):
"""Check that we log a warning if reading properties takes too long."""
class CustomComponentEntity(entity.Entity):
"""Custom component entity."""
__module__ = "custom_components.bla.sensor"
mock_entity = CustomComponentEntity()