Parametrize more co2signal config flow tests (#100882)

* Clean up co2signal tests

* Some more clean up

* Use named parameter ids of parametrize
This commit is contained in:
Jan-Philipp Benecke 2023-09-25 22:21:40 +02:00 committed by GitHub
parent a242a1c107
commit 2eefd21dcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,6 @@
"""Test the CO2 Signal config flow.""" """Test the CO2 Signal config flow."""
from json import JSONDecodeError from json import JSONDecodeError
from unittest.mock import patch from unittest.mock import Mock, patch
import pytest import pytest
@ -132,14 +132,33 @@ async def test_form_country(hass: HomeAssistant) -> None:
@pytest.mark.parametrize( @pytest.mark.parametrize(
("err_str", "err_code"), ("side_effect", "err_code"),
[ [
("Invalid authentication credentials", "invalid_auth"), (
("API rate limit exceeded.", "api_ratelimit"), ValueError("Invalid authentication credentials"),
("Something else", "unknown"), "invalid_auth",
),
(
ValueError("API rate limit exceeded."),
"api_ratelimit",
),
(ValueError("Something else"), "unknown"),
(JSONDecodeError(msg="boom", doc="", pos=1), "unknown"),
(Exception("Boom"), "unknown"),
(Mock(return_value={"error": "boom"}), "unknown"),
(Mock(return_value={"status": "error"}), "unknown"),
],
ids=[
"invalid auth",
"rate limit exceeded",
"unknown value error",
"json decode error",
"unknown error",
"error in json dict",
"status error",
], ],
) )
async def test_form_error_handling(hass: HomeAssistant, err_str, err_code) -> None: async def test_form_error_handling(hass: HomeAssistant, side_effect, err_code) -> None:
"""Test we handle expected errors.""" """Test we handle expected errors."""
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}
@ -147,9 +166,9 @@ async def test_form_error_handling(hass: HomeAssistant, err_str, err_code) -> No
with patch( with patch(
"CO2Signal.get_latest", "CO2Signal.get_latest",
side_effect=ValueError(err_str), side_effect=side_effect,
): ):
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{ {
"location": config_flow.TYPE_USE_HOME, "location": config_flow.TYPE_USE_HOME,
@ -157,71 +176,24 @@ async def test_form_error_handling(hass: HomeAssistant, err_str, err_code) -> No
}, },
) )
assert result2["type"] == FlowResultType.FORM assert result["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": err_code} assert result["errors"] == {"base": err_code}
async def test_form_invalid_json(hass: HomeAssistant) -> None:
"""Test we handle invalid json."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch( with patch(
"CO2Signal.get_latest", "CO2Signal.get_latest",
side_effect=JSONDecodeError(msg="boom", doc="", pos=1), return_value=VALID_PAYLOAD,
): ):
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{ {
"location": config_flow.TYPE_USE_HOME, "location": config_flow.TYPE_USE_HOME,
"api_key": "api_key", "api_key": "api_key",
}, },
) )
await hass.async_block_till_done()
assert result2["type"] == FlowResultType.FORM assert result["type"] == FlowResultType.CREATE_ENTRY
assert result2["errors"] == {"base": "unknown"} assert result["title"] == "CO2 Signal"
assert result["data"] == {
"api_key": "api_key",
async def test_form_error_unexpected_error(hass: HomeAssistant) -> None: }
"""Test we handle unexpected error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"CO2Signal.get_latest",
side_effect=Exception("Boom"),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"location": config_flow.TYPE_USE_HOME,
"api_key": "api_key",
},
)
assert result2["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "unknown"}
async def test_form_error_unexpected_data(hass: HomeAssistant) -> None:
"""Test we handle unexpected data."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"CO2Signal.get_latest",
return_value={"status": "error"},
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"location": config_flow.TYPE_USE_HOME,
"api_key": "api_key",
},
)
assert result2["type"] == FlowResultType.FORM
assert result2["errors"] == {"base": "unknown"}