Renovate ReCollect Waste config flow tests (#84908)

This commit is contained in:
Aaron Bach 2022-12-31 07:35:33 -07:00 committed by GitHub
parent e7cb3f1979
commit ad51952802
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 71 deletions

View File

@ -1,6 +1,6 @@
"""Define test fixtures for ReCollect Waste."""
from datetime import date
from unittest.mock import patch
from unittest.mock import AsyncMock, Mock, patch
from aiorecollect.client import PickupEvent, PickupType
import pytest
@ -10,48 +10,64 @@ from homeassistant.components.recollect_waste.const import (
CONF_SERVICE_ID,
DOMAIN,
)
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
TEST_PLACE_ID = "12345"
TEST_SERVICE_ID = "67890"
@pytest.fixture(name="client")
def client_fixture(pickup_events):
"""Define a fixture to return a mocked aiopurple API object."""
return Mock(async_get_pickup_events=AsyncMock(return_value=pickup_events))
@pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config):
"""Define a config entry fixture."""
entry = MockConfigEntry(
domain=DOMAIN,
unique_id=f"{config[CONF_PLACE_ID]}, {config[CONF_SERVICE_ID]}",
data=config,
domain=DOMAIN, unique_id=f"{TEST_PLACE_ID}, {TEST_SERVICE_ID}", data=config
)
entry.add_to_hass(hass)
return entry
@pytest.fixture(name="config")
def config_fixture(hass):
def config_fixture():
"""Define a config entry data fixture."""
return {
CONF_PLACE_ID: "12345",
CONF_SERVICE_ID: "12345",
CONF_PLACE_ID: TEST_PLACE_ID,
CONF_SERVICE_ID: TEST_SERVICE_ID,
}
@pytest.fixture(name="setup_recollect_waste")
async def setup_recollect_waste_fixture(hass, config):
"""Define a fixture to set up ReCollect Waste."""
pickup_event = PickupEvent(
date(2022, 1, 23), [PickupType("garbage", "Trash Collection")], "The Sun"
)
@pytest.fixture(name="pickup_events")
def pickup_events_fixture():
"""Define a list of pickup events."""
return [
PickupEvent(
date(2022, 1, 23), [PickupType("garbage", "Trash Collection")], "The Sun"
)
]
@pytest.fixture(name="mock_aiorecollect")
async def mock_aiorecollect_fixture(client):
"""Define a fixture to patch aiorecollect."""
with patch(
"homeassistant.components.recollect_waste.Client.async_get_pickup_events",
return_value=[pickup_event],
"homeassistant.components.recollect_waste.Client",
return_value=client,
), patch(
"homeassistant.components.recollect_waste.config_flow.Client.async_get_pickup_events",
return_value=[pickup_event],
), patch(
"homeassistant.components.recollect_waste.PLATFORMS", []
"homeassistant.components.recollect_waste.config_flow.Client",
return_value=client,
):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
yield
@pytest.fixture(name="setup_config_entry")
async def setup_config_entry_fixture(hass, config_entry, mock_aiorecollect):
"""Define a fixture to set up recollect_waste."""
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
yield

View File

@ -1,7 +1,8 @@
"""Define tests for the ReCollect Waste config flow."""
from unittest.mock import patch
from unittest.mock import AsyncMock, patch
from aiorecollect.errors import RecollectError
import pytest
from homeassistant import data_entry_flow
from homeassistant.components.recollect_waste import (
@ -12,8 +13,53 @@ from homeassistant.components.recollect_waste import (
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_FRIENDLY_NAME
from .conftest import TEST_PLACE_ID, TEST_SERVICE_ID
async def test_duplicate_error(hass, config, config_entry):
@pytest.mark.parametrize(
"get_pickup_events_mock,get_pickup_events_errors",
[
(
AsyncMock(side_effect=RecollectError),
{"base": "invalid_place_or_service_id"},
),
],
)
async def test_create_entry(
hass,
client,
config,
get_pickup_events_errors,
get_pickup_events_mock,
mock_aiorecollect,
):
"""Test creating an entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
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):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == get_pickup_events_errors
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == f"{TEST_PLACE_ID}, {TEST_SERVICE_ID}"
assert result["data"] == {
CONF_PLACE_ID: TEST_PLACE_ID,
CONF_SERVICE_ID: TEST_SERVICE_ID,
}
async def test_duplicate_error(hass, config, setup_config_entry):
"""Test that errors are shown when duplicates are added."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
@ -22,51 +68,14 @@ async def test_duplicate_error(hass, config, config_entry):
assert result["reason"] == "already_configured"
async def test_invalid_place_or_service_id(hass, config):
"""Test that an invalid Place or Service ID throws an error."""
with patch(
"homeassistant.components.recollect_waste.config_flow.Client.async_get_pickup_events",
side_effect=RecollectError,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == {"base": "invalid_place_or_service_id"}
async def test_options_flow(hass, config, config_entry):
async def test_options_flow(hass, config, config_entry, setup_config_entry):
"""Test config flow options."""
with patch(
"homeassistant.components.recollect_waste.async_setup_entry", return_value=True
):
await hass.config_entries.async_setup(config_entry.entry_id)
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_FRIENDLY_NAME: True}
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert config_entry.options == {CONF_FRIENDLY_NAME: True}
async def test_show_form(hass):
"""Test that the form is served with no input."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"
assert result["step_id"] == "init"
async def test_step_user(hass, config, setup_recollect_waste):
"""Test that the user step works."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_FRIENDLY_NAME: True}
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "12345, 12345"
assert result["data"] == {CONF_PLACE_ID: "12345", CONF_SERVICE_ID: "12345"}
assert config_entry.options == {CONF_FRIENDLY_NAME: True}

View File

@ -1,12 +1,12 @@
"""Test ReCollect Waste diagnostics."""
from homeassistant.components.diagnostics import REDACTED
from .conftest import TEST_SERVICE_ID
from tests.components.diagnostics import get_diagnostics_for_config_entry
async def test_entry_diagnostics(
hass, config_entry, hass_client, setup_recollect_waste
):
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_config_entry):
"""Test config entry diagnostics."""
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
"entry": {
@ -14,7 +14,7 @@ async def test_entry_diagnostics(
"version": 2,
"domain": "recollect_waste",
"title": REDACTED,
"data": {"place_id": REDACTED, "service_id": "12345"},
"data": {"place_id": REDACTED, "service_id": TEST_SERVICE_ID},
"options": {},
"pref_disable_new_entities": False,
"pref_disable_polling": False,