-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase test coverage in apsystems coordinator (#132631)
Co-authored-by: Joost Lekkerkerker <[email protected]>
- Loading branch information
1 parent
ed938ba
commit 9f0356f
Showing
1 changed file
with
47 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,69 @@ | ||
"""Test the APSystem setup.""" | ||
|
||
import datetime | ||
from unittest.mock import AsyncMock | ||
|
||
from APsystemsEZ1 import InverterReturnedError | ||
from freezegun.api import FrozenDateTimeFactory | ||
import pytest | ||
|
||
from homeassistant.components.apsystems.const import DOMAIN | ||
from homeassistant.config_entries import ConfigEntryState | ||
from homeassistant.core import HomeAssistant | ||
|
||
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, | ||
mock_apsystems: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""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) | ||
entry = hass.config_entries.async_entries(DOMAIN)[0] | ||
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 |