Migrate google_* tests from coroutine to async/await (#30377)

This commit is contained in:
Franck Nijhof 2020-01-02 00:24:30 +01:00 committed by Andrew Sayre
parent bcb47dab45
commit 37d1771400
3 changed files with 23 additions and 35 deletions

View File

@ -1,6 +1,5 @@
"""The tests for the Google Assistant component.""" """The tests for the Google Assistant component."""
# pylint: disable=protected-access # pylint: disable=protected-access
import asyncio
import json import json
from aiohttp.hdrs import AUTHORIZATION from aiohttp.hdrs import AUTHORIZATION
@ -115,18 +114,17 @@ def hass_fixture(loop, hass):
# pylint: disable=redefined-outer-name # pylint: disable=redefined-outer-name
@asyncio.coroutine async def test_sync_request(hass_fixture, assistant_client, auth_header):
def test_sync_request(hass_fixture, assistant_client, auth_header):
"""Test a sync request.""" """Test a sync request."""
reqid = "5711642932632160983" reqid = "5711642932632160983"
data = {"requestId": reqid, "inputs": [{"intent": "action.devices.SYNC"}]} data = {"requestId": reqid, "inputs": [{"intent": "action.devices.SYNC"}]}
result = yield from assistant_client.post( result = await assistant_client.post(
ga.const.GOOGLE_ASSISTANT_API_ENDPOINT, ga.const.GOOGLE_ASSISTANT_API_ENDPOINT,
data=json.dumps(data), data=json.dumps(data),
headers=auth_header, headers=auth_header,
) )
assert result.status == 200 assert result.status == 200
body = yield from result.json() body = await result.json()
assert body.get("requestId") == reqid assert body.get("requestId") == reqid
devices = body["payload"]["devices"] devices = body["payload"]["devices"]
assert sorted([dev["id"] for dev in devices]) == sorted( assert sorted([dev["id"] for dev in devices]) == sorted(
@ -145,8 +143,7 @@ def test_sync_request(hass_fixture, assistant_client, auth_header):
assert dev["type"] == demo["type"] assert dev["type"] == demo["type"]
@asyncio.coroutine async def test_query_request(hass_fixture, assistant_client, auth_header):
def test_query_request(hass_fixture, assistant_client, auth_header):
"""Test a query request.""" """Test a query request."""
reqid = "5711642932632160984" reqid = "5711642932632160984"
data = { data = {
@ -165,13 +162,13 @@ def test_query_request(hass_fixture, assistant_client, auth_header):
} }
], ],
} }
result = yield from assistant_client.post( result = await assistant_client.post(
ga.const.GOOGLE_ASSISTANT_API_ENDPOINT, ga.const.GOOGLE_ASSISTANT_API_ENDPOINT,
data=json.dumps(data), data=json.dumps(data),
headers=auth_header, headers=auth_header,
) )
assert result.status == 200 assert result.status == 200
body = yield from result.json() body = await result.json()
assert body.get("requestId") == reqid assert body.get("requestId") == reqid
devices = body["payload"]["devices"] devices = body["payload"]["devices"]
assert len(devices) == 4 assert len(devices) == 4
@ -187,8 +184,7 @@ def test_query_request(hass_fixture, assistant_client, auth_header):
assert devices["media_player.lounge_room"]["on"] is True assert devices["media_player.lounge_room"]["on"] is True
@asyncio.coroutine async def test_query_climate_request(hass_fixture, assistant_client, auth_header):
def test_query_climate_request(hass_fixture, assistant_client, auth_header):
"""Test a query request.""" """Test a query request."""
reqid = "5711642932632160984" reqid = "5711642932632160984"
data = { data = {
@ -206,13 +202,13 @@ def test_query_climate_request(hass_fixture, assistant_client, auth_header):
} }
], ],
} }
result = yield from assistant_client.post( result = await assistant_client.post(
ga.const.GOOGLE_ASSISTANT_API_ENDPOINT, ga.const.GOOGLE_ASSISTANT_API_ENDPOINT,
data=json.dumps(data), data=json.dumps(data),
headers=auth_header, headers=auth_header,
) )
assert result.status == 200 assert result.status == 200
body = yield from result.json() body = await result.json()
assert body.get("requestId") == reqid assert body.get("requestId") == reqid
devices = body["payload"]["devices"] devices = body["payload"]["devices"]
assert len(devices) == 3 assert len(devices) == 3
@ -238,8 +234,7 @@ def test_query_climate_request(hass_fixture, assistant_client, auth_header):
} }
@asyncio.coroutine async def test_query_climate_request_f(hass_fixture, assistant_client, auth_header):
def test_query_climate_request_f(hass_fixture, assistant_client, auth_header):
"""Test a query request.""" """Test a query request."""
# Mock demo devices as fahrenheit to see if we convert to celsius # Mock demo devices as fahrenheit to see if we convert to celsius
hass_fixture.config.units.temperature_unit = const.TEMP_FAHRENHEIT hass_fixture.config.units.temperature_unit = const.TEMP_FAHRENHEIT
@ -264,13 +259,13 @@ def test_query_climate_request_f(hass_fixture, assistant_client, auth_header):
} }
], ],
} }
result = yield from assistant_client.post( result = await assistant_client.post(
ga.const.GOOGLE_ASSISTANT_API_ENDPOINT, ga.const.GOOGLE_ASSISTANT_API_ENDPOINT,
data=json.dumps(data), data=json.dumps(data),
headers=auth_header, headers=auth_header,
) )
assert result.status == 200 assert result.status == 200
body = yield from result.json() body = await result.json()
assert body.get("requestId") == reqid assert body.get("requestId") == reqid
devices = body["payload"]["devices"] devices = body["payload"]["devices"]
assert len(devices) == 3 assert len(devices) == 3
@ -297,8 +292,7 @@ def test_query_climate_request_f(hass_fixture, assistant_client, auth_header):
hass_fixture.config.units.temperature_unit = const.TEMP_CELSIUS hass_fixture.config.units.temperature_unit = const.TEMP_CELSIUS
@asyncio.coroutine async def test_execute_request(hass_fixture, assistant_client, auth_header):
def test_execute_request(hass_fixture, assistant_client, auth_header):
"""Test an execute request.""" """Test an execute request."""
reqid = "5711642932632160985" reqid = "5711642932632160985"
data = { data = {
@ -357,13 +351,13 @@ def test_execute_request(hass_fixture, assistant_client, auth_header):
} }
], ],
} }
result = yield from assistant_client.post( result = await assistant_client.post(
ga.const.GOOGLE_ASSISTANT_API_ENDPOINT, ga.const.GOOGLE_ASSISTANT_API_ENDPOINT,
data=json.dumps(data), data=json.dumps(data),
headers=auth_header, headers=auth_header,
) )
assert result.status == 200 assert result.status == 200
body = yield from result.json() body = await result.json()
assert body.get("requestId") == reqid assert body.get("requestId") == reqid
commands = body["payload"]["commands"] commands = body["payload"]["commands"]
assert len(commands) == 6 assert len(commands) == 6

View File

@ -1,6 +1,4 @@
"""The tests for google-assistant init.""" """The tests for google-assistant init."""
import asyncio
from homeassistant.components import google_assistant as ga from homeassistant.components import google_assistant as ga
from homeassistant.core import Context from homeassistant.core import Context
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -8,19 +6,18 @@ from homeassistant.setup import async_setup_component
GA_API_KEY = "Agdgjsj399sdfkosd932ksd" GA_API_KEY = "Agdgjsj399sdfkosd932ksd"
@asyncio.coroutine async def test_request_sync_service(aioclient_mock, hass):
def test_request_sync_service(aioclient_mock, hass):
"""Test that it posts to the request_sync url.""" """Test that it posts to the request_sync url."""
aioclient_mock.post(ga.const.REQUEST_SYNC_BASE_URL, status=200) aioclient_mock.post(ga.const.REQUEST_SYNC_BASE_URL, status=200)
yield from async_setup_component( await async_setup_component(
hass, hass,
"google_assistant", "google_assistant",
{"google_assistant": {"project_id": "test_project", "api_key": GA_API_KEY}}, {"google_assistant": {"project_id": "test_project", "api_key": GA_API_KEY}},
) )
assert aioclient_mock.call_count == 0 assert aioclient_mock.call_count == 0
yield from hass.services.async_call( await hass.services.async_call(
ga.const.DOMAIN, ga.const.DOMAIN,
ga.const.SERVICE_REQUEST_SYNC, ga.const.SERVICE_REQUEST_SYNC,
blocking=True, blocking=True,

View File

@ -1,5 +1,4 @@
"""Test the Google Domains component.""" """Test the Google Domains component."""
import asyncio
from datetime import timedelta from datetime import timedelta
import pytest import pytest
@ -37,12 +36,11 @@ def setup_google_domains(hass, aioclient_mock):
) )
@asyncio.coroutine async def test_setup(hass, aioclient_mock):
def test_setup(hass, aioclient_mock):
"""Test setup works if update passes.""" """Test setup works if update passes."""
aioclient_mock.get(UPDATE_URL, params={"hostname": DOMAIN}, text="nochg 0.0.0.0") aioclient_mock.get(UPDATE_URL, params={"hostname": DOMAIN}, text="nochg 0.0.0.0")
result = yield from async_setup_component( result = await async_setup_component(
hass, hass,
google_domains.DOMAIN, google_domains.DOMAIN,
{ {
@ -57,16 +55,15 @@ def test_setup(hass, aioclient_mock):
assert aioclient_mock.call_count == 1 assert aioclient_mock.call_count == 1
async_fire_time_changed(hass, utcnow() + timedelta(minutes=5)) async_fire_time_changed(hass, utcnow() + timedelta(minutes=5))
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert aioclient_mock.call_count == 2 assert aioclient_mock.call_count == 2
@asyncio.coroutine async def test_setup_fails_if_update_fails(hass, aioclient_mock):
def test_setup_fails_if_update_fails(hass, aioclient_mock):
"""Test setup fails if first update fails.""" """Test setup fails if first update fails."""
aioclient_mock.get(UPDATE_URL, params={"hostname": DOMAIN}, text="nohost") aioclient_mock.get(UPDATE_URL, params={"hostname": DOMAIN}, text="nohost")
result = yield from async_setup_component( result = await async_setup_component(
hass, hass,
google_domains.DOMAIN, google_domains.DOMAIN,
{ {