Improve type hints in ezviz tests (#120679)

This commit is contained in:
epenet 2024-07-04 21:59:02 +02:00 committed by GitHub
parent 3ca66be268
commit 84a8259103
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 27 deletions

View file

@ -1,6 +1,6 @@
"""Tests for the EZVIZ integration."""
from unittest.mock import patch
from unittest.mock import _patch, patch
from homeassistant.components.ezviz.const import (
ATTR_SERIAL,
@ -83,10 +83,11 @@ API_LOGIN_RETURN_VALIDATE = {
}
def _patch_async_setup_entry(return_value=True):
def patch_async_setup_entry() -> _patch:
"""Patch async_setup_entry."""
return patch(
"homeassistant.components.ezviz.async_setup_entry",
return_value=return_value,
return_value=True,
)

View file

@ -5,6 +5,9 @@ from unittest.mock import MagicMock, patch
from pyezviz import EzvizClient
from pyezviz.test_cam_rtsp import TestRTSPAuth
import pytest
from typing_extensions import Generator
from homeassistant.core import HomeAssistant
ezviz_login_token_return = {
"session_id": "fake_token",
@ -14,13 +17,13 @@ ezviz_login_token_return = {
@pytest.fixture(autouse=True)
def mock_ffmpeg(hass):
def mock_ffmpeg(hass: HomeAssistant) -> None:
"""Mock ffmpeg is loaded."""
hass.config.components.add("ffmpeg")
@pytest.fixture
def ezviz_test_rtsp_config_flow(hass):
def ezviz_test_rtsp_config_flow() -> Generator[MagicMock]:
"""Mock the EzvizApi for easier testing."""
with (
patch.object(TestRTSPAuth, "main", return_value=True),
@ -40,7 +43,7 @@ def ezviz_test_rtsp_config_flow(hass):
@pytest.fixture
def ezviz_config_flow(hass):
def ezviz_config_flow() -> Generator[MagicMock]:
"""Mock the EzvizAPI for easier config flow testing."""
with (
patch.object(EzvizClient, "login", return_value=True),

View file

@ -1,6 +1,6 @@
"""Test the EZVIZ config flow."""
from unittest.mock import patch
from unittest.mock import MagicMock, patch
from pyezviz.exceptions import (
AuthTestResultFailed,
@ -10,6 +10,7 @@ from pyezviz.exceptions import (
InvalidURL,
PyEzvizError,
)
import pytest
from homeassistant.components.ezviz.const import (
ATTR_SERIAL,
@ -40,12 +41,13 @@ from . import (
API_LOGIN_RETURN_VALIDATE,
DISCOVERY_INFO,
USER_INPUT_VALIDATE,
_patch_async_setup_entry,
init_integration,
patch_async_setup_entry,
)
async def test_user_form(hass: HomeAssistant, ezviz_config_flow) -> None:
@pytest.mark.usefixtures("ezviz_config_flow")
async def test_user_form(hass: HomeAssistant) -> None:
"""Test the user initiated form."""
result = await hass.config_entries.flow.async_init(
@ -55,7 +57,7 @@ async def test_user_form(hass: HomeAssistant, ezviz_config_flow) -> None:
assert result["step_id"] == "user"
assert result["errors"] == {}
with _patch_async_setup_entry() as mock_setup_entry:
with patch_async_setup_entry() as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_VALIDATE,
@ -75,7 +77,8 @@ async def test_user_form(hass: HomeAssistant, ezviz_config_flow) -> None:
assert result["reason"] == "already_configured_account"
async def test_user_custom_url(hass: HomeAssistant, ezviz_config_flow) -> None:
@pytest.mark.usefixtures("ezviz_config_flow")
async def test_user_custom_url(hass: HomeAssistant) -> None:
"""Test custom url step."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
@ -94,7 +97,7 @@ async def test_user_custom_url(hass: HomeAssistant, ezviz_config_flow) -> None:
assert result["step_id"] == "user_custom_url"
assert result["errors"] == {}
with _patch_async_setup_entry() as mock_setup_entry:
with patch_async_setup_entry() as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_URL: "test-user"},
@ -107,7 +110,8 @@ async def test_user_custom_url(hass: HomeAssistant, ezviz_config_flow) -> None:
assert len(mock_setup_entry.mock_calls) == 1
async def test_async_step_reauth(hass: HomeAssistant, ezviz_config_flow) -> None:
@pytest.mark.usefixtures("ezviz_config_flow")
async def test_async_step_reauth(hass: HomeAssistant) -> None:
"""Test the reauth step."""
result = await hass.config_entries.flow.async_init(
@ -117,7 +121,7 @@ async def test_async_step_reauth(hass: HomeAssistant, ezviz_config_flow) -> None
assert result["step_id"] == "user"
assert result["errors"] == {}
with _patch_async_setup_entry() as mock_setup_entry:
with patch_async_setup_entry() as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_VALIDATE,
@ -185,9 +189,8 @@ async def test_step_reauth_abort_if_cloud_account_missing(hass: HomeAssistant) -
assert result["reason"] == "ezviz_cloud_account_missing"
async def test_async_step_integration_discovery(
hass: HomeAssistant, ezviz_config_flow, ezviz_test_rtsp_config_flow
) -> None:
@pytest.mark.usefixtures("ezviz_config_flow", "ezviz_test_rtsp_config_flow")
async def test_async_step_integration_discovery(hass: HomeAssistant) -> None:
"""Test discovery and confirm step."""
with patch("homeassistant.components.ezviz.PLATFORMS_BY_TYPE", []):
await init_integration(hass)
@ -199,7 +202,7 @@ async def test_async_step_integration_discovery(
assert result["step_id"] == "confirm"
assert result["errors"] == {}
with _patch_async_setup_entry() as mock_setup_entry:
with patch_async_setup_entry() as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
@ -221,7 +224,7 @@ async def test_async_step_integration_discovery(
async def test_options_flow(hass: HomeAssistant) -> None:
"""Test updating options."""
with _patch_async_setup_entry() as mock_setup_entry:
with patch_async_setup_entry() as mock_setup_entry:
entry = await init_integration(hass)
assert entry.options[CONF_FFMPEG_ARGUMENTS] == DEFAULT_FFMPEG_ARGUMENTS
@ -245,7 +248,9 @@ async def test_options_flow(hass: HomeAssistant) -> None:
assert len(mock_setup_entry.mock_calls) == 1
async def test_user_form_exception(hass: HomeAssistant, ezviz_config_flow) -> None:
async def test_user_form_exception(
hass: HomeAssistant, ezviz_config_flow: MagicMock
) -> None:
"""Test we handle exception on user form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
@ -311,7 +316,7 @@ async def test_user_form_exception(hass: HomeAssistant, ezviz_config_flow) -> No
async def test_discover_exception_step1(
hass: HomeAssistant,
ezviz_config_flow,
ezviz_config_flow: MagicMock,
) -> None:
"""Test we handle unexpected exception on discovery."""
with patch("homeassistant.components.ezviz.PLATFORMS_BY_TYPE", []):
@ -397,10 +402,9 @@ async def test_discover_exception_step1(
assert result["reason"] == "unknown"
@pytest.mark.usefixtures("ezviz_config_flow")
async def test_discover_exception_step3(
hass: HomeAssistant,
ezviz_config_flow,
ezviz_test_rtsp_config_flow,
hass: HomeAssistant, ezviz_test_rtsp_config_flow: MagicMock
) -> None:
"""Test we handle unexpected exception on discovery."""
with patch("homeassistant.components.ezviz.PLATFORMS_BY_TYPE", []):
@ -459,7 +463,7 @@ async def test_discover_exception_step3(
async def test_user_custom_url_exception(
hass: HomeAssistant, ezviz_config_flow
hass: HomeAssistant, ezviz_config_flow: MagicMock
) -> None:
"""Test we handle unexpected exception."""
ezviz_config_flow.side_effect = PyEzvizError()
@ -534,7 +538,7 @@ async def test_user_custom_url_exception(
async def test_async_step_reauth_exception(
hass: HomeAssistant, ezviz_config_flow
hass: HomeAssistant, ezviz_config_flow: MagicMock
) -> None:
"""Test the reauth step exceptions."""
@ -545,7 +549,7 @@ async def test_async_step_reauth_exception(
assert result["step_id"] == "user"
assert result["errors"] == {}
with _patch_async_setup_entry() as mock_setup_entry:
with patch_async_setup_entry() as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_VALIDATE,