Improve type hints in airnow tests (#119038)

This commit is contained in:
epenet 2024-06-07 09:03:35 +02:00 committed by GitHub
parent 274cd41d57
commit 8628a1e449
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 20 deletions

View File

@ -1,18 +1,23 @@
"""Define fixtures for AirNow tests.""" """Define fixtures for AirNow tests."""
import json from typing import Any
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
import pytest import pytest
from typing_extensions import Generator
from homeassistant.components.airnow import DOMAIN from homeassistant.components.airnow import DOMAIN
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS
from homeassistant.core import HomeAssistant
from homeassistant.util.json import JsonArrayType
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, load_json_array_fixture
@pytest.fixture(name="config_entry") @pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config, options): def config_entry_fixture(
hass: HomeAssistant, config: dict[str, Any], options: dict[str, Any]
) -> MockConfigEntry:
"""Define a config entry fixture.""" """Define a config entry fixture."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
@ -27,7 +32,7 @@ def config_entry_fixture(hass, config, options):
@pytest.fixture(name="config") @pytest.fixture(name="config")
def config_fixture(hass): def config_fixture() -> dict[str, Any]:
"""Define a config entry data fixture.""" """Define a config entry data fixture."""
return { return {
CONF_API_KEY: "abc123", CONF_API_KEY: "abc123",
@ -37,7 +42,7 @@ def config_fixture(hass):
@pytest.fixture(name="options") @pytest.fixture(name="options")
def options_fixture(hass): def options_fixture() -> dict[str, Any]:
"""Define a config options data fixture.""" """Define a config options data fixture."""
return { return {
CONF_RADIUS: 150, CONF_RADIUS: 150,
@ -45,19 +50,19 @@ def options_fixture(hass):
@pytest.fixture(name="data", scope="package") @pytest.fixture(name="data", scope="package")
def data_fixture(): def data_fixture() -> JsonArrayType:
"""Define a fixture for response data.""" """Define a fixture for response data."""
return json.loads(load_fixture("response.json", "airnow")) return load_json_array_fixture("response.json", "airnow")
@pytest.fixture(name="mock_api_get") @pytest.fixture(name="mock_api_get")
def mock_api_get_fixture(data): def mock_api_get_fixture(data: JsonArrayType) -> AsyncMock:
"""Define a fixture for a mock "get" coroutine function.""" """Define a fixture for a mock "get" coroutine function."""
return AsyncMock(return_value=data) return AsyncMock(return_value=data)
@pytest.fixture(name="setup_airnow") @pytest.fixture(name="setup_airnow")
async def setup_airnow_fixture(hass, config, mock_api_get): def setup_airnow_fixture(mock_api_get: AsyncMock) -> Generator[None]:
"""Define a fixture to set up AirNow.""" """Define a fixture to set up AirNow."""
with ( with (
patch("pyairnow.WebServiceAPI._get", mock_api_get), patch("pyairnow.WebServiceAPI._get", mock_api_get),

View File

@ -1,5 +1,6 @@
"""Test the AirNow config flow.""" """Test the AirNow config flow."""
from typing import Any
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from pyairnow.errors import AirNowError, EmptyResponseError, InvalidKeyError from pyairnow.errors import AirNowError, EmptyResponseError, InvalidKeyError
@ -14,7 +15,10 @@ from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_form(hass: HomeAssistant, config, options, setup_airnow) -> None: @pytest.mark.usefixtures("setup_airnow")
async def test_form(
hass: HomeAssistant, config: dict[str, Any], options: dict[str, Any]
) -> None:
"""Test we get the form.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -29,7 +33,8 @@ async def test_form(hass: HomeAssistant, config, options, setup_airnow) -> None:
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=InvalidKeyError)]) @pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=InvalidKeyError)])
async def test_form_invalid_auth(hass: HomeAssistant, config, setup_airnow) -> None: @pytest.mark.usefixtures("setup_airnow")
async def test_form_invalid_auth(hass: HomeAssistant, config: dict[str, Any]) -> None:
"""Test we handle invalid auth.""" """Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -40,7 +45,10 @@ async def test_form_invalid_auth(hass: HomeAssistant, config, setup_airnow) -> N
@pytest.mark.parametrize("data", [{}]) @pytest.mark.parametrize("data", [{}])
async def test_form_invalid_location(hass: HomeAssistant, config, setup_airnow) -> None: @pytest.mark.usefixtures("setup_airnow")
async def test_form_invalid_location(
hass: HomeAssistant, config: dict[str, Any]
) -> None:
"""Test we handle invalid location.""" """Test we handle invalid location."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -51,7 +59,8 @@ async def test_form_invalid_location(hass: HomeAssistant, config, setup_airnow)
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=AirNowError)]) @pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=AirNowError)])
async def test_form_cannot_connect(hass: HomeAssistant, config, setup_airnow) -> None: @pytest.mark.usefixtures("setup_airnow")
async def test_form_cannot_connect(hass: HomeAssistant, config: dict[str, Any]) -> None:
"""Test we handle cannot connect error.""" """Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -62,7 +71,8 @@ async def test_form_cannot_connect(hass: HomeAssistant, config, setup_airnow) ->
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=EmptyResponseError)]) @pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=EmptyResponseError)])
async def test_form_empty_result(hass: HomeAssistant, config, setup_airnow) -> None: @pytest.mark.usefixtures("setup_airnow")
async def test_form_empty_result(hass: HomeAssistant, config: dict[str, Any]) -> None:
"""Test we handle empty response error.""" """Test we handle empty response error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -73,7 +83,8 @@ async def test_form_empty_result(hass: HomeAssistant, config, setup_airnow) -> N
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=RuntimeError)]) @pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=RuntimeError)])
async def test_form_unexpected(hass: HomeAssistant, config, setup_airnow) -> None: @pytest.mark.usefixtures("setup_airnow")
async def test_form_unexpected(hass: HomeAssistant, config: dict[str, Any]) -> None:
"""Test we handle an unexpected error.""" """Test we handle an unexpected error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -83,7 +94,10 @@ async def test_form_unexpected(hass: HomeAssistant, config, setup_airnow) -> Non
assert result2["errors"] == {"base": "unknown"} assert result2["errors"] == {"base": "unknown"}
async def test_entry_already_exists(hass: HomeAssistant, config, config_entry) -> None: @pytest.mark.usefixtures("config_entry")
async def test_entry_already_exists(
hass: HomeAssistant, config: dict[str, Any]
) -> None:
"""Test that the form aborts if the Lat/Lng is already configured.""" """Test that the form aborts if the Lat/Lng is already configured."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -93,7 +107,8 @@ async def test_entry_already_exists(hass: HomeAssistant, config, config_entry) -
assert result2["reason"] == "already_configured" assert result2["reason"] == "already_configured"
async def test_config_migration_v2(hass: HomeAssistant, setup_airnow) -> None: @pytest.mark.usefixtures("setup_airnow")
async def test_config_migration_v2(hass: HomeAssistant) -> None:
"""Test that the config migration from Version 1 to Version 2 works.""" """Test that the config migration from Version 1 to Version 2 works."""
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
version=1, version=1,
@ -119,7 +134,8 @@ async def test_config_migration_v2(hass: HomeAssistant, setup_airnow) -> None:
assert config_entry.options.get(CONF_RADIUS) == 25 assert config_entry.options.get(CONF_RADIUS) == 25
async def test_options_flow(hass: HomeAssistant, setup_airnow) -> None: @pytest.mark.usefixtures("setup_airnow")
async def test_options_flow(hass: HomeAssistant) -> None:
"""Test that the options flow works.""" """Test that the options flow works."""
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
version=2, version=2,

View File

@ -1,18 +1,20 @@
"""Test AirNow diagnostics.""" """Test AirNow diagnostics."""
import pytest
from syrupy import SnapshotAssertion from syrupy import SnapshotAssertion
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
from tests.components.diagnostics import get_diagnostics_for_config_entry from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator from tests.typing import ClientSessionGenerator
@pytest.mark.usefixtures("setup_airnow")
async def test_entry_diagnostics( async def test_entry_diagnostics(
hass: HomeAssistant, hass: HomeAssistant,
config_entry, config_entry: MockConfigEntry,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
setup_airnow,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test config entry diagnostics.""" """Test config entry diagnostics."""