mirror of
https://github.com/jdejaegh/irm-kmi-ha.git
synced 2025-06-27 11:39:26 +02:00
Add test for pollen data fetching
This commit is contained in:
parent
adc9e5d013
commit
8a93adb053
2 changed files with 60 additions and 0 deletions
|
@ -196,6 +196,32 @@ def mock_image_and_high_temp_irm_kmi_api(request: pytest.FixtureRequest) -> Gene
|
||||||
yield irm_kmi
|
yield irm_kmi
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def mock_svg_pollen(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
|
||||||
|
"""Return a mocked IrmKmi api client."""
|
||||||
|
fixture: str = "pollen.svg"
|
||||||
|
|
||||||
|
svg_str = load_fixture(fixture)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"custom_components.irm_kmi.coordinator.IrmKmiApiClient", autospec=True
|
||||||
|
) as irm_kmi_api_mock:
|
||||||
|
irm_kmi = irm_kmi_api_mock.return_value
|
||||||
|
irm_kmi.get_svg.return_value = svg_str
|
||||||
|
yield irm_kmi
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def mock_exception_irm_kmi_api_svg_pollen(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
|
||||||
|
"""Return a mocked IrmKmi api client."""
|
||||||
|
with patch(
|
||||||
|
"custom_components.irm_kmi.coordinator.IrmKmiApiClient", autospec=True
|
||||||
|
) as irm_kmi_api_mock:
|
||||||
|
irm_kmi = irm_kmi_api_mock.return_value
|
||||||
|
irm_kmi.get_svg.side_effect = IrmKmiApiParametersError
|
||||||
|
yield irm_kmi
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def mock_coordinator(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
|
def mock_coordinator(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
|
||||||
"""Return a mocked coordinator."""
|
"""Return a mocked coordinator."""
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
||||||
|
|
||||||
|
from custom_components.irm_kmi import IrmKmiCoordinator
|
||||||
from custom_components.irm_kmi.pollen import PollenParser
|
from custom_components.irm_kmi.pollen import PollenParser
|
||||||
|
from tests.conftest import get_api_data
|
||||||
|
|
||||||
|
|
||||||
def test_svg_pollen_parsing():
|
def test_svg_pollen_parsing():
|
||||||
|
@ -16,3 +23,30 @@ def test_pollen_options():
|
||||||
def test_pollen_default_values():
|
def test_pollen_default_values():
|
||||||
assert PollenParser.get_default_data() == {'birch': 'none', 'oak': 'none', 'hazel': 'none', 'mugwort': 'none',
|
assert PollenParser.get_default_data() == {'birch': 'none', 'oak': 'none', 'hazel': 'none', 'mugwort': 'none',
|
||||||
'alder': 'none', 'grasses': 'none', 'ash': 'none'}
|
'alder': 'none', 'grasses': 'none', 'ash': 'none'}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_pollen_data_from_api(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_svg_pollen: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry
|
||||||
|
) -> None:
|
||||||
|
coordinator = IrmKmiCoordinator(hass, mock_config_entry)
|
||||||
|
api_data = get_api_data("be_forecast_warning.json")
|
||||||
|
|
||||||
|
result = await coordinator._async_pollen_data(api_data)
|
||||||
|
expected = {'mugwort': 'none', 'birch': 'purple', 'alder': 'green', 'ash': 'active', 'oak': 'active',
|
||||||
|
'grasses': 'none', 'hazel': 'none'}
|
||||||
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
|
async def test_pollen_error_leads_to_default_values(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_exception_irm_kmi_api_svg_pollen: AsyncMock
|
||||||
|
) -> None:
|
||||||
|
coordinator = IrmKmiCoordinator(hass, mock_config_entry)
|
||||||
|
api_data = get_api_data("be_forecast_warning.json")
|
||||||
|
|
||||||
|
result = await coordinator._async_pollen_data(api_data)
|
||||||
|
expected = PollenParser.get_default_data()
|
||||||
|
assert result == expected
|
||||||
|
|
Loading…
Add table
Reference in a new issue