Skip to content

Commit

Permalink
Improve type hints in iqvia tests (home-assistant#121506)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jul 8, 2024
1 parent 1488034 commit b048ad8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
61 changes: 33 additions & 28 deletions tests/components/iqvia/conftest.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
"""Define test fixtures for IQVIA."""

import json
from collections.abc import AsyncGenerator
from typing import Any
from unittest.mock import patch

import pytest

from homeassistant.components.iqvia.const import CONF_ZIP_CODE, DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util.json import JsonObjectType

from tests.common import MockConfigEntry, load_fixture
from tests.common import MockConfigEntry, load_json_object_fixture


@pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config):
def config_entry_fixture(
hass: HomeAssistant, config: dict[str, Any]
) -> MockConfigEntry:
"""Define a config entry fixture."""
entry = MockConfigEntry(
domain=DOMAIN,
Expand All @@ -25,67 +30,67 @@ def config_entry_fixture(hass, config):


@pytest.fixture(name="config")
def config_fixture(hass):
def config_fixture() -> dict[str, Any]:
"""Define a config entry data fixture."""
return {
CONF_ZIP_CODE: "12345",
}


@pytest.fixture(name="data_allergy_forecast", scope="package")
def data_allergy_forecast_fixture():
def data_allergy_forecast_fixture() -> JsonObjectType:
"""Define allergy forecast data."""
return json.loads(load_fixture("allergy_forecast_data.json", "iqvia"))
return load_json_object_fixture("allergy_forecast_data.json", "iqvia")


@pytest.fixture(name="data_allergy_index", scope="package")
def data_allergy_index_fixture():
def data_allergy_index_fixture() -> JsonObjectType:
"""Define allergy index data."""
return json.loads(load_fixture("allergy_index_data.json", "iqvia"))
return load_json_object_fixture("allergy_index_data.json", "iqvia")


@pytest.fixture(name="data_allergy_outlook", scope="package")
def data_allergy_outlook_fixture():
def data_allergy_outlook_fixture() -> JsonObjectType:
"""Define allergy outlook data."""
return json.loads(load_fixture("allergy_outlook_data.json", "iqvia"))
return load_json_object_fixture("allergy_outlook_data.json", "iqvia")


@pytest.fixture(name="data_asthma_forecast", scope="package")
def data_asthma_forecast_fixture():
def data_asthma_forecast_fixture() -> JsonObjectType:
"""Define asthma forecast data."""
return json.loads(load_fixture("asthma_forecast_data.json", "iqvia"))
return load_json_object_fixture("asthma_forecast_data.json", "iqvia")


@pytest.fixture(name="data_asthma_index", scope="package")
def data_asthma_index_fixture():
def data_asthma_index_fixture() -> JsonObjectType:
"""Define asthma index data."""
return json.loads(load_fixture("asthma_index_data.json", "iqvia"))
return load_json_object_fixture("asthma_index_data.json", "iqvia")


@pytest.fixture(name="data_disease_forecast", scope="package")
def data_disease_forecast_fixture():
def data_disease_forecast_fixture() -> JsonObjectType:
"""Define disease forecast data."""
return json.loads(load_fixture("disease_forecast_data.json", "iqvia"))
return load_json_object_fixture("disease_forecast_data.json", "iqvia")


@pytest.fixture(name="data_disease_index", scope="package")
def data_disease_index_fixture():
def data_disease_index_fixture() -> JsonObjectType:
"""Define disease index data."""
return json.loads(load_fixture("disease_index_data.json", "iqvia"))
return load_json_object_fixture("disease_index_data.json", "iqvia")


@pytest.fixture(name="setup_iqvia")
async def setup_iqvia_fixture(
hass,
config,
data_allergy_forecast,
data_allergy_index,
data_allergy_outlook,
data_asthma_forecast,
data_asthma_index,
data_disease_forecast,
data_disease_index,
):
hass: HomeAssistant,
config: dict[str, Any],
data_allergy_forecast: JsonObjectType,
data_allergy_index: JsonObjectType,
data_allergy_outlook: JsonObjectType,
data_asthma_forecast: JsonObjectType,
data_asthma_index: JsonObjectType,
data_disease_forecast: JsonObjectType,
data_disease_index: JsonObjectType,
) -> AsyncGenerator[None]:
"""Define a fixture to set up IQVIA."""
with (
patch(
Expand Down
10 changes: 8 additions & 2 deletions tests/components/iqvia/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
"""Define tests for the IQVIA config flow."""

from typing import Any

import pytest

from homeassistant.components.iqvia import CONF_ZIP_CODE, DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType


async def test_duplicate_error(hass: HomeAssistant, config, config_entry) -> None:
@pytest.mark.usefixtures("config_entry")
async def test_duplicate_error(hass: HomeAssistant, config: dict[str, Any]) -> None:
"""Test that errors are shown when duplicates are added."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
Expand All @@ -33,7 +38,8 @@ async def test_show_form(hass: HomeAssistant) -> None:
assert result["step_id"] == "user"


async def test_step_user(hass: HomeAssistant, config, setup_iqvia) -> None:
@pytest.mark.usefixtures("setup_iqvia")
async def test_step_user(hass: HomeAssistant, config: dict[str, Any]) -> None:
"""Test that the user step works (without MFA)."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
Expand Down
5 changes: 3 additions & 2 deletions tests/components/iqvia/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator


async def test_entry_diagnostics(
hass: HomeAssistant,
config_entry,
config_entry: MockConfigEntry,
hass_client: ClientSessionGenerator,
setup_iqvia,
setup_iqvia: None, # Needs to be injected after config_entry
snapshot: SnapshotAssertion,
) -> None:
"""Test config entry diagnostics."""
Expand Down

0 comments on commit b048ad8

Please sign in to comment.