Increase test coverage in apsystems coordinator (#132631)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Thomas55555 2024-12-09 00:20:53 +01:00 committed by GitHub
parent ed938ba315
commit 9f0356fcfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,8 +1,11 @@
"""Test the APSystem setup.""" """Test the APSystem setup."""
import datetime
from unittest.mock import AsyncMock from unittest.mock import AsyncMock
from APsystemsEZ1 import InverterReturnedError from APsystemsEZ1 import InverterReturnedError
from freezegun.api import FrozenDateTimeFactory
import pytest
from homeassistant.components.apsystems.const import DOMAIN from homeassistant.components.apsystems.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
@ -10,16 +13,57 @@ from homeassistant.core import HomeAssistant
from . import setup_integration from . import setup_integration
from tests.common import MockConfigEntry from tests.common import MockConfigEntry, async_fire_time_changed
SCAN_INTERVAL = datetime.timedelta(seconds=12)
async def test_update_failed( @pytest.mark.usefixtures("mock_apsystems")
async def test_load_unload_entry(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test load and unload entry."""
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.config_entries.async_remove(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
async def test_setup_failed(
hass: HomeAssistant, hass: HomeAssistant,
mock_apsystems: AsyncMock, mock_apsystems: AsyncMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test update failed.""" """Test update failed."""
mock_apsystems.get_output_data.side_effect = InverterReturnedError mock_apsystems.get_device_info.side_effect = TimeoutError
await setup_integration(hass, mock_config_entry) await setup_integration(hass, mock_config_entry)
entry = hass.config_entries.async_entries(DOMAIN)[0] entry = hass.config_entries.async_entries(DOMAIN)[0]
assert entry.state is ConfigEntryState.SETUP_RETRY assert entry.state is ConfigEntryState.SETUP_RETRY
async def test_update(
hass: HomeAssistant,
mock_apsystems: AsyncMock,
mock_config_entry: MockConfigEntry,
caplog: pytest.LogCaptureFixture,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test update data with an inverter error and recover."""
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.LOADED
assert "Inverter returned an error" not in caplog.text
mock_apsystems.get_output_data.side_effect = InverterReturnedError
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert "Error fetching APSystems Data data:" in caplog.text
caplog.clear()
mock_apsystems.get_output_data.side_effect = None
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert "Fetching APSystems Data data recovered" in caplog.text