Clean up OpenUV config flow tests (#64715)

* Clean up OpenUV config flow tests

* Code review
This commit is contained in:
Aaron Bach 2022-01-22 16:52:25 -07:00 committed by GitHub
parent a7982adc73
commit 8badb1085c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 70 deletions

View file

@ -0,0 +1,56 @@
"""Define test fixtures for OpenUV."""
from unittest.mock import patch
import pytest
from homeassistant.components.openuv import CONF_FROM_WINDOW, CONF_TO_WINDOW, DOMAIN
from homeassistant.const import (
CONF_API_KEY,
CONF_ELEVATION,
CONF_LATITUDE,
CONF_LONGITUDE,
)
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,
options={CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 3.5},
)
entry.add_to_hass(hass)
return entry
@pytest.fixture(name="config")
def config_fixture(hass):
"""Define a config entry data fixture."""
return {
CONF_API_KEY: "abcde12345",
CONF_ELEVATION: 0,
CONF_LATITUDE: 51.528308,
CONF_LONGITUDE: -0.3817765,
}
@pytest.fixture(name="setup_openuv")
async def setup_openuv_fixture(hass, config):
"""Define a fixture to set up OpenUV."""
with patch("homeassistant.components.openuv.Client.uv_index"), patch(
"homeassistant.components.openuv.Client.uv_protection_window"
), patch("homeassistant.components.openuv.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 "51.528308, -0.3817765"

View file

@ -13,107 +13,60 @@ from homeassistant.const import (
CONF_LONGITUDE, CONF_LONGITUDE,
) )
from tests.common import MockConfigEntry
async def test_duplicate_error(hass, config, config_entry):
async def test_duplicate_error(hass):
"""Test that errors are shown when duplicates are added.""" """Test that errors are shown when duplicates are added."""
conf = {
CONF_API_KEY: "12345abcde",
CONF_ELEVATION: 59.1234,
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
}
MockConfigEntry(
domain=DOMAIN, unique_id="39.128712, -104.9812612", data=conf
).add_to_hass(hass)
result = await hass.config_entries.flow.async_init( 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["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
async def test_invalid_api_key(hass): async def test_invalid_api_key(hass, config):
"""Test that an invalid API key throws an error.""" """Test that an invalid API key throws an error."""
conf = {
CONF_API_KEY: "12345abcde",
CONF_ELEVATION: 59.1234,
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
}
with patch( with patch(
"pyopenuv.client.Client.uv_index", "homeassistant.components.openuv.Client.uv_index",
side_effect=InvalidApiKeyError, side_effect=InvalidApiKeyError,
): ):
result = await hass.config_entries.flow.async_init( 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["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"} assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
async def test_options_flow(hass): async def test_options_flow(hass, config_entry):
"""Test config flow options.""" """Test config flow options."""
conf = {
CONF_API_KEY: "12345abcde",
CONF_ELEVATION: 59.1234,
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
}
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="abcde12345",
data=conf,
)
config_entry.add_to_hass(hass)
with patch("homeassistant.components.openuv.async_setup_entry", return_value=True): with patch("homeassistant.components.openuv.async_setup_entry", return_value=True):
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)
result = await hass.config_entries.options.async_init(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["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init" assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0} result["flow_id"], user_input={CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0}
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert config_entry.options == {CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0} assert config_entry.options == {CONF_FROM_WINDOW: 3.5, CONF_TO_WINDOW: 2.0}
async def test_step_user(hass): async def test_step_user(hass, config, setup_openuv):
"""Test that the user step works.""" """Test that the user step works."""
conf = { result = await hass.config_entries.flow.async_init(
CONF_API_KEY: "12345abcde", DOMAIN, context={"source": SOURCE_USER}
CONF_ELEVATION: 59.1234, )
CONF_LATITUDE: 39.128712, assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
CONF_LONGITUDE: -104.9812612, assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "51.528308, -0.3817765"
assert result["data"] == {
CONF_API_KEY: "abcde12345",
CONF_ELEVATION: 0,
CONF_LATITUDE: 51.528308,
CONF_LONGITUDE: -0.3817765,
} }
with patch(
"homeassistant.components.openuv.async_setup_entry", return_value=True
), patch("pyopenuv.client.Client.uv_index"):
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"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "39.128712, -104.9812612"
assert result["data"] == {
CONF_API_KEY: "12345abcde",
CONF_ELEVATION: 59.1234,
CONF_LATITUDE: 39.128712,
CONF_LONGITUDE: -104.9812612,
}