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
pull/88320/head
Erik Montnemery 2023-02-17 12:32:27 +01:00 committed by GitHub
parent 5ff87e6110
commit 273d289e03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 9 deletions

View File

@ -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"],