Migrate automatic tests from coroutine to async/await (#30371)

* Migrate automatic tests from coroutine to async/await

* Remove unneeded side effect

* Replace unittest with asynctest, add additional asserts to tests
This commit is contained in:
Franck Nijhof 2020-01-05 14:23:22 +01:00 committed by Andrew Sayre
parent 5216477353
commit 0a9ac91dec

View File

@ -1,10 +1,9 @@
"""Test the automatic device tracker platform.""" """Test the automatic device tracker platform."""
import asyncio
from datetime import datetime from datetime import datetime
import logging import logging
from unittest.mock import MagicMock, patch
import aioautomatic import aioautomatic
from asynctest import MagicMock, patch
from homeassistant.components.automatic.device_tracker import async_setup_scanner from homeassistant.components.automatic.device_tracker import async_setup_scanner
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -31,8 +30,7 @@ def test_invalid_credentials(
hass.loop.run_until_complete(async_setup_component(hass, "http", {})) hass.loop.run_until_complete(async_setup_component(hass, "http", {}))
mock_json_load.return_value = {"refresh_token": "bad_token"} mock_json_load.return_value = {"refresh_token": "bad_token"}
@asyncio.coroutine async def get_session(*args, **kwargs):
def get_session(*args, **kwargs):
"""Return the test session.""" """Return the test session."""
raise aioautomatic.exceptions.BadRequestError("err_invalid_refresh_token") raise aioautomatic.exceptions.BadRequestError("err_invalid_refresh_token")
@ -87,18 +85,15 @@ def test_valid_credentials(
trip.end_location.accuracy_m = 5.6 trip.end_location.accuracy_m = 5.6
trip.ended_at = datetime(2017, 8, 13, 1, 2, 4) trip.ended_at = datetime(2017, 8, 13, 1, 2, 4)
@asyncio.coroutine async def get_session(*args, **kwargs):
def get_session(*args, **kwargs):
"""Return the test session.""" """Return the test session."""
return session return session
@asyncio.coroutine async def get_vehicles(*args, **kwargs):
def get_vehicles(*args, **kwargs):
"""Return list of test vehicles.""" """Return list of test vehicles."""
return [vehicle] return [vehicle]
@asyncio.coroutine async def get_trips(*args, **kwargs):
def get_trips(*args, **kwargs):
"""Return list of test trips.""" """Return list of test trips."""
return [trip] return [trip]
@ -108,12 +103,6 @@ def test_valid_credentials(
session.get_trips.side_effect = get_trips session.get_trips.side_effect = get_trips
session.refresh_token = "mock_refresh_token" session.refresh_token = "mock_refresh_token"
@asyncio.coroutine
def ws_connect():
return asyncio.Future()
mock_ws_connect.side_effect = ws_connect
config = { config = {
"platform": "automatic", "platform": "automatic",
"username": "good_username", "username": "good_username",
@ -126,6 +115,9 @@ def test_valid_credentials(
assert result assert result
assert mock_ws_connect.called
assert len(mock_ws_connect.mock_calls) == 2
assert mock_create_session.called assert mock_create_session.called
assert len(mock_create_session.mock_calls) == 1 assert len(mock_create_session.mock_calls) == 1
assert mock_create_session.mock_calls[0][1][0] == "good_token" assert mock_create_session.mock_calls[0][1][0] == "good_token"