Convert evohome's test factory into an async generator (#126925)

This commit is contained in:
David Bonnes 2024-10-04 07:29:36 +01:00 committed by GitHub
parent 8754b54d81
commit 49e634a62f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 39 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable from collections.abc import AsyncGenerator, Callable
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from http import HTTPMethod from http import HTTPMethod
from typing import Any from typing import Any
@ -112,16 +112,16 @@ def config() -> dict[str, str]:
async def setup_evohome( async def setup_evohome(
hass: HomeAssistant, hass: HomeAssistant,
test_config: dict[str, str], config: dict[str, str],
install: str = "default", install: str = "default",
) -> MagicMock: ) -> AsyncGenerator[MagicMock]:
"""Set up the evohome integration and return its client. """Set up the evohome integration and return its client.
The class is mocked here to check the client was instantiated with the correct args. The class is mocked here to check the client was instantiated with the correct args.
""" """
# set the time zone as for the active evohome location # set the time zone as for the active evohome location
loc_idx: int = test_config.get("location_idx", 0) # type: ignore[assignment] loc_idx: int = config.get("location_idx", 0) # type: ignore[assignment]
try: try:
locn = user_locations_config_fixture(install)[loc_idx] locn = user_locations_config_fixture(install)[loc_idx]
@ -140,16 +140,16 @@ async def setup_evohome(
): ):
mock_client.side_effect = EvohomeClient mock_client.side_effect = EvohomeClient
assert await async_setup_component(hass, DOMAIN, {DOMAIN: test_config}) assert await async_setup_component(hass, DOMAIN, {DOMAIN: config})
await hass.async_block_till_done() await hass.async_block_till_done()
mock_client.assert_called_once() mock_client.assert_called_once()
assert mock_client.call_args.args[0] == test_config[CONF_USERNAME] assert mock_client.call_args.args[0] == config[CONF_USERNAME]
assert mock_client.call_args.args[1] == test_config[CONF_PASSWORD] assert mock_client.call_args.args[1] == config[CONF_PASSWORD]
assert isinstance(mock_client.call_args.kwargs["session"], ClientSession) assert isinstance(mock_client.call_args.kwargs["session"], ClientSession)
assert mock_client.account_info is not None assert mock_client.account_info is not None
return mock_client yield mock_client

View File

@ -23,8 +23,9 @@ async def test_entities(
"""Test entities and state after setup of a Honeywell TCC-compatible system.""" """Test entities and state after setup of a Honeywell TCC-compatible system."""
# some extended state attrs are relative the current time # some extended state attrs are relative the current time
freezer.move_to("2024-07-10 12:00:00+00:00") freezer.move_to("2024-07-10T12:00:00Z")
await setup_evohome(hass, config, install=install) async for _ in setup_evohome(hass, config, install=install):
pass
assert hass.states.async_all() == snapshot assert hass.states.async_all() == snapshot

View File

@ -96,8 +96,7 @@ async def test_auth_tokens_null(
hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_NULL[idx]} hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_NULL[idx]}
mock_client = await setup_evohome(hass, config, install=install) async for mock_client in setup_evohome(hass, config, install=install):
# Confirm client was instantiated without tokens, as cache was empty... # Confirm client was instantiated without tokens, as cache was empty...
assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN not in mock_client.call_args.kwargs assert SZ_ACCESS_TOKEN not in mock_client.call_args.kwargs
@ -128,14 +127,13 @@ async def test_auth_tokens_same(
hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_DATA[idx]} hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_DATA[idx]}
mock_client = await setup_evohome(hass, config, install="minimal") async for mock_client in setup_evohome(hass, config, install=install):
# Confirm client was instantiated with the cached tokens... # Confirm client was instantiated with the cached tokens...
assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN] == ACCESS_TOKEN assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN_EXPIRES] == dt_aware_to_naive( assert mock_client.call_args.kwargs[
ACCESS_TOKEN_EXP_DTM SZ_ACCESS_TOKEN_EXPIRES
) ] == dt_aware_to_naive(ACCESS_TOKEN_EXP_DTM)
# Confirm the expected tokens were cached to storage... # Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"] data: _TokenStoreT = hass_storage[DOMAIN]["data"]
@ -165,14 +163,13 @@ async def test_auth_tokens_past(
hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": test_data} hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": test_data}
mock_client = await setup_evohome(hass, config, install="minimal") async for mock_client in setup_evohome(hass, config, install=install):
# Confirm client was instantiated with the cached tokens... # Confirm client was instantiated with the cached tokens...
assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN] == ACCESS_TOKEN assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN_EXPIRES] == dt_aware_to_naive( assert mock_client.call_args.kwargs[
dt_dtm SZ_ACCESS_TOKEN_EXPIRES
) ] == dt_aware_to_naive(dt_dtm)
# Confirm the expected tokens were cached to storage... # Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"] data: _TokenStoreT = hass_storage[DOMAIN]["data"]
@ -199,9 +196,9 @@ async def test_auth_tokens_diff(
hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_DATA[idx]} hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_DATA[idx]}
mock_client = await setup_evohome( async for mock_client in setup_evohome(
hass, config | {CONF_USERNAME: USERNAME_DIFF}, install="minimal" hass, config | {CONF_USERNAME: USERNAME_DIFF}, install=install
) ):
# Confirm client was instantiated without tokens, as username was different... # Confirm client was instantiated without tokens, as username was different...
assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs
assert SZ_ACCESS_TOKEN not in mock_client.call_args.kwargs assert SZ_ACCESS_TOKEN not in mock_client.call_args.kwargs