From d5a68ad31192a79bd51c244bf9358ba42fc24851 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 7 Jun 2024 10:25:11 +0200 Subject: [PATCH] Improve type hints in zamg tests (#119042) --- tests/components/zamg/conftest.py | 23 ++++-------------- tests/components/zamg/test_config_flow.py | 29 +++++++---------------- tests/components/zamg/test_init.py | 8 +++---- 3 files changed, 16 insertions(+), 44 deletions(-) diff --git a/tests/components/zamg/conftest.py b/tests/components/zamg/conftest.py index fa2eccf42d1..1795baa7fad 100644 --- a/tests/components/zamg/conftest.py +++ b/tests/components/zamg/conftest.py @@ -37,7 +37,7 @@ def mock_setup_entry() -> Generator[None]: @pytest.fixture -def mock_zamg_config_flow(request: pytest.FixtureRequest) -> Generator[MagicMock]: +def mock_zamg_config_flow() -> Generator[MagicMock]: """Return a mocked Zamg client.""" with patch( "homeassistant.components.zamg.sensor.ZamgData", autospec=True @@ -51,7 +51,7 @@ def mock_zamg_config_flow(request: pytest.FixtureRequest) -> Generator[MagicMock @pytest.fixture -def mock_zamg(request: pytest.FixtureRequest) -> Generator[MagicMock]: +def mock_zamg() -> Generator[MagicMock]: """Return a mocked Zamg client.""" with patch( @@ -70,7 +70,7 @@ def mock_zamg(request: pytest.FixtureRequest) -> Generator[MagicMock]: @pytest.fixture -def mock_zamg_coordinator(request: pytest.FixtureRequest) -> Generator[MagicMock]: +def mock_zamg_coordinator() -> Generator[MagicMock]: """Return a mocked Zamg client.""" with patch( @@ -89,22 +89,7 @@ def mock_zamg_coordinator(request: pytest.FixtureRequest) -> Generator[MagicMock @pytest.fixture -def mock_zamg_stations(request: pytest.FixtureRequest) -> Generator[MagicMock]: - """Return a mocked Zamg client.""" - with patch( - "homeassistant.components.zamg.config_flow.ZamgData.zamg_stations" - ) as zamg_mock: - zamg_mock.return_value = { - "11240": (46.99305556, 15.43916667, "GRAZ-FLUGHAFEN"), - "11244": (46.87222222, 15.90361111, "BAD GLEICHENBERG"), - } - yield zamg_mock - - -@pytest.fixture -async def init_integration( - hass: HomeAssistant, -) -> MockConfigEntry: +async def init_integration(hass: HomeAssistant) -> MockConfigEntry: """Set up the Zamg integration for testing.""" mock_config_entry.add_to_hass(hass) diff --git a/tests/components/zamg/test_config_flow.py b/tests/components/zamg/test_config_flow.py index f67eda67a49..949f14df89c 100644 --- a/tests/components/zamg/test_config_flow.py +++ b/tests/components/zamg/test_config_flow.py @@ -2,6 +2,7 @@ from unittest.mock import MagicMock +import pytest from zamg.exceptions import ZamgApiError from homeassistant.components.zamg.const import CONF_STATION_ID, DOMAIN, LOGGER @@ -12,11 +13,8 @@ from homeassistant.data_entry_flow import FlowResultType from .conftest import TEST_STATION_ID -async def test_full_user_flow_implementation( - hass: HomeAssistant, - mock_zamg: MagicMock, - mock_setup_entry: None, -) -> None: +@pytest.mark.usefixtures("mock_zamg", "mock_setup_entry") +async def test_full_user_flow_implementation(hass: HomeAssistant) -> None: """Test the full manual user flow from start to finish.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -37,11 +35,8 @@ async def test_full_user_flow_implementation( assert result["result"].unique_id == TEST_STATION_ID -async def test_error_closest_station( - hass: HomeAssistant, - mock_zamg: MagicMock, - mock_setup_entry: None, -) -> None: +@pytest.mark.usefixtures("mock_setup_entry") +async def test_error_closest_station(hass: HomeAssistant, mock_zamg: MagicMock) -> None: """Test with error of reading from Zamg.""" mock_zamg.closest_station.side_effect = ZamgApiError result = await hass.config_entries.flow.async_init( @@ -52,11 +47,8 @@ async def test_error_closest_station( assert result.get("reason") == "cannot_connect" -async def test_error_update( - hass: HomeAssistant, - mock_zamg: MagicMock, - mock_setup_entry: None, -) -> None: +@pytest.mark.usefixtures("mock_setup_entry") +async def test_error_update(hass: HomeAssistant, mock_zamg: MagicMock) -> None: """Test with error of reading from Zamg.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -75,11 +67,8 @@ async def test_error_update( assert result.get("reason") == "cannot_connect" -async def test_user_flow_duplicate( - hass: HomeAssistant, - mock_zamg: MagicMock, - mock_setup_entry: None, -) -> None: +@pytest.mark.usefixtures("mock_zamg", "mock_setup_entry") +async def test_user_flow_duplicate(hass: HomeAssistant) -> None: """Test the full manual user flow from start to finish.""" result = await hass.config_entries.flow.async_init( DOMAIN, diff --git a/tests/components/zamg/test_init.py b/tests/components/zamg/test_init.py index eec7dcef101..9f05882853a 100644 --- a/tests/components/zamg/test_init.py +++ b/tests/components/zamg/test_init.py @@ -1,7 +1,5 @@ """Test Zamg component init.""" -from unittest.mock import MagicMock - import pytest from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN @@ -62,10 +60,10 @@ from tests.common import MockConfigEntry ), ], ) +@pytest.mark.usefixtures("mock_zamg_coordinator") async def test_migrate_unique_ids( hass: HomeAssistant, entity_registry: er.EntityRegistry, - mock_zamg_coordinator: MagicMock, entitydata: dict, old_unique_id: str, new_unique_id: str, @@ -108,10 +106,10 @@ async def test_migrate_unique_ids( ), ], ) +@pytest.mark.usefixtures("mock_zamg_coordinator") async def test_dont_migrate_unique_ids( hass: HomeAssistant, entity_registry: er.EntityRegistry, - mock_zamg_coordinator: MagicMock, entitydata: dict, old_unique_id: str, new_unique_id: str, @@ -167,10 +165,10 @@ async def test_dont_migrate_unique_ids( ), ], ) +@pytest.mark.usefixtures("mock_zamg_coordinator") async def test_unload_entry( hass: HomeAssistant, entity_registry: er.EntityRegistry, - mock_zamg_coordinator: MagicMock, entitydata: dict, unique_id: str, ) -> None: