Updating tests to support paramterization

This commit is contained in:
Erwin Douna
2023-08-06 09:32:56 +00:00
parent b3c224c525
commit 526d4b2476

View File

@@ -2,6 +2,7 @@
from http import HTTPStatus from http import HTTPStatus
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
import pytest
import requests import requests
from homeassistant import config_entries from homeassistant import config_entries
@@ -78,53 +79,24 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None:
assert result2["errors"] == {"base": "invalid_auth"} assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_key_error(hass: HomeAssistant) -> None: @pytest.mark.parametrize(
"""Test we handle KeyError.""" "error",
result = await hass.config_entries.flow.async_init( [
DOMAIN, context={"source": config_entries.SOURCE_USER} (KeyError, "invalid_auth"),
) (RuntimeError, "cannot_connect"),
(ValueError, "unknown"),
with patch( ],
"homeassistant.components.tado.config_flow.Tado", )
side_effect=KeyError, async def test_form_exceptions(hass: HomeAssistant, error) -> None:
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"username": "test-username", "password": "test-password"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_runtime_error(hass: HomeAssistant) -> None:
"""Test we handle RuntimeError."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.tado.config_flow.Tado",
side_effect=RuntimeError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"username": "test-username", "password": "test-password"},
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_exception(hass: HomeAssistant) -> None:
"""Test we handle Exception.""" """Test we handle Exception."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
exc, base_error = error
with patch( with patch(
"homeassistant.components.tado.config_flow.Tado", "homeassistant.components.tado.config_flow.Tado",
side_effect=Exception, side_effect=exc,
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
@@ -132,7 +104,7 @@ async def test_form_exception(hass: HomeAssistant) -> None:
) )
assert result2["type"] == "form" assert result2["type"] == "form"
assert result2["errors"] == {"base": "unknown"} assert result2["errors"] == {"base": base_error}
async def test_options_flow(hass: HomeAssistant) -> None: async def test_options_flow(hass: HomeAssistant) -> None: