Renovate PurpleAir tests (#84894)

This commit is contained in:
Aaron Bach 2023-01-02 04:55:19 -07:00 committed by GitHub
parent 9af17fa5a0
commit 0a77232444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 71 deletions

View File

@ -6,24 +6,29 @@ from aiopurpleair.models.sensors import GetSensorsResponse
import pytest
from homeassistant.components.purpleair import DOMAIN
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture
TEST_API_KEY = "abcde12345"
TEST_SENSOR_INDEX1 = 123456
TEST_SENSOR_INDEX2 = 567890
@pytest.fixture(name="api")
def api_fixture(check_api_key, get_nearby_sensors, get_sensors):
def api_fixture(get_sensors_response):
"""Define a fixture to return a mocked aiopurple API object."""
api = Mock(async_check_api_key=check_api_key)
api.sensors.async_get_nearby_sensors = get_nearby_sensors
api.sensors.async_get_sensors = get_sensors
return api
@pytest.fixture(name="check_api_key")
def check_api_key_fixture():
"""Define a fixture to mock the method to check an API key's validity."""
return AsyncMock()
return Mock(
async_check_api_key=AsyncMock(),
sensors=Mock(
async_get_nearby_sensors=AsyncMock(
return_value=[
NearbySensorResult(sensor=sensor, distance=1.0)
for sensor in get_sensors_response.data.values()
]
),
async_get_sensors=AsyncMock(return_value=get_sensors_response),
),
)
@pytest.fixture(name="config_entry")
@ -32,7 +37,7 @@ def config_entry_fixture(hass, config_entry_data, config_entry_options):
entry = MockConfigEntry(
domain=DOMAIN,
title="abcde",
unique_id="abcde12345",
unique_id=TEST_API_KEY,
data=config_entry_data,
options=config_entry_options,
)
@ -44,7 +49,7 @@ def config_entry_fixture(hass, config_entry_data, config_entry_options):
def config_entry_data_fixture():
"""Define a config entry data fixture."""
return {
"api_key": "abcde12345",
"api_key": TEST_API_KEY,
}
@ -52,27 +57,10 @@ def config_entry_data_fixture():
def config_entry_options_fixture():
"""Define a config entry options fixture."""
return {
"sensor_indices": [123456],
"sensor_indices": [TEST_SENSOR_INDEX1],
}
@pytest.fixture(name="get_nearby_sensors")
def get_nearby_sensors_fixture(get_sensors_response):
"""Define a mocked API.sensors.async_get_nearby_sensors."""
return AsyncMock(
return_value=[
NearbySensorResult(sensor=sensor, distance=1.0)
for sensor in get_sensors_response.data.values()
]
)
@pytest.fixture(name="get_sensors")
def get_sensors_fixture(get_sensors_response):
"""Define a mocked API.sensors.async_get_sensors."""
return AsyncMock(return_value=get_sensors_response)
@pytest.fixture(name="get_sensors_response", scope="package")
def get_sensors_response_fixture():
"""Define a fixture to mock an aiopurpleair GetSensorsResponse object."""
@ -81,12 +69,18 @@ def get_sensors_response_fixture():
)
@pytest.fixture(name="setup_purpleair")
async def setup_purpleair_fixture(hass, api, config_entry_data):
"""Define a fixture to set up PurpleAir."""
@pytest.fixture(name="mock_aiopurpleair")
async def mock_aiopurpleair_fixture(api):
"""Define a fixture to patch aiopurpleair."""
with patch(
"homeassistant.components.purpleair.config_flow.API", return_value=api
), patch("homeassistant.components.purpleair.coordinator.API", return_value=api):
assert await async_setup_component(hass, DOMAIN, config_entry_data)
await hass.async_block_till_done()
yield
@pytest.fixture(name="setup_config_entry")
async def setup_config_entry_fixture(hass, config_entry, mock_aiopurpleair):
"""Define a fixture to set up purpleair."""
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
yield

View File

@ -9,14 +9,10 @@ from homeassistant.components.purpleair import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
from homeassistant.helpers import device_registry as dr
from .conftest import TEST_API_KEY, TEST_SENSOR_INDEX1, TEST_SENSOR_INDEX2
async def test_duplicate_error(hass, config_entry, setup_purpleair):
"""Test that the proper error is shown when adding a duplicate config entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data={"api_key": "abcde12345"}
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "already_configured"
TEST_LATITUDE = 51.5285582
TEST_LONGITUDE = -0.2416796
@pytest.mark.parametrize(
@ -42,7 +38,7 @@ async def test_create_entry_by_coordinates(
check_api_key_mock,
get_nearby_sensors_errors,
get_nearby_sensors_mock,
setup_purpleair,
mock_aiopurpleair,
):
"""Test creating an entry by entering a latitude/longitude (including errors)."""
result = await hass.config_entries.flow.async_init(
@ -54,13 +50,13 @@ async def test_create_entry_by_coordinates(
# Test errors that can arise when checking the API key:
with patch.object(api, "async_check_api_key", check_api_key_mock):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"api_key": "abcde12345"}
result["flow_id"], user_input={"api_key": TEST_API_KEY}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == check_api_key_errors
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"api_key": "abcde12345"}
result["flow_id"], user_input={"api_key": TEST_API_KEY}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "by_coordinates"
@ -70,8 +66,8 @@ async def test_create_entry_by_coordinates(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
"latitude": 51.5285582,
"longitude": -0.2416796,
"latitude": TEST_LATITUDE,
"longitude": TEST_LONGITUDE,
"distance": 5,
},
)
@ -81,8 +77,8 @@ async def test_create_entry_by_coordinates(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
"latitude": 51.5285582,
"longitude": -0.2416796,
"latitude": TEST_LATITUDE,
"longitude": TEST_LONGITUDE,
"distance": 5,
},
)
@ -92,19 +88,28 @@ async def test_create_entry_by_coordinates(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
"sensor_index": "123456",
"sensor_index": str(TEST_SENSOR_INDEX1),
},
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "abcde"
assert result["data"] == {
"api_key": "abcde12345",
"api_key": TEST_API_KEY,
}
assert result["options"] == {
"sensor_indices": [123456],
"sensor_indices": [TEST_SENSOR_INDEX1],
}
async def test_duplicate_error(hass, config_entry, setup_config_entry):
"""Test that the proper error is shown when adding a duplicate config entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data={"api_key": TEST_API_KEY}
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "already_configured"
@pytest.mark.parametrize(
"check_api_key_mock,check_api_key_errors",
[
@ -114,7 +119,12 @@ async def test_create_entry_by_coordinates(
],
)
async def test_reauth(
hass, api, check_api_key_errors, check_api_key_mock, config_entry, setup_purpleair
hass,
api,
check_api_key_errors,
check_api_key_mock,
config_entry,
setup_config_entry,
):
"""Test re-auth (including errors)."""
result = await hass.config_entries.flow.async_init(
@ -124,7 +134,7 @@ async def test_reauth(
"entry_id": config_entry.entry_id,
"unique_id": config_entry.unique_id,
},
data={"api_key": "abcde12345"},
data={"api_key": TEST_API_KEY},
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
@ -160,7 +170,7 @@ async def test_options_add_sensor(
config_entry,
get_nearby_sensors_errors,
get_nearby_sensors_mock,
setup_purpleair,
setup_config_entry,
):
"""Test adding a sensor via the options flow (including errors)."""
result = await hass.config_entries.options.async_init(config_entry.entry_id)
@ -178,8 +188,8 @@ async def test_options_add_sensor(
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"latitude": 51.5285582,
"longitude": -0.2416796,
"latitude": TEST_LATITUDE,
"longitude": TEST_LONGITUDE,
"distance": 5,
},
)
@ -189,8 +199,8 @@ async def test_options_add_sensor(
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"latitude": 51.5285582,
"longitude": -0.2416796,
"latitude": TEST_LATITUDE,
"longitude": TEST_LONGITUDE,
"distance": 5,
},
)
@ -200,19 +210,22 @@ async def test_options_add_sensor(
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"sensor_index": "567890",
"sensor_index": str(TEST_SENSOR_INDEX2),
},
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["data"] == {
"last_update_sensor_add": True,
"sensor_indices": [123456, 567890],
"sensor_indices": [TEST_SENSOR_INDEX1, TEST_SENSOR_INDEX2],
}
assert config_entry.options["sensor_indices"] == [123456, 567890]
assert config_entry.options["sensor_indices"] == [
TEST_SENSOR_INDEX1,
TEST_SENSOR_INDEX2,
]
async def test_options_add_sensor_duplicate(hass, config_entry, setup_purpleair):
async def test_options_add_sensor_duplicate(hass, config_entry, setup_config_entry):
"""Test adding a duplicate sensor via the options flow."""
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.FlowResultType.MENU
@ -227,8 +240,8 @@ async def test_options_add_sensor_duplicate(hass, config_entry, setup_purpleair)
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"latitude": 51.5285582,
"longitude": -0.2416796,
"latitude": TEST_LATITUDE,
"longitude": TEST_LONGITUDE,
"distance": 5,
},
)
@ -238,14 +251,14 @@ async def test_options_add_sensor_duplicate(hass, config_entry, setup_purpleair)
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"sensor_index": "123456",
"sensor_index": str(TEST_SENSOR_INDEX1),
},
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_options_remove_sensor(hass, config_entry, setup_purpleair):
async def test_options_remove_sensor(hass, config_entry, setup_config_entry):
"""Test removing a sensor via the options flow."""
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.FlowResultType.MENU
@ -258,7 +271,7 @@ async def test_options_remove_sensor(hass, config_entry, setup_purpleair):
assert result["step_id"] == "remove_sensor"
device_registry = dr.async_get(hass)
device_entry = device_registry.async_get_device({(DOMAIN, "123456")})
device_entry = device_registry.async_get_device({(DOMAIN, str(TEST_SENSOR_INDEX1))})
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"sensor_device_id": device_entry.id},

View File

@ -4,7 +4,7 @@ from homeassistant.components.diagnostics import REDACTED
from tests.components.diagnostics import get_diagnostics_for_config_entry
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_purpleair):
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_config_entry):
"""Test config entry diagnostics."""
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
"entry": {