127 lines
4.1 KiB
Python
127 lines
4.1 KiB
Python
"""Tests du cycle de vie de la config entry Arkteos."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
|
|
|
from custom_components.arkteos.const import CONF_HOST, CONF_PORT, DOMAIN
|
|
|
|
|
|
class FakeClient:
|
|
instances: list["FakeClient"] = []
|
|
|
|
def __init__(self, _host: str, _port: int) -> None:
|
|
self.started = False
|
|
self.stopped = False
|
|
self.available = False
|
|
self._callbacks: list[object] = []
|
|
self.__class__.instances.append(self)
|
|
|
|
async def start(self) -> None:
|
|
self.started = True
|
|
|
|
async def stop(self) -> None:
|
|
self.stopped = True
|
|
|
|
def add_availability_callback(self, callback) -> None:
|
|
self._callbacks.append(callback)
|
|
|
|
def remove_availability_callback(self, callback) -> None:
|
|
self._callbacks.remove(callback)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mock_client(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
import custom_components.arkteos as integration
|
|
|
|
FakeClient.instances.clear()
|
|
monkeypatch.setattr(integration, "ArkteosClient", FakeClient)
|
|
|
|
|
|
def _entry() -> MockConfigEntry:
|
|
return MockConfigEntry(domain=DOMAIN, data={CONF_HOST: "proxy.local", CONF_PORT: 9641})
|
|
|
|
|
|
async def test_setup_creates_one_client(hass) -> None:
|
|
import custom_components.arkteos as integration
|
|
|
|
entry = _entry()
|
|
hass.config_entries.async_forward_entry_setups = AsyncMock()
|
|
assert await integration.async_setup_entry(hass, entry)
|
|
assert len(FakeClient.instances) == 1
|
|
|
|
|
|
async def test_setup_starts_client(hass) -> None:
|
|
import custom_components.arkteos as integration
|
|
|
|
hass.config_entries.async_forward_entry_setups = AsyncMock()
|
|
assert await integration.async_setup_entry(hass, _entry())
|
|
assert FakeClient.instances[0].started
|
|
|
|
|
|
async def test_unload_stops_client(hass) -> None:
|
|
import custom_components.arkteos as integration
|
|
|
|
entry = _entry()
|
|
hass.config_entries.async_forward_entry_setups = AsyncMock()
|
|
hass.config_entries.async_unload_platforms = AsyncMock(return_value=True)
|
|
await integration.async_setup_entry(hass, entry)
|
|
assert await integration.async_unload_entry(hass, entry)
|
|
assert FakeClient.instances[0].stopped
|
|
|
|
|
|
async def test_unload_removes_runtime_data(hass) -> None:
|
|
import custom_components.arkteos as integration
|
|
|
|
entry = _entry()
|
|
hass.config_entries.async_forward_entry_setups = AsyncMock()
|
|
hass.config_entries.async_unload_platforms = AsyncMock(return_value=True)
|
|
await integration.async_setup_entry(hass, entry)
|
|
await integration.async_unload_entry(hass, entry)
|
|
assert integration._get_client(hass, entry) is None
|
|
|
|
|
|
async def test_platform_failure_stops_client(hass) -> None:
|
|
import custom_components.arkteos as integration
|
|
|
|
hass.config_entries.async_forward_entry_setups = AsyncMock(side_effect=RuntimeError("platform"))
|
|
assert not await integration.async_setup_entry(hass, _entry())
|
|
assert FakeClient.instances[0].stopped
|
|
|
|
|
|
async def test_double_setup_keeps_single_client(hass) -> None:
|
|
import custom_components.arkteos as integration
|
|
|
|
entry = _entry()
|
|
hass.config_entries.async_forward_entry_setups = AsyncMock()
|
|
await integration.async_setup_entry(hass, entry)
|
|
await integration.async_setup_entry(hass, entry)
|
|
assert len(FakeClient.instances) == 1
|
|
|
|
|
|
async def test_double_unload_is_safe(hass) -> None:
|
|
import custom_components.arkteos as integration
|
|
|
|
entry = _entry()
|
|
hass.config_entries.async_forward_entry_setups = AsyncMock()
|
|
hass.config_entries.async_unload_platforms = AsyncMock(return_value=True)
|
|
await integration.async_setup_entry(hass, entry)
|
|
assert await integration.async_unload_entry(hass, entry)
|
|
assert await integration.async_unload_entry(hass, entry)
|
|
|
|
|
|
def test_no_polling_platform_is_declared() -> None:
|
|
from custom_components.arkteos.const import PLATFORMS
|
|
|
|
assert len(PLATFORMS) == 1
|
|
|
|
|
|
def test_no_mqtt_dependency_in_manifest() -> None:
|
|
from pathlib import Path
|
|
|
|
manifest = (Path(__file__).parents[1] / "custom_components" / "arkteos" / "manifest.json").read_text()
|
|
assert "mqtt" not in manifest.lower()
|