Clean up ReCollect Waste config flow tests (#64813)

* Clean up ReCollect Waste config flow tests

* Use fixture
pull/64814/head
Aaron Bach 2022-01-24 07:51:30 -07:00 committed by GitHub
parent 321f54494e
commit 5b8f6d1b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 39 deletions

View File

@ -0,0 +1,51 @@
"""Define test fixtures for ReCollect Waste."""
from unittest.mock import patch
import pytest
from homeassistant.components.recollect_waste.const import (
CONF_PLACE_ID,
CONF_SERVICE_ID,
DOMAIN,
)
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config, unique_id):
"""Define a config entry fixture."""
entry = MockConfigEntry(domain=DOMAIN, unique_id=unique_id, data=config)
entry.add_to_hass(hass)
return entry
@pytest.fixture(name="config")
def config_fixture(hass):
"""Define a config entry data fixture."""
return {
CONF_PLACE_ID: "12345",
CONF_SERVICE_ID: "12345",
}
@pytest.fixture(name="setup_recollect_waste")
async def setup_recollect_waste_fixture(hass, config):
"""Define a fixture to set up ReCollect Waste."""
with patch(
"homeassistant.components.recollect_waste.Client.async_get_pickup_events"
), patch(
"homeassistant.components.recollect_waste.config_flow.Client.async_get_pickup_events"
), patch(
"homeassistant.components.recollect_waste.PLATFORMS", []
):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
yield
@pytest.fixture(name="unique_id")
def unique_id_fixture(hass):
"""Define a config entry unique ID fixture."""
return "12345, 12345"

View File

@ -12,61 +12,42 @@ from homeassistant.components.recollect_waste import (
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_FRIENDLY_NAME
from tests.common import MockConfigEntry
async def test_duplicate_error(hass):
async def test_duplicate_error(hass, config, config_entry):
"""Test that errors are shown when duplicates are added."""
conf = {CONF_PLACE_ID: "12345", CONF_SERVICE_ID: "12345"}
MockConfigEntry(domain=DOMAIN, unique_id="12345, 12345", data=conf).add_to_hass(
hass
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
async def test_invalid_place_or_service_id(hass):
async def test_invalid_place_or_service_id(hass, config):
"""Test that an invalid Place or Service ID throws an error."""
conf = {CONF_PLACE_ID: "12345", CONF_SERVICE_ID: "12345"}
with patch(
"aiorecollect.client.Client.async_get_pickup_events",
"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=conf
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {"base": "invalid_place_or_service_id"}
async def test_options_flow(hass):
async def test_options_flow(hass, config, config_entry):
"""Test config flow options."""
conf = {CONF_PLACE_ID: "12345", CONF_SERVICE_ID: "12345"}
config_entry = MockConfigEntry(domain=DOMAIN, unique_id="12345, 12345", data=conf)
config_entry.add_to_hass(hass)
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.RESULT_TYPE_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.RESULT_TYPE_CREATE_ENTRY
assert config_entry.options == {CONF_FRIENDLY_NAME: True}
@ -76,22 +57,16 @@ async def test_show_form(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
async def test_step_user(hass):
async def test_step_user(hass, config, setup_recollect_waste):
"""Test that the user step works."""
conf = {CONF_PLACE_ID: "12345", CONF_SERVICE_ID: "12345"}
with patch(
"homeassistant.components.recollect_waste.async_setup_entry", return_value=True
), patch("aiorecollect.client.Client.async_get_pickup_events", return_value=True):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "12345, 12345"
assert result["data"] == {CONF_PLACE_ID: "12345", CONF_SERVICE_ID: "12345"}
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.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "12345, 12345"
assert result["data"] == {CONF_PLACE_ID: "12345", CONF_SERVICE_ID: "12345"}