Add unit tests

This commit is contained in:
Jules 2023-12-27 11:17:24 +01:00
parent 0a82f58fb5
commit 1ad42f5d7f
Signed by: jdejaegh
GPG key ID: 99D6D184CA66933A
6 changed files with 1809 additions and 0 deletions

6
setup.cfg Normal file
View file

@ -0,0 +1,6 @@
[tool:pytest]
testpaths = tests
norecursedirs = .git
addopts =
--cov=custom_components
asyncio_mode = auto

2
tests/__init__.py Normal file
View file

@ -0,0 +1,2 @@
"""Tests for the IRM KMI custom integration."""
# Test suite inspired by https://github.com/home-assistant/core/tree/dev/tests/components/open_meteo

69
tests/conftest.py Normal file
View file

@ -0,0 +1,69 @@
"""Fixtures for the IRM KMI integration tests."""
from __future__ import annotations
import json
from collections.abc import Generator
from unittest.mock import MagicMock, patch
import pytest
from homeassistant.const import CONF_ZONE
from pytest_homeassistant_custom_component.common import (MockConfigEntry,
load_fixture)
from custom_components.irm_kmi.api import IrmKmiApiParametersError
from custom_components.irm_kmi.const import DOMAIN
@pytest.fixture(autouse=True)
def auto_enable_custom_integrations(enable_custom_integrations):
yield
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
title="Home",
domain=DOMAIN,
data={CONF_ZONE: "zone.home"},
unique_id="zone.home",
)
@pytest.fixture
def mock_setup_entry() -> Generator[None, None, None]:
"""Mock setting up a config entry."""
with patch(
"custom_components.irm_kmi.async_setup_entry", return_value=True
):
yield
@pytest.fixture()
def mock_irm_kmi_api(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
"""Return a mocked IrmKmi api client."""
fixture: str = "forecast.json"
forecast = json.loads(load_fixture(fixture))
print(type(forecast))
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_forecasts_coord.return_value = forecast
yield irm_kmi
@pytest.fixture()
def mock_exception_irm_kmi_api(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
"""Return a mocked IrmKmi api client."""
fixture: str = "forecast.json"
forecast = json.loads(load_fixture(fixture))
print(type(forecast))
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_forecasts_coord.side_effect = IrmKmiApiParametersError
yield irm_kmi

1625
tests/fixtures/forecast.json vendored Normal file

File diff suppressed because it is too large Load diff

33
tests/test_config_flow.py Normal file
View file

@ -0,0 +1,33 @@
"""Tests for the Open-Meteo config flow."""
from unittest.mock import MagicMock
from homeassistant.components.zone import ENTITY_ID_HOME
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_ZONE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from custom_components.irm_kmi.const import DOMAIN
async def test_full_user_flow(
hass: HomeAssistant,
mock_setup_entry: MagicMock,
) -> None:
"""Test the full user configuration flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result.get("type") == FlowResultType.FORM
assert result.get("step_id") == "user"
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_ZONE: ENTITY_ID_HOME},
)
assert result2.get("type") == FlowResultType.CREATE_ENTRY
assert result2.get("title") == "IRM KMI"
assert result2.get("data") == {CONF_ZONE: ENTITY_ID_HOME}

74
tests/test_init.py Normal file
View file

@ -0,0 +1,74 @@
"""Tests for the Open-Meteo integration."""
from unittest.mock import AsyncMock
import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_ZONE
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry
from custom_components.irm_kmi.const import DOMAIN
async def test_load_unload_config_entry(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_irm_kmi_api: AsyncMock,
) -> None:
"""Test the Open-Meteo configuration entry loading/unloading."""
hass.states.async_set(
"zone.home",
0,
{"latitude": 50.738681639, "longitude": 4.054077148},
)
mock_config_entry.add_to_hass(hass)
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
await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert not hass.data.get(DOMAIN)
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
async def test_config_entry_not_ready(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_exception_irm_kmi_api: AsyncMock
) -> None:
"""Test the Open-Meteo configuration entry not ready."""
hass.states.async_set(
"zone.home",
"zoning",
{"latitude": 50.738681639, "longitude": 4.054077148},
)
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_exception_irm_kmi_api.get_forecasts_coord.call_count == 1
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_config_entry_zone_removed(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the Open-Meteo configuration entry not ready."""
mock_config_entry = MockConfigEntry(
title="My Castle",
domain=DOMAIN,
data={CONF_ZONE: "zone.castle"},
unique_id="zone.castle",
)
mock_config_entry.add_to_hass(hass)
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
assert "Zone 'zone.castle' not found" in caplog.text