Refactor discovergy config flow test to use parametrize (#100115)

* Refactor discovergy config flow test to use parametrize

* Formatting

* Implement code review sugesstions
pull/100318/head
Jan-Philipp Benecke 2023-09-11 21:53:07 +02:00 committed by GitHub
parent 13cd873e38
commit 5a56adb3f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 63 deletions

View File

@ -2,6 +2,7 @@
from unittest.mock import Mock, patch
from pydiscovergy.error import DiscovergyClientError, HTTPError, InvalidLogin
import pytest
from homeassistant import data_entry_flow
from homeassistant.components.discovergy.const import DOMAIN
@ -73,17 +74,27 @@ async def test_reauth(
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
"""Test we handle invalid auth."""
@pytest.mark.parametrize(
("error", "message"),
[
(InvalidLogin, "invalid_auth"),
(HTTPError, "cannot_connect"),
(DiscovergyClientError, "cannot_connect"),
(Exception, "unknown"),
],
)
async def test_form_fail(hass: HomeAssistant, error: Exception, message: str) -> None:
"""Test to handle exceptions."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
with patch(
"pydiscovergy.Discovergy.meters",
side_effect=InvalidLogin,
side_effect=error,
):
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_EMAIL: "test@example.com",
@ -91,62 +102,5 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None:
},
)
assert result2["type"] == data_entry_flow.FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
with patch("pydiscovergy.Discovergy.meters", side_effect=HTTPError):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_EMAIL: "test@example.com",
CONF_PASSWORD: "test-password",
},
)
assert result2["type"] == data_entry_flow.FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_client_error(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
with patch("pydiscovergy.Discovergy.meters", side_effect=DiscovergyClientError):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_EMAIL: "test@example.com",
CONF_PASSWORD: "test-password",
},
)
assert result2["type"] == data_entry_flow.FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_unknown_exception(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
with patch("pydiscovergy.Discovergy.meters", side_effect=Exception):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_EMAIL: "test@example.com",
CONF_PASSWORD: "test-password",
},
)
assert result2["type"] == data_entry_flow.FlowResultType.FORM
assert result2["errors"] == {"base": "unknown"}
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == {"base": message}