Increase test coverage in ista EcoTrend integration (#143426)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Manu 2025-04-22 13:37:13 +02:00 committed by GitHub
parent 159e55296f
commit 6c7317fbc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 87 additions and 12 deletions

View File

@ -6,9 +6,7 @@ rules:
appropriate-polling: done
brands: done
common-modules: done
config-flow-test-coverage:
status: todo
comment: test_form/docstrings outdated, test already_configuret, test abort conditions in reauth,
config-flow-test-coverage: done
config-flow: done
dependency-transparency: done
docs-actions:

View File

@ -80,7 +80,7 @@ def mock_ista() -> Generator[MagicMock]:
"26e93f1a-c828-11ea-87d0-0242ac130003",
"eaf5c5c8-889f-4a3c-b68c-e9a676505762",
]
client.get_consumption_data = get_consumption_data
client.get_consumption_data.return_value = get_consumption_data()
yield client

View File

@ -11,6 +11,8 @@ from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("mock_ista")
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
@ -47,14 +49,14 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
(IndexError, "unknown"),
],
)
async def test_form_invalid_auth(
async def test_form_error_and_recover(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_ista: MagicMock,
side_effect: Exception,
error_text: str,
) -> None:
"""Test we handle invalid auth."""
"""Test config flow error and recover."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
@ -89,10 +91,10 @@ async def test_form_invalid_auth(
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.usefixtures("mock_ista")
async def test_reauth(
hass: HomeAssistant,
ista_config_entry: AsyncMock,
mock_ista: MagicMock,
ista_config_entry: MockConfigEntry,
) -> None:
"""Test reauth flow."""
@ -131,12 +133,12 @@ async def test_reauth(
)
async def test_reauth_error_and_recover(
hass: HomeAssistant,
ista_config_entry: AsyncMock,
ista_config_entry: MockConfigEntry,
mock_ista: MagicMock,
side_effect: Exception,
error_text: str,
) -> None:
"""Test reauth flow."""
"""Test reauth flow error and recover."""
ista_config_entry.add_to_hass(hass)
@ -174,3 +176,31 @@ async def test_reauth_error_and_recover(
CONF_PASSWORD: "new-password",
}
assert len(hass.config_entries.async_entries()) == 1
@pytest.mark.usefixtures("mock_ista")
async def test_form__already_configured(
hass: HomeAssistant,
ista_config_entry: MockConfigEntry,
) -> None:
"""Test we abort form login when entry is already configured."""
ista_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_EMAIL: "new@example.com",
CONF_PASSWORD: "new-password",
},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"

View File

@ -1,11 +1,12 @@
"""Test the ista EcoTrend init."""
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
from pyecotrend_ista import KeycloakError, LoginError, ParserError, ServerError
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.ista_ecotrend.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
@ -60,7 +61,7 @@ async def test_config_entry_auth_failed(
mock_ista: MagicMock,
side_effect: Exception,
) -> None:
"""Test config entry not ready."""
"""Test config entry auth failed."""
mock_ista.login.side_effect = side_effect
ista_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(ista_config_entry.entry_id)
@ -88,3 +89,49 @@ async def test_device_registry(
device_registry, ista_config_entry.entry_id
):
assert device == snapshot
async def test_update_failed(
hass: HomeAssistant,
ista_config_entry: MockConfigEntry,
mock_ista: MagicMock,
) -> None:
"""Test coordinator update failed."""
with patch(
"homeassistant.components.ista_ecotrend.PLATFORMS",
[],
):
mock_ista.get_consumption_data.side_effect = ServerError
ista_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(ista_config_entry.entry_id)
await hass.async_block_till_done()
assert ista_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_auth_failed(
hass: HomeAssistant, ista_config_entry: MockConfigEntry, mock_ista: MagicMock
) -> None:
"""Test coordinator auth failed and reauth flow started."""
with patch(
"homeassistant.components.ista_ecotrend.PLATFORMS",
[],
):
mock_ista.get_consumption_data.side_effect = LoginError
ista_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(ista_config_entry.entry_id)
await hass.async_block_till_done()
assert ista_config_entry.state is ConfigEntryState.SETUP_ERROR
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
flow = flows[0]
assert flow.get("step_id") == "reauth_confirm"
assert flow.get("handler") == DOMAIN
assert "context" in flow
assert flow["context"].get("source") == SOURCE_REAUTH
assert flow["context"].get("entry_id") == ista_config_entry.entry_id