Use a future for mock coro (#34989)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-04-30 16:31:00 -07:00
1 parent ba7391528f
commit 76f392476b
26 files changed
+154 -262

No files matched your search

+2 -3
View File
@@ -1,18 +1,17 @@
"""Tests for the cloud component."""
from unittest.mock import patch
from homeassistant.components import cloud
from homeassistant.components.cloud import const
from homeassistant.setup import async_setup_component
from tests.common import mock_coro
from tests.async_mock import AsyncMock, patch
async def mock_cloud(hass, config=None):
"""Mock cloud."""
assert await async_setup_component(hass, cloud.DOMAIN, {"cloud": config or {}})
cloud_inst = hass.data["cloud"]
with patch("hass_nabucasa.Cloud.run_executor", return_value=mock_coro()):
with patch("hass_nabucasa.Cloud.run_executor", AsyncMock(return_value=None)):
await cloud_inst.start()
+1 -1
View File
@@ -89,7 +89,7 @@ def patch_sync_helper():
to_update = []
to_remove = []
async def sync_helper(to_upd, to_rem):
def sync_helper(to_upd, to_rem):
to_update.extend([ent_id for ent_id in to_upd if ent_id not in to_update])
to_remove.extend([ent_id for ent_id in to_rem if ent_id not in to_remove])
return True
+3 -6
View File
@@ -1,6 +1,4 @@
"""Test the cloud.iot module."""
from unittest.mock import MagicMock, patch
from aiohttp import web
import pytest
@@ -12,8 +10,7 @@ from homeassistant.setup import async_setup_component
from . import mock_cloud, mock_cloud_prefs
from tests.async_mock import AsyncMock
from tests.common import mock_coro
from tests.async_mock import AsyncMock, MagicMock, patch
from tests.components.alexa import test_smart_home as test_alexa
@@ -131,7 +128,7 @@ async def test_handler_google_actions_disabled(hass, mock_cloud_fixture):
"""Test handler Google Actions when user has disabled it."""
mock_cloud_fixture._prefs[PREF_ENABLE_GOOGLE] = False
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
with patch("hass_nabucasa.Cloud.start"):
assert await async_setup_component(hass, "cloud", {})
reqid = "5711642932632160983"
@@ -146,7 +143,7 @@ async def test_handler_google_actions_disabled(hass, mock_cloud_fixture):
async def test_webhook_msg(hass):
"""Test webhook msg."""
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
with patch("hass_nabucasa.Cloud.start"):
setup = await async_setup_component(hass, "cloud", {"cloud": {}})
assert setup
cloud = hass.data["cloud"]
+11 -18
View File
@@ -1,5 +1,4 @@
"""Test the cloud component."""
from unittest.mock import patch
import pytest
@@ -11,12 +10,12 @@ from homeassistant.core import Context
from homeassistant.exceptions import Unauthorized
from homeassistant.setup import async_setup_component
from tests.common import mock_coro
from tests.async_mock import patch
async def test_constructor_loads_info_from_config(hass):
"""Test non-dev mode loads info from SERVERS constant."""
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
with patch("hass_nabucasa.Cloud.start"):
result = await async_setup_component(
hass,
"cloud",
@@ -63,17 +62,13 @@ async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user):
assert hass.services.has_service(DOMAIN, "remote_connect")
assert hass.services.has_service(DOMAIN, "remote_disconnect")
with patch(
"hass_nabucasa.remote.RemoteUI.connect", return_value=mock_coro()
) as mock_connect:
with patch("hass_nabucasa.remote.RemoteUI.connect") as mock_connect:
await hass.services.async_call(DOMAIN, "remote_connect", blocking=True)
assert mock_connect.called
assert cloud.client.remote_autostart
with patch(
"hass_nabucasa.remote.RemoteUI.disconnect", return_value=mock_coro()
) as mock_disconnect:
with patch("hass_nabucasa.remote.RemoteUI.disconnect") as mock_disconnect:
await hass.services.async_call(DOMAIN, "remote_disconnect", blocking=True)
assert mock_disconnect.called
@@ -82,9 +77,9 @@ async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user):
# Test admin access required
non_admin_context = Context(user_id=hass_read_only_user.id)
with patch(
"hass_nabucasa.remote.RemoteUI.connect", return_value=mock_coro()
) as mock_connect, pytest.raises(Unauthorized):
with patch("hass_nabucasa.remote.RemoteUI.connect") as mock_connect, pytest.raises(
Unauthorized
):
await hass.services.async_call(
DOMAIN, "remote_connect", blocking=True, context=non_admin_context
)
@@ -92,7 +87,7 @@ async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user):
assert mock_connect.called is False
with patch(
"hass_nabucasa.remote.RemoteUI.disconnect", return_value=mock_coro()
"hass_nabucasa.remote.RemoteUI.disconnect"
) as mock_disconnect, pytest.raises(Unauthorized):
await hass.services.async_call(
DOMAIN, "remote_disconnect", blocking=True, context=non_admin_context
@@ -103,7 +98,7 @@ async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user):
async def test_startup_shutdown_events(hass, mock_cloud_fixture):
"""Test if the cloud will start on startup event."""
with patch("hass_nabucasa.Cloud.stop", return_value=mock_coro()) as mock_stop:
with patch("hass_nabucasa.Cloud.stop") as mock_stop:
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
@@ -114,7 +109,7 @@ async def test_setup_existing_cloud_user(hass, hass_storage):
"""Test setup with API push default data."""
user = await hass.auth.async_create_system_user("Cloud test")
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"cloud_user": user.id}}
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
with patch("hass_nabucasa.Cloud.start"):
result = await async_setup_component(
hass,
"cloud",
@@ -148,9 +143,7 @@ async def test_on_connect(hass, mock_cloud_fixture):
assert len(hass.states.async_entity_ids("binary_sensor")) == 1
with patch(
"homeassistant.helpers.discovery.async_load_platform", side_effect=mock_coro
) as mock_load:
with patch("homeassistant.helpers.discovery.async_load_platform") as mock_load:
await cl.iot._on_connect[-1]()
await hass.async_block_till_done()