Files
core/tests/components/nmbs/test_init.py
T

185 lines
6.3 KiB
Python

"""Test the NMBS integration setup."""
import asyncio
from unittest.mock import AsyncMock
from pyrail import ApiUnavailableError
from pyrail.models import StationsApiResponse
import pytest
from pytest_homeassistant.common import MockConfigEntry
from homeassistant.components.nmbs.const import (
CONF_STATION_FROM,
CONF_STATION_TO,
DOMAIN,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
async def test_setup_entry(
hass: HomeAssistant,
mock_nmbs_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test a config entry is set up successfully."""
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
@pytest.mark.parametrize(
("side_effect", "return_value"),
[
pytest.param(
ApiUnavailableError("The iRail API is unavailable"),
None,
id="api_unavailable",
),
pytest.param(None, None, id="station_list_not_modified"),
pytest.param(
None,
StationsApiResponse.from_dict(
{"version": "1.3", "timestamp": "0", "station": []}
),
id="empty_station_list",
),
],
)
async def test_setup_retry_after_failed_fetch(
hass: HomeAssistant,
mock_nmbs_client: AsyncMock,
mock_config_entry: MockConfigEntry,
side_effect: Exception | None,
return_value: StationsApiResponse | None,
) -> None:
"""Test the entry is retried and refetches the station list after a failure."""
stations_response = mock_nmbs_client.get_stations.return_value
mock_nmbs_client.get_stations.side_effect = side_effect
mock_nmbs_client.get_stations.return_value = return_value
mock_config_entry.add_to_hass(hass)
assert not await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
mock_nmbs_client.get_stations.side_effect = None
mock_nmbs_client.get_stations.return_value = stations_response
await hass.config_entries.async_reload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
assert mock_nmbs_client.get_stations.call_count == 2
@pytest.mark.parametrize(
("station_from", "station_to", "missing_station"),
[
pytest.param(
"BE.NMBS.000000000",
"BE.NMBS.008814001",
"BE.NMBS.000000000",
id="departure",
),
pytest.param(
"BE.NMBS.008812005", "BE.NMBS.000000000", "BE.NMBS.000000000", id="arrival"
),
],
)
@pytest.mark.usefixtures("mock_nmbs_client")
async def test_setup_entry_unknown_station(
hass: HomeAssistant,
station_from: str,
station_to: str,
missing_station: str,
) -> None:
"""Test the entry errors when a configured station is not in the station list."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Train from an unknown station",
data={CONF_STATION_FROM: station_from, CONF_STATION_TO: station_to},
unique_id=f"{station_from}_{station_to}",
)
entry.add_to_hass(hass)
assert not await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.SETUP_ERROR
assert entry.error_reason_translation_key == "station_not_found"
assert entry.error_reason_translation_placeholders == {"station": missing_station}
async def test_second_entry_reuses_station_list(
hass: HomeAssistant,
mock_nmbs_client: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_second_config_entry: MockConfigEntry,
) -> None:
"""Test that a second entry reuses the station list of a loaded entry."""
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
mock_second_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_second_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_second_config_entry.state is ConfigEntryState.LOADED
# The two entries use the same stations swapped, so resolving both against
# the same shared list yields the very same station objects.
assert (
mock_second_config_entry.runtime_data.station_from
is mock_config_entry.runtime_data.station_to
)
mock_nmbs_client.get_stations.assert_called_once()
async def test_concurrent_setup_shares_station_fetch(
hass: HomeAssistant,
mock_nmbs_client: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_second_config_entry: MockConfigEntry,
) -> None:
"""Test that entries set up concurrently share one in-flight station fetch."""
stations_response = mock_nmbs_client.get_stations.return_value
release_fetch = asyncio.Event()
async def _blocked_get_stations() -> StationsApiResponse:
await release_fetch.wait()
return stations_response
mock_nmbs_client.get_stations.side_effect = _blocked_get_stations
mock_config_entry.add_to_hass(hass)
mock_second_config_entry.add_to_hass(hass)
# Setting up one entry sets up the integration, which sets up both entries.
setup_task = hass.async_create_task(
hass.config_entries.async_setup(mock_config_entry.entry_id)
)
# Release the fetch only after both entry setups are underway, so the
# second entry sees the first entry's fetch in flight rather than a
# completed task.
async with asyncio.timeout(10):
while (
mock_config_entry.state is not ConfigEntryState.SETUP_IN_PROGRESS
or mock_second_config_entry.state is not ConfigEntryState.SETUP_IN_PROGRESS
):
await asyncio.sleep(0)
release_fetch.set()
assert await setup_task
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
assert mock_second_config_entry.state is ConfigEntryState.LOADED
assert (
mock_second_config_entry.runtime_data.station_from
is mock_config_entry.runtime_data.station_to
)
mock_nmbs_client.get_stations.assert_called_once()