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:
parent
a242a1c107
commit
2eefd21dcc
1 changed files with 37 additions and 65 deletions
|
@ -1,6 +1,6 @@
|
|||
"""Test the CO2 Signal config flow."""
|
||||
from json import JSONDecodeError
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -132,14 +132,33 @@ async def test_form_country(hass: HomeAssistant) -> None:
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("err_str", "err_code"),
|
||||
("side_effect", "err_code"),
|
||||
[
|
||||
("Invalid authentication credentials", "invalid_auth"),
|
||||
("API rate limit exceeded.", "api_ratelimit"),
|
||||
("Something else", "unknown"),
|
||||
(
|
||||
ValueError("Invalid authentication credentials"),
|
||||
"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."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
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(
|
||||
"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"],
|
||||
{
|
||||
"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 result2["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}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {"base": err_code}
|
||||
|
||||
with patch(
|
||||
"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"],
|
||||
{
|
||||
"location": config_flow.TYPE_USE_HOME,
|
||||
"api_key": "api_key",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
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"}
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "CO2 Signal"
|
||||
assert result["data"] == {
|
||||
"api_key": "api_key",
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue