From 5b8f6d1b9a231fdaffe8e70dde992b5b738cef7f Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Mon, 24 Jan 2022 07:51:30 -0700 Subject: [PATCH] Clean up ReCollect Waste config flow tests (#64813) * Clean up ReCollect Waste config flow tests * Use fixture --- tests/components/recollect_waste/conftest.py | 51 ++++++++++++++++++ .../recollect_waste/test_config_flow.py | 53 +++++-------------- 2 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 tests/components/recollect_waste/conftest.py diff --git a/tests/components/recollect_waste/conftest.py b/tests/components/recollect_waste/conftest.py new file mode 100644 index 00000000000..1bdf729c47b --- /dev/null +++ b/tests/components/recollect_waste/conftest.py @@ -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" diff --git a/tests/components/recollect_waste/test_config_flow.py b/tests/components/recollect_waste/test_config_flow.py index b55202d93b3..4295f3777d5 100644 --- a/tests/components/recollect_waste/test_config_flow.py +++ b/tests/components/recollect_waste/test_config_flow.py @@ -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"}