mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Improve type hints in ezviz tests (#120679)
This commit is contained in:
parent
3ca66be268
commit
84a8259103
@ -1,6 +1,6 @@
|
|||||||
"""Tests for the EZVIZ integration."""
|
"""Tests for the EZVIZ integration."""
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import _patch, patch
|
||||||
|
|
||||||
from homeassistant.components.ezviz.const import (
|
from homeassistant.components.ezviz.const import (
|
||||||
ATTR_SERIAL,
|
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(
|
return patch(
|
||||||
"homeassistant.components.ezviz.async_setup_entry",
|
"homeassistant.components.ezviz.async_setup_entry",
|
||||||
return_value=return_value,
|
return_value=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,6 +5,9 @@ from unittest.mock import MagicMock, patch
|
|||||||
from pyezviz import EzvizClient
|
from pyezviz import EzvizClient
|
||||||
from pyezviz.test_cam_rtsp import TestRTSPAuth
|
from pyezviz.test_cam_rtsp import TestRTSPAuth
|
||||||
import pytest
|
import pytest
|
||||||
|
from typing_extensions import Generator
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
ezviz_login_token_return = {
|
ezviz_login_token_return = {
|
||||||
"session_id": "fake_token",
|
"session_id": "fake_token",
|
||||||
@ -14,13 +17,13 @@ ezviz_login_token_return = {
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def mock_ffmpeg(hass):
|
def mock_ffmpeg(hass: HomeAssistant) -> None:
|
||||||
"""Mock ffmpeg is loaded."""
|
"""Mock ffmpeg is loaded."""
|
||||||
hass.config.components.add("ffmpeg")
|
hass.config.components.add("ffmpeg")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def ezviz_test_rtsp_config_flow(hass):
|
def ezviz_test_rtsp_config_flow() -> Generator[MagicMock]:
|
||||||
"""Mock the EzvizApi for easier testing."""
|
"""Mock the EzvizApi for easier testing."""
|
||||||
with (
|
with (
|
||||||
patch.object(TestRTSPAuth, "main", return_value=True),
|
patch.object(TestRTSPAuth, "main", return_value=True),
|
||||||
@ -40,7 +43,7 @@ def ezviz_test_rtsp_config_flow(hass):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def ezviz_config_flow(hass):
|
def ezviz_config_flow() -> Generator[MagicMock]:
|
||||||
"""Mock the EzvizAPI for easier config flow testing."""
|
"""Mock the EzvizAPI for easier config flow testing."""
|
||||||
with (
|
with (
|
||||||
patch.object(EzvizClient, "login", return_value=True),
|
patch.object(EzvizClient, "login", return_value=True),
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"""Test the EZVIZ config flow."""
|
"""Test the EZVIZ config flow."""
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from pyezviz.exceptions import (
|
from pyezviz.exceptions import (
|
||||||
AuthTestResultFailed,
|
AuthTestResultFailed,
|
||||||
@ -10,6 +10,7 @@ from pyezviz.exceptions import (
|
|||||||
InvalidURL,
|
InvalidURL,
|
||||||
PyEzvizError,
|
PyEzvizError,
|
||||||
)
|
)
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.ezviz.const import (
|
from homeassistant.components.ezviz.const import (
|
||||||
ATTR_SERIAL,
|
ATTR_SERIAL,
|
||||||
@ -40,12 +41,13 @@ from . import (
|
|||||||
API_LOGIN_RETURN_VALIDATE,
|
API_LOGIN_RETURN_VALIDATE,
|
||||||
DISCOVERY_INFO,
|
DISCOVERY_INFO,
|
||||||
USER_INPUT_VALIDATE,
|
USER_INPUT_VALIDATE,
|
||||||
_patch_async_setup_entry,
|
|
||||||
init_integration,
|
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."""
|
"""Test the user initiated form."""
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
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["step_id"] == "user"
|
||||||
assert result["errors"] == {}
|
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 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
USER_INPUT_VALIDATE,
|
USER_INPUT_VALIDATE,
|
||||||
@ -75,7 +77,8 @@ async def test_user_form(hass: HomeAssistant, ezviz_config_flow) -> None:
|
|||||||
assert result["reason"] == "already_configured_account"
|
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."""
|
"""Test custom url step."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}
|
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["step_id"] == "user_custom_url"
|
||||||
assert result["errors"] == {}
|
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 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{CONF_URL: "test-user"},
|
{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
|
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."""
|
"""Test the reauth step."""
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
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["step_id"] == "user"
|
||||||
assert result["errors"] == {}
|
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 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
USER_INPUT_VALIDATE,
|
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"
|
assert result["reason"] == "ezviz_cloud_account_missing"
|
||||||
|
|
||||||
|
|
||||||
async def test_async_step_integration_discovery(
|
@pytest.mark.usefixtures("ezviz_config_flow", "ezviz_test_rtsp_config_flow")
|
||||||
hass: HomeAssistant, ezviz_config_flow, ezviz_test_rtsp_config_flow
|
async def test_async_step_integration_discovery(hass: HomeAssistant) -> None:
|
||||||
) -> None:
|
|
||||||
"""Test discovery and confirm step."""
|
"""Test discovery and confirm step."""
|
||||||
with patch("homeassistant.components.ezviz.PLATFORMS_BY_TYPE", []):
|
with patch("homeassistant.components.ezviz.PLATFORMS_BY_TYPE", []):
|
||||||
await init_integration(hass)
|
await init_integration(hass)
|
||||||
@ -199,7 +202,7 @@ async def test_async_step_integration_discovery(
|
|||||||
assert result["step_id"] == "confirm"
|
assert result["step_id"] == "confirm"
|
||||||
assert result["errors"] == {}
|
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 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
{
|
||||||
@ -221,7 +224,7 @@ async def test_async_step_integration_discovery(
|
|||||||
|
|
||||||
async def test_options_flow(hass: HomeAssistant) -> None:
|
async def test_options_flow(hass: HomeAssistant) -> None:
|
||||||
"""Test updating options."""
|
"""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)
|
entry = await init_integration(hass)
|
||||||
|
|
||||||
assert entry.options[CONF_FFMPEG_ARGUMENTS] == DEFAULT_FFMPEG_ARGUMENTS
|
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
|
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."""
|
"""Test we handle exception on user form."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}
|
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(
|
async def test_discover_exception_step1(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
ezviz_config_flow,
|
ezviz_config_flow: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle unexpected exception on discovery."""
|
"""Test we handle unexpected exception on discovery."""
|
||||||
with patch("homeassistant.components.ezviz.PLATFORMS_BY_TYPE", []):
|
with patch("homeassistant.components.ezviz.PLATFORMS_BY_TYPE", []):
|
||||||
@ -397,10 +402,9 @@ async def test_discover_exception_step1(
|
|||||||
assert result["reason"] == "unknown"
|
assert result["reason"] == "unknown"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("ezviz_config_flow")
|
||||||
async def test_discover_exception_step3(
|
async def test_discover_exception_step3(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant, ezviz_test_rtsp_config_flow: MagicMock
|
||||||
ezviz_config_flow,
|
|
||||||
ezviz_test_rtsp_config_flow,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle unexpected exception on discovery."""
|
"""Test we handle unexpected exception on discovery."""
|
||||||
with patch("homeassistant.components.ezviz.PLATFORMS_BY_TYPE", []):
|
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(
|
async def test_user_custom_url_exception(
|
||||||
hass: HomeAssistant, ezviz_config_flow
|
hass: HomeAssistant, ezviz_config_flow: MagicMock
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle unexpected exception."""
|
"""Test we handle unexpected exception."""
|
||||||
ezviz_config_flow.side_effect = PyEzvizError()
|
ezviz_config_flow.side_effect = PyEzvizError()
|
||||||
@ -534,7 +538,7 @@ async def test_user_custom_url_exception(
|
|||||||
|
|
||||||
|
|
||||||
async def test_async_step_reauth_exception(
|
async def test_async_step_reauth_exception(
|
||||||
hass: HomeAssistant, ezviz_config_flow
|
hass: HomeAssistant, ezviz_config_flow: MagicMock
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the reauth step exceptions."""
|
"""Test the reauth step exceptions."""
|
||||||
|
|
||||||
@ -545,7 +549,7 @@ async def test_async_step_reauth_exception(
|
|||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"] == {}
|
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 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
USER_INPUT_VALIDATE,
|
USER_INPUT_VALIDATE,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user