mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Improve type hints in cloudflare tests (#120344)
This commit is contained in:
parent
a4e22bcba6
commit
31157828e1
@ -3,7 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
import pycfdns
|
import pycfdns
|
||||||
|
|
||||||
@ -80,25 +80,20 @@ async def init_integration(
|
|||||||
return entry
|
return entry
|
||||||
|
|
||||||
|
|
||||||
def _get_mock_client(
|
def get_mock_client() -> Mock:
|
||||||
zone: pycfdns.ZoneModel | UndefinedType = UNDEFINED,
|
"""Return of Mock of pycfdns.Client."""
|
||||||
records: list[pycfdns.RecordModel] | UndefinedType = UNDEFINED,
|
client = Mock()
|
||||||
):
|
|
||||||
client: pycfdns.Client = AsyncMock()
|
|
||||||
|
|
||||||
client.list_zones = AsyncMock(
|
client.list_zones = AsyncMock(return_value=[MOCK_ZONE])
|
||||||
return_value=[MOCK_ZONE if zone is UNDEFINED else zone]
|
client.list_dns_records = AsyncMock(return_value=MOCK_ZONE_RECORDS)
|
||||||
)
|
|
||||||
client.list_dns_records = AsyncMock(
|
|
||||||
return_value=MOCK_ZONE_RECORDS if records is UNDEFINED else records
|
|
||||||
)
|
|
||||||
client.update_dns_record = AsyncMock(return_value=None)
|
client.update_dns_record = AsyncMock(return_value=None)
|
||||||
|
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
def _patch_async_setup_entry(return_value=True):
|
def patch_async_setup_entry() -> AsyncMock:
|
||||||
|
"""Patch the async_setup_entry method and return a mock."""
|
||||||
return patch(
|
return patch(
|
||||||
"homeassistant.components.cloudflare.async_setup_entry",
|
"homeassistant.components.cloudflare.async_setup_entry",
|
||||||
return_value=return_value,
|
return_value=True,
|
||||||
)
|
)
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
"""Define fixtures available for all tests."""
|
"""Define fixtures available for all tests."""
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from typing_extensions import Generator
|
||||||
|
|
||||||
from . import _get_mock_client
|
from . import get_mock_client
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def cfupdate(hass):
|
def cfupdate() -> Generator[MagicMock]:
|
||||||
"""Mock the CloudflareUpdater for easier testing."""
|
"""Mock the CloudflareUpdater for easier testing."""
|
||||||
mock_cfupdate = _get_mock_client()
|
mock_cfupdate = get_mock_client()
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.cloudflare.pycfdns.Client",
|
"homeassistant.components.cloudflare.pycfdns.Client",
|
||||||
return_value=mock_cfupdate,
|
return_value=mock_cfupdate,
|
||||||
@ -19,11 +20,11 @@ def cfupdate(hass):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def cfupdate_flow(hass):
|
def cfupdate_flow() -> Generator[MagicMock]:
|
||||||
"""Mock the CloudflareUpdater for easier config flow testing."""
|
"""Mock the CloudflareUpdater for easier config flow testing."""
|
||||||
mock_cfupdate = _get_mock_client()
|
mock_cfupdate = get_mock_client()
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.cloudflare.pycfdns.Client",
|
"homeassistant.components.cloudflare.config_flow.pycfdns.Client",
|
||||||
return_value=mock_cfupdate,
|
return_value=mock_cfupdate,
|
||||||
) as mock_api:
|
) as mock_api:
|
||||||
yield mock_api
|
yield mock_api
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
"""Test the Cloudflare config flow."""
|
"""Test the Cloudflare config flow."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
import pycfdns
|
import pycfdns
|
||||||
|
|
||||||
from homeassistant.components.cloudflare.const import CONF_RECORDS, DOMAIN
|
from homeassistant.components.cloudflare.const import CONF_RECORDS, DOMAIN
|
||||||
@ -13,13 +15,13 @@ from . import (
|
|||||||
USER_INPUT,
|
USER_INPUT,
|
||||||
USER_INPUT_RECORDS,
|
USER_INPUT_RECORDS,
|
||||||
USER_INPUT_ZONE,
|
USER_INPUT_ZONE,
|
||||||
_patch_async_setup_entry,
|
patch_async_setup_entry,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def test_user_form(hass: HomeAssistant, cfupdate_flow) -> None:
|
async def test_user_form(hass: HomeAssistant, cfupdate_flow: MagicMock) -> None:
|
||||||
"""Test we get the user initiated form."""
|
"""Test we get the user initiated form."""
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
@ -49,7 +51,7 @@ async def test_user_form(hass: HomeAssistant, cfupdate_flow) -> None:
|
|||||||
assert result["step_id"] == "records"
|
assert result["step_id"] == "records"
|
||||||
assert result["errors"] is None
|
assert result["errors"] is None
|
||||||
|
|
||||||
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_RECORDS,
|
USER_INPUT_RECORDS,
|
||||||
@ -70,7 +72,9 @@ async def test_user_form(hass: HomeAssistant, cfupdate_flow) -> None:
|
|||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_user_form_cannot_connect(hass: HomeAssistant, cfupdate_flow) -> None:
|
async def test_user_form_cannot_connect(
|
||||||
|
hass: HomeAssistant, cfupdate_flow: MagicMock
|
||||||
|
) -> None:
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle cannot connect error."""
|
||||||
instance = cfupdate_flow.return_value
|
instance = cfupdate_flow.return_value
|
||||||
|
|
||||||
@ -88,7 +92,9 @@ async def test_user_form_cannot_connect(hass: HomeAssistant, cfupdate_flow) -> N
|
|||||||
assert result["errors"] == {"base": "cannot_connect"}
|
assert result["errors"] == {"base": "cannot_connect"}
|
||||||
|
|
||||||
|
|
||||||
async def test_user_form_invalid_auth(hass: HomeAssistant, cfupdate_flow) -> None:
|
async def test_user_form_invalid_auth(
|
||||||
|
hass: HomeAssistant, cfupdate_flow: MagicMock
|
||||||
|
) -> None:
|
||||||
"""Test we handle invalid auth error."""
|
"""Test we handle invalid auth error."""
|
||||||
instance = cfupdate_flow.return_value
|
instance = cfupdate_flow.return_value
|
||||||
|
|
||||||
@ -107,7 +113,7 @@ async def test_user_form_invalid_auth(hass: HomeAssistant, cfupdate_flow) -> Non
|
|||||||
|
|
||||||
|
|
||||||
async def test_user_form_unexpected_exception(
|
async def test_user_form_unexpected_exception(
|
||||||
hass: HomeAssistant, cfupdate_flow
|
hass: HomeAssistant, cfupdate_flow: MagicMock
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle unexpected exception."""
|
"""Test we handle unexpected exception."""
|
||||||
instance = cfupdate_flow.return_value
|
instance = cfupdate_flow.return_value
|
||||||
@ -140,7 +146,7 @@ async def test_user_form_single_instance_allowed(hass: HomeAssistant) -> None:
|
|||||||
assert result["reason"] == "single_instance_allowed"
|
assert result["reason"] == "single_instance_allowed"
|
||||||
|
|
||||||
|
|
||||||
async def test_reauth_flow(hass: HomeAssistant, cfupdate_flow) -> None:
|
async def test_reauth_flow(hass: HomeAssistant, cfupdate_flow: MagicMock) -> None:
|
||||||
"""Test the reauthentication configuration flow."""
|
"""Test the reauthentication configuration flow."""
|
||||||
entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG)
|
entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
@ -157,7 +163,7 @@ async def test_reauth_flow(hass: HomeAssistant, cfupdate_flow) -> None:
|
|||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "reauth_confirm"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
|
||||||
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_API_TOKEN: "other_token"},
|
{CONF_API_TOKEN: "other_token"},
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""Test the Cloudflare integration."""
|
"""Test the Cloudflare integration."""
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import pycfdns
|
import pycfdns
|
||||||
import pytest
|
import pytest
|
||||||
@ -23,7 +23,7 @@ from . import ENTRY_CONFIG, init_integration
|
|||||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||||
|
|
||||||
|
|
||||||
async def test_unload_entry(hass: HomeAssistant, cfupdate) -> None:
|
async def test_unload_entry(hass: HomeAssistant, cfupdate: MagicMock) -> None:
|
||||||
"""Test successful unload of entry."""
|
"""Test successful unload of entry."""
|
||||||
entry = await init_integration(hass)
|
entry = await init_integration(hass)
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ async def test_unload_entry(hass: HomeAssistant, cfupdate) -> None:
|
|||||||
[pycfdns.ComunicationException()],
|
[pycfdns.ComunicationException()],
|
||||||
)
|
)
|
||||||
async def test_async_setup_raises_entry_not_ready(
|
async def test_async_setup_raises_entry_not_ready(
|
||||||
hass: HomeAssistant, cfupdate, side_effect
|
hass: HomeAssistant, cfupdate: MagicMock, side_effect: Exception
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that it throws ConfigEntryNotReady when exception occurs during setup."""
|
"""Test that it throws ConfigEntryNotReady when exception occurs during setup."""
|
||||||
instance = cfupdate.return_value
|
instance = cfupdate.return_value
|
||||||
@ -57,7 +57,7 @@ async def test_async_setup_raises_entry_not_ready(
|
|||||||
|
|
||||||
|
|
||||||
async def test_async_setup_raises_entry_auth_failed(
|
async def test_async_setup_raises_entry_auth_failed(
|
||||||
hass: HomeAssistant, cfupdate
|
hass: HomeAssistant, cfupdate: MagicMock
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that it throws ConfigEntryAuthFailed when exception occurs during setup."""
|
"""Test that it throws ConfigEntryAuthFailed when exception occurs during setup."""
|
||||||
instance = cfupdate.return_value
|
instance = cfupdate.return_value
|
||||||
@ -84,7 +84,7 @@ async def test_async_setup_raises_entry_auth_failed(
|
|||||||
|
|
||||||
|
|
||||||
async def test_integration_services(
|
async def test_integration_services(
|
||||||
hass: HomeAssistant, cfupdate, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, cfupdate: MagicMock, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test integration services."""
|
"""Test integration services."""
|
||||||
instance = cfupdate.return_value
|
instance = cfupdate.return_value
|
||||||
@ -120,7 +120,9 @@ async def test_integration_services(
|
|||||||
assert "All target records are up to date" not in caplog.text
|
assert "All target records are up to date" not in caplog.text
|
||||||
|
|
||||||
|
|
||||||
async def test_integration_services_with_issue(hass: HomeAssistant, cfupdate) -> None:
|
async def test_integration_services_with_issue(
|
||||||
|
hass: HomeAssistant, cfupdate: MagicMock
|
||||||
|
) -> None:
|
||||||
"""Test integration services with issue."""
|
"""Test integration services with issue."""
|
||||||
instance = cfupdate.return_value
|
instance = cfupdate.return_value
|
||||||
|
|
||||||
@ -145,7 +147,7 @@ async def test_integration_services_with_issue(hass: HomeAssistant, cfupdate) ->
|
|||||||
|
|
||||||
|
|
||||||
async def test_integration_services_with_nonexisting_record(
|
async def test_integration_services_with_nonexisting_record(
|
||||||
hass: HomeAssistant, cfupdate, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, cfupdate: MagicMock, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test integration services."""
|
"""Test integration services."""
|
||||||
instance = cfupdate.return_value
|
instance = cfupdate.return_value
|
||||||
@ -185,7 +187,7 @@ async def test_integration_services_with_nonexisting_record(
|
|||||||
|
|
||||||
async def test_integration_update_interval(
|
async def test_integration_update_interval(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
cfupdate,
|
cfupdate: MagicMock,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test integration update interval."""
|
"""Test integration update interval."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user