Skip to content

Commit

Permalink
Convert evohome's test factory into an async generator (home-assistan…
Browse files Browse the repository at this point in the history
  • Loading branch information
zxdavb authored Oct 4, 2024
1 parent 8754b54 commit 49e634a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
16 changes: 8 additions & 8 deletions tests/components/evohome/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from collections.abc import Callable
from collections.abc import AsyncGenerator, Callable
from datetime import datetime, timedelta, timezone
from http import HTTPMethod
from typing import Any
Expand Down Expand Up @@ -112,16 +112,16 @@ def config() -> dict[str, str]:

async def setup_evohome(
hass: HomeAssistant,
test_config: dict[str, str],
config: dict[str, str],
install: str = "default",
) -> MagicMock:
) -> AsyncGenerator[MagicMock]:
"""Set up the evohome integration and return its client.
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
loc_idx: int = test_config.get("location_idx", 0) # type: ignore[assignment]
loc_idx: int = config.get("location_idx", 0) # type: ignore[assignment]

try:
locn = user_locations_config_fixture(install)[loc_idx]
Expand All @@ -140,16 +140,16 @@ async def setup_evohome(
):
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()

mock_client.assert_called_once()

assert mock_client.call_args.args[0] == test_config[CONF_USERNAME]
assert mock_client.call_args.args[1] == test_config[CONF_PASSWORD]
assert mock_client.call_args.args[0] == config[CONF_USERNAME]
assert mock_client.call_args.args[1] == config[CONF_PASSWORD]

assert isinstance(mock_client.call_args.kwargs["session"], ClientSession)

assert mock_client.account_info is not None

return mock_client
yield mock_client
5 changes: 3 additions & 2 deletions tests/components/evohome/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ async def test_entities(
"""Test entities and state after setup of a Honeywell TCC-compatible system."""

# 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
55 changes: 26 additions & 29 deletions tests/components/evohome/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ async def test_auth_tokens_null(

hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_NULL[idx]}

mock_client = await setup_evohome(hass, config, install=install)

# Confirm client was instantiated without tokens, as cache was empty...
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_EXPIRES not in mock_client.call_args.kwarg
async for mock_client in setup_evohome(hass, config, install=install):
# Confirm client was instantiated without tokens, as cache was empty...
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_EXPIRES not in mock_client.call_args.kwarg

# Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"]
Expand All @@ -128,14 +127,13 @@ async def test_auth_tokens_same(

hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_DATA[idx]}

mock_client = await setup_evohome(hass, config, install="minimal")

# 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_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN_EXPIRES] == dt_aware_to_naive(
ACCESS_TOKEN_EXP_DTM
)
async for mock_client in setup_evohome(hass, config, install=install):
# 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_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[
SZ_ACCESS_TOKEN_EXPIRES
] == dt_aware_to_naive(ACCESS_TOKEN_EXP_DTM)

# Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"]
Expand Down Expand Up @@ -165,14 +163,13 @@ async def test_auth_tokens_past(

hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": test_data}

mock_client = await setup_evohome(hass, config, install="minimal")

# 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_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[SZ_ACCESS_TOKEN_EXPIRES] == dt_aware_to_naive(
dt_dtm
)
async for mock_client in setup_evohome(hass, config, install=install):
# 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_ACCESS_TOKEN] == ACCESS_TOKEN
assert mock_client.call_args.kwargs[
SZ_ACCESS_TOKEN_EXPIRES
] == dt_aware_to_naive(dt_dtm)

# Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"]
Expand All @@ -199,13 +196,13 @@ async def test_auth_tokens_diff(

hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_STORAGE_DATA[idx]}

mock_client = await setup_evohome(
hass, config | {CONF_USERNAME: USERNAME_DIFF}, install="minimal"
)
# Confirm client was instantiated without tokens, as username was different...
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_EXPIRES not in mock_client.call_args.kwarg
async for mock_client in setup_evohome(
hass, config | {CONF_USERNAME: USERNAME_DIFF}, install=install
):
# Confirm client was instantiated without tokens, as username was different...
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_EXPIRES not in mock_client.call_args.kwarg

# Confirm the expected tokens were cached to storage...
data: _TokenStoreT = hass_storage[DOMAIN]["data"]
Expand Down

0 comments on commit 49e634a

Please sign in to comment.