mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Renovate Ambient PWS config flow tests (#84879)
* Renovate Ambient PWS config flow tests * Naming * Update tests/components/ambient_station/conftest.py * Update tests/components/ambient_station/conftest.py * Simplify
This commit is contained in:
parent
9b3d727e8e
commit
f8467d253e
@ -1,16 +1,21 @@
|
||||
"""Define test fixtures for Ambient PWS."""
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.ambient_station.const import CONF_APP_KEY, DOMAIN
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
|
||||
@pytest.fixture(name="api")
|
||||
def api_fixture(hass, data_devices):
|
||||
"""Define a mock API object."""
|
||||
return Mock(get_devices=AsyncMock(return_value=data_devices))
|
||||
|
||||
|
||||
@pytest.fixture(name="config")
|
||||
def config_fixture(hass):
|
||||
"""Define a config entry data fixture."""
|
||||
@ -28,27 +33,31 @@ def config_entry_fixture(hass, config):
|
||||
return entry
|
||||
|
||||
|
||||
@pytest.fixture(name="devices", scope="package")
|
||||
def devices_fixture():
|
||||
@pytest.fixture(name="data_devices", scope="package")
|
||||
def data_devices_fixture():
|
||||
"""Define devices data."""
|
||||
return json.loads(load_fixture("devices.json", "ambient_station"))
|
||||
|
||||
|
||||
@pytest.fixture(name="setup_ambient_station")
|
||||
async def setup_ambient_station_fixture(hass, config, devices):
|
||||
"""Define a fixture to set up AirVisual."""
|
||||
with patch("homeassistant.components.ambient_station.PLATFORMS", []), patch(
|
||||
"homeassistant.components.ambient_station.config_flow.API.get_devices",
|
||||
side_effect=devices,
|
||||
), patch("aioambient.api.API.get_devices", side_effect=devices), patch(
|
||||
"aioambient.websocket.Websocket.connect"
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
@pytest.fixture(name="data_station", scope="package")
|
||||
def data_station_fixture():
|
||||
"""Define station data."""
|
||||
return json.loads(load_fixture("station_data.json", "ambient_station"))
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_aioambient")
|
||||
async def mock_aioambient_fixture(api):
|
||||
"""Define a fixture to patch aioambient."""
|
||||
with patch(
|
||||
"homeassistant.components.ambient_station.config_flow.API",
|
||||
return_value=api,
|
||||
), patch("aioambient.websocket.Websocket.connect"):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="station_data", scope="package")
|
||||
def station_data_fixture():
|
||||
"""Define devices data."""
|
||||
return json.loads(load_fixture("station_data.json", "ambient_station"))
|
||||
@pytest.fixture(name="setup_config_entry")
|
||||
async def setup_config_entry_fixture(hass, config_entry, mock_aioambient):
|
||||
"""Define a fixture to set up ambient_station."""
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
yield
|
||||
|
@ -1,5 +1,5 @@
|
||||
"""Define tests for the Ambient PWS config flow."""
|
||||
from unittest.mock import AsyncMock
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from aioambient.errors import AmbientError
|
||||
import pytest
|
||||
@ -10,44 +10,34 @@ from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
|
||||
|
||||
async def test_duplicate_error(hass, config, config_entry, setup_ambient_station):
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"devices,error",
|
||||
"devices_response,errors",
|
||||
[
|
||||
(AmbientError, "invalid_key"),
|
||||
(AsyncMock(return_value=[]), "no_devices"),
|
||||
(AsyncMock(side_effect=AmbientError), {"base": "invalid_key"}),
|
||||
(AsyncMock(return_value=[]), {"base": "no_devices"}),
|
||||
],
|
||||
)
|
||||
async def test_errors(hass, config, devices, error, setup_ambient_station):
|
||||
"""Test that various issues show the correct error."""
|
||||
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["errors"] == {"base": error}
|
||||
|
||||
|
||||
async def test_show_form(hass):
|
||||
"""Test that the form is served with no input."""
|
||||
async def test_create_entry(
|
||||
hass, api, config, devices_response, errors, mock_aioambient
|
||||
):
|
||||
"""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 errors that can arise:
|
||||
with patch.object(api, "get_devices", devices_response):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=config
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["errors"] == errors
|
||||
|
||||
async def test_step_user(hass, config, setup_ambient_station):
|
||||
"""Test that the user step works."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
# Test that we can recover and finish the flow after errors occur:
|
||||
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"] == "67890fghij67"
|
||||
@ -55,3 +45,12 @@ async def test_step_user(hass, config, setup_ambient_station):
|
||||
CONF_API_KEY: "12345abcde12345abcde",
|
||||
CONF_APP_KEY: "67890fghij67890fghij",
|
||||
}
|
||||
|
||||
|
||||
async def test_duplicate_error(hass, config, config_entry, setup_config_entry):
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
@ -6,11 +6,11 @@ from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
|
||||
|
||||
async def test_entry_diagnostics(
|
||||
hass, config_entry, hass_client, setup_ambient_station, station_data
|
||||
hass, config_entry, hass_client, data_station, setup_config_entry
|
||||
):
|
||||
"""Test config entry diagnostics."""
|
||||
ambient = hass.data[DOMAIN][config_entry.entry_id]
|
||||
ambient.stations = station_data
|
||||
ambient.stations = data_station
|
||||
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
|
||||
"entry": {
|
||||
"entry_id": config_entry.entry_id,
|
||||
|
Loading…
x
Reference in New Issue
Block a user