From 49e634a62f6aa39cf4ccb30f3cc06cb7f7887659 Mon Sep 17 00:00:00 2001 From: David Bonnes Date: Fri, 4 Oct 2024 07:29:36 +0100 Subject: [PATCH] Convert evohome's test factory into an async generator (#126925) --- tests/components/evohome/conftest.py | 16 +++---- tests/components/evohome/test_init.py | 5 ++- tests/components/evohome/test_storage.py | 55 +++++++++++------------- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/tests/components/evohome/conftest.py b/tests/components/evohome/conftest.py index 6928451145f0df..b46c62f8651685 100644 --- a/tests/components/evohome/conftest.py +++ b/tests/components/evohome/conftest.py @@ -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 @@ -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] @@ -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 diff --git a/tests/components/evohome/test_init.py b/tests/components/evohome/test_init.py index 9c4558d0eb610c..b61efe9b0666f7 100644 --- a/tests/components/evohome/test_init.py +++ b/tests/components/evohome/test_init.py @@ -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 diff --git a/tests/components/evohome/test_storage.py b/tests/components/evohome/test_storage.py index a4608701273abc..33f6c6b3e6ce19 100644 --- a/tests/components/evohome/test_storage.py +++ b/tests/components/evohome/test_storage.py @@ -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"] @@ -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"] @@ -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"] @@ -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"]