Refactor Aurora tests (#117323)
This commit is contained in:
parent
54ba393be8
commit
1d16e219e4
3 changed files with 121 additions and 73 deletions
|
@ -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)
|
||||
|
|
55
tests/components/aurora/conftest.py
Normal file
55
tests/components/aurora/conftest.py
Normal 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,
|
||||
},
|
||||
)
|
|
@ -1,117 +1,99 @@
|
|||
"""Test the Aurora config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from aiohttp import ClientError
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.aurora.const import DOMAIN
|
||||
from homeassistant.components.aurora.const import CONF_THRESHOLD, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.aurora import setup_integration
|
||||
|
||||
DATA = {
|
||||
"latitude": -10,
|
||||
"longitude": 10.2,
|
||||
CONF_LATITUDE: -10,
|
||||
CONF_LONGITUDE: 10.2,
|
||||
}
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant) -> None:
|
||||
"""Test we get the form."""
|
||||
async def test_full_flow(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_aurora_client: AsyncMock
|
||||
) -> None:
|
||||
"""Test full flow."""
|
||||
|
||||
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["errors"] == {}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"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()
|
||||
result = 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 result2["title"] == "Aurora visibility"
|
||||
assert result2["data"] == DATA
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Aurora visibility"
|
||||
assert result["data"] == DATA
|
||||
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."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.aurora.config_flow.AuroraForecast.get_forecast_data",
|
||||
side_effect=ClientError,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
DATA,
|
||||
)
|
||||
mock_aurora_client.get_forecast_data.side_effect = side_effect
|
||||
|
||||
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": "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:
|
||||
"""Test with unknown error response from the API."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
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:
|
||||
async def test_option_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_aurora_client: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test option flow."""
|
||||
entry = MockConfigEntry(domain=DOMAIN, data=DATA)
|
||||
entry.add_to_hass(hass)
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert not entry.options
|
||||
|
||||
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,
|
||||
)
|
||||
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={"forecast_threshold": 65},
|
||||
user_input={CONF_THRESHOLD: 65},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["data"]["forecast_threshold"] == 65
|
||||
assert result["data"][CONF_THRESHOLD] == 65
|
||||
|
|
Loading…
Add table
Reference in a new issue