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