From 2eefd21dccc55e841d36c4215e7851189a474f38 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Mon, 25 Sep 2023 22:21:40 +0200 Subject: [PATCH] Parametrize more co2signal config flow tests (#100882) * Clean up co2signal tests * Some more clean up * Use named parameter ids of parametrize --- .../components/co2signal/test_config_flow.py | 102 +++++++----------- 1 file changed, 37 insertions(+), 65 deletions(-) diff --git a/tests/components/co2signal/test_config_flow.py b/tests/components/co2signal/test_config_flow.py index 8f5c4cfd55c..879293ae959 100644 --- a/tests/components/co2signal/test_config_flow.py +++ b/tests/components/co2signal/test_config_flow.py @@ -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", + }