Renovate OpenUV config flow tests (#85150)

This commit is contained in:
Aaron Bach 2023-01-06 02:51:46 -07:00 committed by GitHub
parent 9ed629d838
commit 39b110b9b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 98 deletions

View file

@ -1,6 +1,6 @@
"""Define test fixtures for OpenUV.""" """Define test fixtures for OpenUV."""
import json import json
from unittest.mock import patch from unittest.mock import AsyncMock, Mock, patch
import pytest import pytest
@ -11,10 +11,23 @@ from homeassistant.const import (
CONF_LATITUDE, CONF_LATITUDE,
CONF_LONGITUDE, CONF_LONGITUDE,
) )
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, load_fixture
TEST_API_KEY = "abcde12345"
TEST_ELEVATION = 0
TEST_LATITUDE = 51.528308
TEST_LONGITUDE = -0.3817765
@pytest.fixture(name="client")
def client_fixture(data_protection_window, data_uv_index):
"""Define a mock Client object."""
return Mock(
uv_index=AsyncMock(return_value=data_uv_index),
uv_protection_window=AsyncMock(return_value=data_protection_window),
)
@pytest.fixture(name="config_entry") @pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config): def config_entry_fixture(hass, config):
@ -30,13 +43,13 @@ def config_entry_fixture(hass, config):
@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_API_KEY: "abcde12345", CONF_API_KEY: TEST_API_KEY,
CONF_ELEVATION: 0, CONF_ELEVATION: TEST_ELEVATION,
CONF_LATITUDE: 51.528308, CONF_LATITUDE: TEST_LATITUDE,
CONF_LONGITUDE: -0.3817765, CONF_LONGITUDE: TEST_LONGITUDE,
} }
@ -52,17 +65,18 @@ def data_uv_index_fixture():
return json.loads(load_fixture("uv_index_data.json", "openuv")) return json.loads(load_fixture("uv_index_data.json", "openuv"))
@pytest.fixture(name="setup_openuv") @pytest.fixture(name="mock_pyopenuv")
async def setup_openuv_fixture(hass, config, data_protection_window, data_uv_index): async def mock_pyopenuv_fixture(client):
"""Define a fixture to set up OpenUV.""" """Define a fixture to patch pyopenuv."""
with patch( with patch(
"homeassistant.components.openuv.Client.uv_index", return_value=data_uv_index "homeassistant.components.openuv.config_flow.Client", return_value=client
), patch( ), patch("homeassistant.components.openuv.Client", return_value=client):
"homeassistant.components.openuv.Client.uv_protection_window", yield
return_value=data_protection_window,
), patch(
"homeassistant.components.openuv.PLATFORMS", [] @pytest.fixture(name="setup_config_entry")
): async def setup_config_entry_fixture(hass, config_entry, mock_pyopenuv):
assert await async_setup_component(hass, DOMAIN, config) """Define a fixture to set up openuv."""
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
yield yield

View file

@ -1,5 +1,5 @@
"""Define tests for the OpenUV config flow.""" """Define tests for the OpenUV config flow."""
from unittest.mock import patch from unittest.mock import AsyncMock, patch
from pyopenuv.errors import InvalidApiKeyError from pyopenuv.errors import InvalidApiKeyError
import voluptuous as vol import voluptuous as vol
@ -14,8 +14,41 @@ from homeassistant.const import (
CONF_LONGITUDE, CONF_LONGITUDE,
) )
from .conftest import TEST_API_KEY, TEST_ELEVATION, TEST_LATITUDE, TEST_LONGITUDE
async def test_duplicate_error(hass, config, config_entry):
async def test_create_entry(hass, client, config, mock_pyopenuv):
"""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 an error occurring:
with patch.object(client, "uv_index", AsyncMock(side_effect=InvalidApiKeyError)):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
# Test that we can recover from the error:
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_LATITUDE}, {TEST_LONGITUDE}"
assert result["data"] == {
CONF_API_KEY: TEST_API_KEY,
CONF_ELEVATION: TEST_ELEVATION,
CONF_LATITUDE: TEST_LATITUDE,
CONF_LONGITUDE: TEST_LONGITUDE,
}
async def test_duplicate_error(hass, config, config_entry, 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
@ -24,40 +57,25 @@ async def test_duplicate_error(hass, config, config_entry):
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
async def test_invalid_api_key(hass, config): async def test_options_flow(hass, config_entry, setup_config_entry):
"""Test that an invalid API key throws an error.""" """Test config flow options."""
with patch( result = await hass.config_entries.options.async_init(config_entry.entry_id)
"homeassistant.components.openuv.Client.uv_index",
side_effect=InvalidApiKeyError,
):
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["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"} assert result["step_id"] == "init"
def get_schema_marker(data_schema: vol.Schema, key: str) -> vol.Marker:
def _get_schema_marker(data_schema: vol.Schema, key: str) -> vol.Marker:
for k in data_schema.schema: for k in data_schema.schema:
if k == key and isinstance(k, vol.Marker): if k == key and isinstance(k, vol.Marker):
return k return k
return None return None
# Original schema uses defaults for suggested values:
async def test_options_flow(hass, config_entry): assert get_schema_marker(result["data_schema"], CONF_FROM_WINDOW).description == {
"""Test config flow options.""" "suggested_value": 3.5
with patch("homeassistant.components.openuv.async_setup_entry", return_value=True): }
await hass.config_entries.async_setup(config_entry.entry_id) assert get_schema_marker(result["data_schema"], CONF_TO_WINDOW).description == {
result = await hass.config_entries.options.async_init(config_entry.entry_id) "suggested_value": 3.5
assert result["type"] == data_entry_flow.FlowResultType.FORM }
assert result["step_id"] == "init"
# Original schema uses defaults for suggested values
assert _get_schema_marker(
result["data_schema"], CONF_FROM_WINDOW
).description == {"suggested_value": 3.5}
assert _get_schema_marker(
result["data_schema"], CONF_TO_WINDOW
).description == {"suggested_value": 3.5}
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}
@ -65,19 +83,19 @@ async def test_options_flow(hass, config_entry):
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY assert result["type"] == data_entry_flow.FlowResultType.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}
# Subsequent schema uses previous input for suggested values # Subsequent schema uses previous input for suggested values:
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.FlowResultType.FORM assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init" assert result["step_id"] == "init"
assert _get_schema_marker( assert get_schema_marker(result["data_schema"], CONF_FROM_WINDOW).description == {
result["data_schema"], CONF_FROM_WINDOW "suggested_value": 3.5
).description == {"suggested_value": 3.5} }
assert _get_schema_marker( assert get_schema_marker(result["data_schema"], CONF_TO_WINDOW).description == {
result["data_schema"], CONF_TO_WINDOW "suggested_value": 2.0
).description == {"suggested_value": 2.0} }
async def test_step_reauth(hass, config, config_entry, setup_openuv): async def test_step_reauth(hass, config, config_entry, setup_config_entry):
"""Test that the reauth step works.""" """Test that the reauth step works."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_REAUTH}, data=config DOMAIN, context={"source": SOURCE_REAUTH}, data=config
@ -88,33 +106,9 @@ async def test_step_reauth(hass, config, config_entry, setup_openuv):
assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "reauth_confirm" assert result["step_id"] == "reauth_confirm"
with patch("homeassistant.components.openuv.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_API_KEY: "new_api_key"} result["flow_id"], user_input={CONF_API_KEY: "new_api_key"}
) )
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.FlowResultType.ABORT assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "reauth_successful" assert result["reason"] == "reauth_successful"
assert len(hass.config_entries.async_entries()) == 1 assert len(hass.config_entries.async_entries()) == 1
async def test_step_user(hass, config, setup_openuv):
"""Test that the user step works."""
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"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.FlowResultType.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,
}

View file

@ -5,7 +5,7 @@ from homeassistant.setup import async_setup_component
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(hass, config_entry, hass_client, setup_openuv): async def test_entry_diagnostics(hass, config_entry, hass_client, setup_config_entry):
"""Test config entry diagnostics.""" """Test config entry diagnostics."""
await async_setup_component(hass, "homeassistant", {}) await async_setup_component(hass, "homeassistant", {})
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == { assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {