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.""" """Define test fixtures for ReCollect Waste."""
from datetime import date from datetime import date
from unittest.mock import patch from unittest.mock import AsyncMock, Mock, patch
from aiorecollect.client import PickupEvent, PickupType from aiorecollect.client import PickupEvent, PickupType
import pytest import pytest
@ -10,48 +10,64 @@ from homeassistant.components.recollect_waste.const import (
CONF_SERVICE_ID, CONF_SERVICE_ID,
DOMAIN, DOMAIN,
) )
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry 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") @pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config): def config_entry_fixture(hass, config):
"""Define a config entry fixture.""" """Define a config entry fixture."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN, unique_id=f"{TEST_PLACE_ID}, {TEST_SERVICE_ID}", data=config
unique_id=f"{config[CONF_PLACE_ID]}, {config[CONF_SERVICE_ID]}",
data=config,
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
return entry return entry
@pytest.fixture(name="config") @pytest.fixture(name="config")
def config_fixture(hass): def config_fixture():
"""Define a config entry data fixture.""" """Define a config entry data fixture."""
return { return {
CONF_PLACE_ID: "12345", CONF_PLACE_ID: TEST_PLACE_ID,
CONF_SERVICE_ID: "12345", CONF_SERVICE_ID: TEST_SERVICE_ID,
} }
@pytest.fixture(name="setup_recollect_waste") @pytest.fixture(name="pickup_events")
async def setup_recollect_waste_fixture(hass, config): def pickup_events_fixture():
"""Define a fixture to set up ReCollect Waste.""" """Define a list of pickup events."""
pickup_event = PickupEvent( return [
date(2022, 1, 23), [PickupType("garbage", "Trash Collection")], "The Sun" 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( with patch(
"homeassistant.components.recollect_waste.Client.async_get_pickup_events", "homeassistant.components.recollect_waste.Client",
return_value=[pickup_event], return_value=client,
), patch( ), patch(
"homeassistant.components.recollect_waste.config_flow.Client.async_get_pickup_events", "homeassistant.components.recollect_waste.config_flow.Client",
return_value=[pickup_event], return_value=client,
), patch(
"homeassistant.components.recollect_waste.PLATFORMS", []
): ):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
yield 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.""" """Define tests for the ReCollect Waste config flow."""
from unittest.mock import patch from unittest.mock import AsyncMock, patch
from aiorecollect.errors import RecollectError from aiorecollect.errors import RecollectError
import pytest
from homeassistant import data_entry_flow from homeassistant import data_entry_flow
from homeassistant.components.recollect_waste import ( 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.config_entries import SOURCE_USER
from homeassistant.const import CONF_FRIENDLY_NAME 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.""" """Test that errors are shown when duplicates are added."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config 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" assert result["reason"] == "already_configured"
async def test_invalid_place_or_service_id(hass, config): async def test_options_flow(hass, config, config_entry, setup_config_entry):
"""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):
"""Test config flow options.""" """Test config flow options."""
with patch( result = await hass.config_entries.options.async_init(config_entry.entry_id)
"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}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user" assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
async def test_step_user(hass, config, setup_recollect_waste): result["flow_id"], user_input={CONF_FRIENDLY_NAME: True}
"""Test that the user step works."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
) )
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "12345, 12345" assert config_entry.options == {CONF_FRIENDLY_NAME: True}
assert result["data"] == {CONF_PLACE_ID: "12345", CONF_SERVICE_ID: "12345"}

View file

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