Refactor Aurora tests (#117323)

This commit is contained in:
Joost Lekkerkerker 2024-05-18 13:40:30 +02:00 committed by GitHub
parent 54ba393be8
commit 1d16e219e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 121 additions and 73 deletions

View file

@ -1 +1,12 @@
"""The tests for the Aurora sensor platform.""" """The tests for the Aurora integration."""
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Fixture for setting up the component."""
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)

View file

@ -0,0 +1,55 @@
"""Common fixtures for the Aurora tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.aurora.const import CONF_THRESHOLD, DOMAIN
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from tests.common import MockConfigEntry
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.aurora.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_aurora_client() -> Generator[AsyncMock, None, None]:
"""Mock a Homeassistant Analytics client."""
with (
patch(
"homeassistant.components.aurora.coordinator.AuroraForecast",
autospec=True,
) as mock_client,
patch(
"homeassistant.components.aurora.config_flow.AuroraForecast",
new=mock_client,
),
):
client = mock_client.return_value
client.get_forecast_data.return_value = 42
yield client
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Mock a config entry."""
return MockConfigEntry(
domain=DOMAIN,
title="Aurora visibility",
data={
CONF_LATITUDE: -10,
CONF_LONGITUDE: 10.2,
},
options={
CONF_THRESHOLD: 75,
},
)

View file

@ -1,117 +1,99 @@
"""Test the Aurora config flow.""" """Test the Aurora config flow."""
from unittest.mock import patch from unittest.mock import AsyncMock
from aiohttp import ClientError from aiohttp import ClientError
import pytest
from homeassistant import config_entries from homeassistant.components.aurora.const import CONF_THRESHOLD, DOMAIN
from homeassistant.components.aurora.const import DOMAIN from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
from tests.components.aurora import setup_integration
DATA = { DATA = {
"latitude": -10, CONF_LATITUDE: -10,
"longitude": 10.2, CONF_LONGITUDE: 10.2,
} }
async def test_form(hass: HomeAssistant) -> None: async def test_full_flow(
"""Test we get the form.""" hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_aurora_client: AsyncMock
) -> None:
"""Test full flow."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
) )
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["errors"] == {} assert result["errors"] == {}
with ( result = await hass.config_entries.flow.async_configure(result["flow_id"], DATA)
patch( await hass.async_block_till_done()
"homeassistant.components.aurora.config_flow.AuroraForecast.get_forecast_data",
return_value=True,
),
patch(
"homeassistant.components.aurora.async_setup_entry",
return_value=True,
) as mock_setup_entry,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
DATA,
)
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "Aurora visibility" assert result["title"] == "Aurora visibility"
assert result2["data"] == DATA assert result["data"] == DATA
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
async def test_form_cannot_connect(hass: HomeAssistant) -> None: @pytest.mark.parametrize(
("side_effect", "error"),
[
(ClientError, "cannot_connect"),
(Exception, "unknown"),
],
)
async def test_form_errors(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_aurora_client: AsyncMock,
side_effect: Exception,
error: str,
) -> None:
"""Test if invalid response or no connection returned from the API.""" """Test if invalid response or no connection returned from the API."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
) )
with patch( mock_aurora_client.get_forecast_data.side_effect = side_effect
"homeassistant.components.aurora.config_flow.AuroraForecast.get_forecast_data",
side_effect=ClientError, result = await hass.config_entries.flow.async_configure(result["flow_id"], DATA)
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
DATA,
)
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"} assert result["errors"] == {"base": error}
mock_aurora_client.get_forecast_data.side_effect = None
result = await hass.config_entries.flow.async_configure(result["flow_id"], DATA)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_with_unknown_error(hass: HomeAssistant) -> None: async def test_option_flow(
"""Test with unknown error response from the API.""" hass: HomeAssistant,
mock_setup_entry: AsyncMock,
result = await hass.config_entries.flow.async_init( mock_aurora_client: AsyncMock,
DOMAIN, context={"source": config_entries.SOURCE_USER} mock_config_entry: MockConfigEntry,
) ) -> None:
with patch(
"homeassistant.components.aurora.config_flow.AuroraForecast.get_forecast_data",
side_effect=Exception,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
DATA,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "unknown"}
async def test_option_flow(hass: HomeAssistant) -> None:
"""Test option flow.""" """Test option flow."""
entry = MockConfigEntry(domain=DOMAIN, data=DATA) await setup_integration(hass, mock_config_entry)
entry.add_to_hass(hass)
assert not entry.options result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
with patch("homeassistant.components.aurora.async_setup_entry", return_value=True):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(
entry.entry_id,
data=None,
)
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.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"], result["flow_id"],
user_input={"forecast_threshold": 65}, user_input={CONF_THRESHOLD: 65},
) )
assert result["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"]["forecast_threshold"] == 65 assert result["data"][CONF_THRESHOLD] == 65