Migrate websocket_api tests from coroutine to async/await (#30364)

This commit is contained in:
Franck Nijhof 2020-01-02 00:15:29 +01:00 committed by Andrew Sayre
parent 0fb5fbd85c
commit 4e7b35355d

View File

@ -1,5 +1,4 @@
"""Tests for the Home Assistant Websocket API.""" """Tests for the Home Assistant Websocket API."""
import asyncio
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from aiohttp import WSMsgType from aiohttp import WSMsgType
@ -16,12 +15,11 @@ def mock_low_queue():
yield yield
@asyncio.coroutine async def test_invalid_message_format(websocket_client):
def test_invalid_message_format(websocket_client):
"""Test sending invalid JSON.""" """Test sending invalid JSON."""
yield from websocket_client.send_json({"type": 5}) await websocket_client.send_json({"type": 5})
msg = yield from websocket_client.receive_json() msg = await websocket_client.receive_json()
assert msg["type"] == const.TYPE_RESULT assert msg["type"] == const.TYPE_RESULT
error = msg["error"] error = msg["error"]
@ -29,42 +27,38 @@ def test_invalid_message_format(websocket_client):
assert error["message"].startswith("Message incorrectly formatted") assert error["message"].startswith("Message incorrectly formatted")
@asyncio.coroutine async def test_invalid_json(websocket_client):
def test_invalid_json(websocket_client):
"""Test sending invalid JSON.""" """Test sending invalid JSON."""
yield from websocket_client.send_str("this is not JSON") await websocket_client.send_str("this is not JSON")
msg = yield from websocket_client.receive() msg = await websocket_client.receive()
assert msg.type == WSMsgType.close assert msg.type == WSMsgType.close
@asyncio.coroutine async def test_quiting_hass(hass, websocket_client):
def test_quiting_hass(hass, websocket_client):
"""Test sending invalid JSON.""" """Test sending invalid JSON."""
with patch.object(hass.loop, "stop"): with patch.object(hass.loop, "stop"):
yield from hass.async_stop() await hass.async_stop()
msg = yield from websocket_client.receive() msg = await websocket_client.receive()
assert msg.type == WSMsgType.CLOSE assert msg.type == WSMsgType.CLOSE
@asyncio.coroutine async def test_pending_msg_overflow(hass, mock_low_queue, websocket_client):
def test_pending_msg_overflow(hass, mock_low_queue, websocket_client):
"""Test get_panels command.""" """Test get_panels command."""
for idx in range(10): for idx in range(10):
yield from websocket_client.send_json({"id": idx + 1, "type": "ping"}) await websocket_client.send_json({"id": idx + 1, "type": "ping"})
msg = yield from websocket_client.receive() msg = await websocket_client.receive()
assert msg.type == WSMsgType.close assert msg.type == WSMsgType.close
@asyncio.coroutine async def test_unknown_command(websocket_client):
def test_unknown_command(websocket_client):
"""Test get_panels command.""" """Test get_panels command."""
yield from websocket_client.send_json({"id": 5, "type": "unknown_command"}) await websocket_client.send_json({"id": 5, "type": "unknown_command"})
msg = yield from websocket_client.receive_json() msg = await websocket_client.receive_json()
assert not msg["success"] assert not msg["success"]
assert msg["error"]["code"] == const.ERR_UNKNOWN_COMMAND assert msg["error"]["code"] == const.ERR_UNKNOWN_COMMAND