Split user flow init and data submission in recollect_waste config flow tests (#184169)

This commit is contained in:
Manu
2026-10-04 15:11:17 +02:00
committed by GitHub
parent ac3525e8bc
commit 02ac65860c
3 changed files with 32 additions and 28 deletions
+4 -3
View File
@@ -1,5 +1,6 @@
"""Define test fixtures for ReCollect Waste."""
from collections.abc import Generator
from datetime import date
from typing import Any
from unittest.mock import AsyncMock, Mock, patch
@@ -21,7 +22,7 @@ TEST_SERVICE_ID = "67890"
@pytest.fixture(name="client")
def client_fixture(pickup_events):
def client_fixture(pickup_events: list[PickupEvent]) -> Mock:
"""Define a fixture to return a mocked aiopurple API object."""
return Mock(async_get_pickup_events=AsyncMock(return_value=pickup_events))
@@ -48,7 +49,7 @@ def config_fixture() -> dict[str, Any]:
@pytest.fixture(name="pickup_events")
def pickup_events_fixture():
def pickup_events_fixture() -> list[PickupEvent]:
"""Define a list of pickup events."""
return [
PickupEvent(
@@ -58,7 +59,7 @@ def pickup_events_fixture():
@pytest.fixture(name="mock_aiorecollect")
def mock_aiorecollect_fixture(client):
def mock_aiorecollect_fixture(client: Mock) -> Generator[None]:
"""Define a fixture to patch aiorecollect."""
with (
patch(
@@ -1,6 +1,7 @@
"""Define tests for the ReCollect Waste config flow."""
from unittest.mock import AsyncMock, patch
from typing import Any
from unittest.mock import AsyncMock, Mock, patch
from aiorecollect.errors import RecollectError
import pytest
@@ -17,23 +18,14 @@ from homeassistant.data_entry_flow import FlowResultType
from .conftest import TEST_PLACE_ID, TEST_SERVICE_ID
from tests.common import MockConfigEntry
@pytest.mark.parametrize(
("get_pickup_events_mock", "get_pickup_events_errors"),
[
(
AsyncMock(side_effect=RecollectError),
{"base": "invalid_place_or_service_id"},
),
],
)
@pytest.mark.usefixtures("mock_aiorecollect")
async def test_create_entry(
hass: HomeAssistant,
client,
config,
get_pickup_events_errors,
get_pickup_events_mock,
mock_aiorecollect,
client: Mock,
config: dict[str, Any],
) -> None:
"""Test creating an entry."""
result = await hass.config_entries.flow.async_init(
@@ -43,12 +35,14 @@ async def test_create_entry(
assert result["step_id"] == "user"
# Test errors that can arise when checking the API key:
with patch.object(client, "async_get_pickup_events", get_pickup_events_mock):
with patch.object(
client, "async_get_pickup_events", AsyncMock(side_effect=RecollectError)
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == get_pickup_events_errors
assert result["errors"] == {"base": "invalid_place_or_service_id"}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
@@ -61,18 +55,25 @@ async def test_create_entry(
}
async def test_duplicate_error(hass: HomeAssistant, config, setup_config_entry) -> None:
@pytest.mark.usefixtures("setup_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
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_options_flow(
hass: HomeAssistant, config, config_entry, setup_config_entry
) -> None:
@pytest.mark.usefixtures("setup_config_entry")
async def test_options_flow(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Test config flow options."""
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] is FlowResultType.FORM
@@ -1,20 +1,22 @@
"""Test ReCollect Waste diagnostics."""
import pytest
from homeassistant.components.diagnostics import REDACTED
from homeassistant.core import HomeAssistant
from .conftest import TEST_SERVICE_ID
from tests.common import ANY
from tests.common import ANY, MockConfigEntry
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
@pytest.mark.usefixtures("setup_config_entry")
async def test_entry_diagnostics(
hass: HomeAssistant,
config_entry,
config_entry: MockConfigEntry,
hass_client: ClientSessionGenerator,
setup_config_entry,
) -> None:
"""Test config entry diagnostics."""
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {