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:
Aaron Bach 2022-12-30 17:09:38 -07:00 committed by GitHub
parent 9b3d727e8e
commit f8467d253e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 49 deletions

View File

@ -1,16 +1,21 @@
"""Define test fixtures for Ambient PWS.""" """Define test fixtures for Ambient PWS."""
import json import json
from unittest.mock import patch from unittest.mock import AsyncMock, Mock, patch
import pytest import pytest
from homeassistant.components.ambient_station.const import CONF_APP_KEY, DOMAIN from homeassistant.components.ambient_station.const import CONF_APP_KEY, DOMAIN
from homeassistant.const import CONF_API_KEY from homeassistant.const import CONF_API_KEY
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture 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") @pytest.fixture(name="config")
def config_fixture(hass): def config_fixture(hass):
"""Define a config entry data fixture.""" """Define a config entry data fixture."""
@ -28,27 +33,31 @@ def config_entry_fixture(hass, config):
return entry return entry
@pytest.fixture(name="devices", scope="package") @pytest.fixture(name="data_devices", scope="package")
def devices_fixture(): def data_devices_fixture():
"""Define devices data.""" """Define devices data."""
return json.loads(load_fixture("devices.json", "ambient_station")) return json.loads(load_fixture("devices.json", "ambient_station"))
@pytest.fixture(name="setup_ambient_station") @pytest.fixture(name="data_station", scope="package")
async def setup_ambient_station_fixture(hass, config, devices): def data_station_fixture():
"""Define a fixture to set up AirVisual.""" """Define station data."""
with patch("homeassistant.components.ambient_station.PLATFORMS", []), patch( return json.loads(load_fixture("station_data.json", "ambient_station"))
"homeassistant.components.ambient_station.config_flow.API.get_devices",
side_effect=devices,
), patch("aioambient.api.API.get_devices", side_effect=devices), patch( @pytest.fixture(name="mock_aioambient")
"aioambient.websocket.Websocket.connect" async def mock_aioambient_fixture(api):
): """Define a fixture to patch aioambient."""
assert await async_setup_component(hass, DOMAIN, config) with patch(
await hass.async_block_till_done() "homeassistant.components.ambient_station.config_flow.API",
return_value=api,
), patch("aioambient.websocket.Websocket.connect"):
yield yield
@pytest.fixture(name="station_data", scope="package") @pytest.fixture(name="setup_config_entry")
def station_data_fixture(): async def setup_config_entry_fixture(hass, config_entry, mock_aioambient):
"""Define devices data.""" """Define a fixture to set up ambient_station."""
return json.loads(load_fixture("station_data.json", "ambient_station")) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
yield

View File

@ -1,5 +1,5 @@
"""Define tests for the Ambient PWS config flow.""" """Define tests for the Ambient PWS config flow."""
from unittest.mock import AsyncMock from unittest.mock import AsyncMock, patch
from aioambient.errors import AmbientError from aioambient.errors import AmbientError
import pytest import pytest
@ -10,44 +10,34 @@ from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_API_KEY 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( @pytest.mark.parametrize(
"devices,error", "devices_response,errors",
[ [
(AmbientError, "invalid_key"), (AsyncMock(side_effect=AmbientError), {"base": "invalid_key"}),
(AsyncMock(return_value=[]), "no_devices"), (AsyncMock(return_value=[]), {"base": "no_devices"}),
], ],
) )
async def test_errors(hass, config, devices, error, setup_ambient_station): async def test_create_entry(
"""Test that various issues show the correct error.""" hass, api, config, devices_response, errors, mock_aioambient
result = await hass.config_entries.flow.async_init( ):
DOMAIN, context={"source": SOURCE_USER}, data=config """Test creating an entry."""
)
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."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
) )
assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user" 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 we can recover and finish the flow after errors occur:
"""Test that the user step works.""" result = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_init( result["flow_id"], user_input=config
DOMAIN, context={"source": SOURCE_USER}, data=config
) )
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "67890fghij67" assert result["title"] == "67890fghij67"
@ -55,3 +45,12 @@ async def test_step_user(hass, config, setup_ambient_station):
CONF_API_KEY: "12345abcde12345abcde", CONF_API_KEY: "12345abcde12345abcde",
CONF_APP_KEY: "67890fghij67890fghij", 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"

View File

@ -6,11 +6,11 @@ from tests.components.diagnostics import get_diagnostics_for_config_entry
async def test_entry_diagnostics( 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.""" """Test config entry diagnostics."""
ambient = hass.data[DOMAIN][config_entry.entry_id] 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) == { assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
"entry": { "entry": {
"entry_id": config_entry.entry_id, "entry_id": config_entry.entry_id,