From 526d4b24763d21a9af299a212f2bfde6f05658ba Mon Sep 17 00:00:00 2001 From: Erwin Douna Date: Sun, 6 Aug 2023 09:32:56 +0000 Subject: [PATCH] Updating tests to support paramterization --- tests/components/tado/test_config_flow.py | 54 ++++++----------------- 1 file changed, 13 insertions(+), 41 deletions(-) diff --git a/tests/components/tado/test_config_flow.py b/tests/components/tado/test_config_flow.py index 056325524fd..06397110bc4 100644 --- a/tests/components/tado/test_config_flow.py +++ b/tests/components/tado/test_config_flow.py @@ -2,6 +2,7 @@ from http import HTTPStatus from unittest.mock import MagicMock, patch +import pytest import requests from homeassistant import config_entries @@ -78,53 +79,24 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None: assert result2["errors"] == {"base": "invalid_auth"} -async def test_form_key_error(hass: HomeAssistant) -> None: - """Test we handle KeyError.""" - 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=KeyError, - ): - 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: +@pytest.mark.parametrize( + "error", + [ + (KeyError, "invalid_auth"), + (RuntimeError, "cannot_connect"), + (ValueError, "unknown"), + ], +) +async def test_form_exceptions(hass: HomeAssistant, error) -> None: """Test we handle Exception.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) + exc, base_error = error with patch( "homeassistant.components.tado.config_flow.Tado", - side_effect=Exception, + side_effect=exc, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -132,7 +104,7 @@ async def test_form_exception(hass: HomeAssistant) -> None: ) assert result2["type"] == "form" - assert result2["errors"] == {"base": "unknown"} + assert result2["errors"] == {"base": base_error} async def test_options_flow(hass: HomeAssistant) -> None: