From 273d289e03acbb1a79a972e79cdc34da5439141c Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 17 Feb 2023 12:32:27 +0100 Subject: [PATCH] Fix sleepiq test to not contact the API (#88315) * Fix sleepiq test to not contact the API * Add an autosue fixture for config entry setup --- tests/components/sleepiq/test_config_flow.py | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/components/sleepiq/test_config_flow.py b/tests/components/sleepiq/test_config_flow.py index 7786165a2ac..a2191b56583 100644 --- a/tests/components/sleepiq/test_config_flow.py +++ b/tests/components/sleepiq/test_config_flow.py @@ -1,5 +1,6 @@ """Tests for the SleepIQ config flow.""" -from unittest.mock import patch +from collections.abc import Generator +from unittest.mock import AsyncMock, patch from asyncsleepiq import SleepIQLoginException, SleepIQTimeoutException import pytest @@ -12,6 +13,15 @@ from homeassistant.core import HomeAssistant from .conftest import SLEEPIQ_CONFIG, setup_platform +@pytest.fixture(autouse=True, name="mock_setup_entry") +def override_async_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.sleepiq.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + async def test_import(hass: HomeAssistant) -> None: """Test that we can import a config entry.""" with patch("asyncsleepiq.AsyncSleepIQ.login"): @@ -72,7 +82,7 @@ async def test_login_failure(hass: HomeAssistant, side_effect, error) -> None: assert result["errors"] == {"base": error} -async def test_success(hass: HomeAssistant) -> None: +async def test_success(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: """Test successful flow provides entry creation data.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -80,10 +90,7 @@ async def test_success(hass: HomeAssistant) -> None: assert result["type"] == "form" assert result["errors"] == {} - with patch("asyncsleepiq.AsyncSleepIQ.login", return_value=True), patch( - "homeassistant.components.sleepiq.async_setup_entry", - return_value=True, - ) as mock_setup_entry: + with patch("asyncsleepiq.AsyncSleepIQ.login", return_value=True): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], SLEEPIQ_CONFIG ) @@ -117,9 +124,6 @@ async def test_reauth_password(hass: HomeAssistant) -> None: with patch( "homeassistant.components.sleepiq.config_flow.AsyncSleepIQ.login", return_value=True, - ), patch( - "homeassistant.components.sleepiq.async_setup_entry", - return_value=True, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"],