Skip to content

Commit

Permalink
Improve type hints in cloudflare tests (#120344)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jun 24, 2024
1 parent a4e22bc commit 3115782
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
23 changes: 9 additions & 14 deletions tests/components/cloudflare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from typing import Any
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, Mock, patch

import pycfdns

Expand Down Expand Up @@ -80,25 +80,20 @@ async def init_integration(
return entry


def _get_mock_client(
zone: pycfdns.ZoneModel | UndefinedType = UNDEFINED,
records: list[pycfdns.RecordModel] | UndefinedType = UNDEFINED,
):
client: pycfdns.Client = AsyncMock()
def get_mock_client() -> Mock:
"""Return of Mock of pycfdns.Client."""
client = Mock()

client.list_zones = AsyncMock(
return_value=[MOCK_ZONE if zone is UNDEFINED else zone]
)
client.list_dns_records = AsyncMock(
return_value=MOCK_ZONE_RECORDS if records is UNDEFINED else records
)
client.list_zones = AsyncMock(return_value=[MOCK_ZONE])
client.list_dns_records = AsyncMock(return_value=MOCK_ZONE_RECORDS)
client.update_dns_record = AsyncMock(return_value=None)

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(
"homeassistant.components.cloudflare.async_setup_entry",
return_value=return_value,
return_value=True,
)
15 changes: 8 additions & 7 deletions tests/components/cloudflare/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""Define fixtures available for all tests."""

from unittest.mock import patch
from unittest.mock import MagicMock, patch

import pytest
from typing_extensions import Generator

from . import _get_mock_client
from . import get_mock_client


@pytest.fixture
def cfupdate(hass):
def cfupdate() -> Generator[MagicMock]:
"""Mock the CloudflareUpdater for easier testing."""
mock_cfupdate = _get_mock_client()
mock_cfupdate = get_mock_client()
with patch(
"homeassistant.components.cloudflare.pycfdns.Client",
return_value=mock_cfupdate,
Expand All @@ -19,11 +20,11 @@ def cfupdate(hass):


@pytest.fixture
def cfupdate_flow(hass):
def cfupdate_flow() -> Generator[MagicMock]:
"""Mock the CloudflareUpdater for easier config flow testing."""
mock_cfupdate = _get_mock_client()
mock_cfupdate = get_mock_client()
with patch(
"homeassistant.components.cloudflare.pycfdns.Client",
"homeassistant.components.cloudflare.config_flow.pycfdns.Client",
return_value=mock_cfupdate,
) as mock_api:
yield mock_api
22 changes: 14 additions & 8 deletions tests/components/cloudflare/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test the Cloudflare config flow."""

from unittest.mock import MagicMock

import pycfdns

from homeassistant.components.cloudflare.const import CONF_RECORDS, DOMAIN
Expand All @@ -13,13 +15,13 @@
USER_INPUT,
USER_INPUT_RECORDS,
USER_INPUT_ZONE,
_patch_async_setup_entry,
patch_async_setup_entry,
)

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."""

result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -49,7 +51,7 @@ async def test_user_form(hass: HomeAssistant, cfupdate_flow) -> None:
assert result["step_id"] == "records"
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["flow_id"],
USER_INPUT_RECORDS,
Expand All @@ -70,7 +72,9 @@ async def test_user_form(hass: HomeAssistant, cfupdate_flow) -> None:
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."""
instance = cfupdate_flow.return_value

Expand All @@ -88,7 +92,9 @@ async def test_user_form_cannot_connect(hass: HomeAssistant, cfupdate_flow) -> N
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."""
instance = cfupdate_flow.return_value

Expand All @@ -107,7 +113,7 @@ async def test_user_form_invalid_auth(hass: HomeAssistant, cfupdate_flow) -> Non


async def test_user_form_unexpected_exception(
hass: HomeAssistant, cfupdate_flow
hass: HomeAssistant, cfupdate_flow: MagicMock
) -> None:
"""Test we handle unexpected exception."""
instance = cfupdate_flow.return_value
Expand Down Expand Up @@ -140,7 +146,7 @@ async def test_user_form_single_instance_allowed(hass: HomeAssistant) -> None:
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."""
entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG)
entry.add_to_hass(hass)
Expand All @@ -157,7 +163,7 @@ async def test_reauth_flow(hass: HomeAssistant, cfupdate_flow) -> None:
assert result["type"] is FlowResultType.FORM
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["flow_id"],
{CONF_API_TOKEN: "other_token"},
Expand Down
18 changes: 10 additions & 8 deletions tests/components/cloudflare/test_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test the Cloudflare integration."""

from datetime import timedelta
from unittest.mock import patch
from unittest.mock import MagicMock, patch

import pycfdns
import pytest
Expand All @@ -23,7 +23,7 @@
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."""
entry = await init_integration(hass)

Expand All @@ -42,7 +42,7 @@ async def test_unload_entry(hass: HomeAssistant, cfupdate) -> None:
[pycfdns.ComunicationException()],
)
async def test_async_setup_raises_entry_not_ready(
hass: HomeAssistant, cfupdate, side_effect
hass: HomeAssistant, cfupdate: MagicMock, side_effect: Exception
) -> None:
"""Test that it throws ConfigEntryNotReady when exception occurs during setup."""
instance = cfupdate.return_value
Expand All @@ -57,7 +57,7 @@ async def test_async_setup_raises_entry_not_ready(


async def test_async_setup_raises_entry_auth_failed(
hass: HomeAssistant, cfupdate
hass: HomeAssistant, cfupdate: MagicMock
) -> None:
"""Test that it throws ConfigEntryAuthFailed when exception occurs during setup."""
instance = cfupdate.return_value
Expand All @@ -84,7 +84,7 @@ async def test_async_setup_raises_entry_auth_failed(


async def test_integration_services(
hass: HomeAssistant, cfupdate, caplog: pytest.LogCaptureFixture
hass: HomeAssistant, cfupdate: MagicMock, caplog: pytest.LogCaptureFixture
) -> None:
"""Test integration services."""
instance = cfupdate.return_value
Expand Down Expand Up @@ -120,7 +120,9 @@ async def test_integration_services(
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."""
instance = cfupdate.return_value

Expand All @@ -145,7 +147,7 @@ async def test_integration_services_with_issue(hass: HomeAssistant, cfupdate) ->


async def test_integration_services_with_nonexisting_record(
hass: HomeAssistant, cfupdate, caplog: pytest.LogCaptureFixture
hass: HomeAssistant, cfupdate: MagicMock, caplog: pytest.LogCaptureFixture
) -> None:
"""Test integration services."""
instance = cfupdate.return_value
Expand Down Expand Up @@ -185,7 +187,7 @@ async def test_integration_services_with_nonexisting_record(

async def test_integration_update_interval(
hass: HomeAssistant,
cfupdate,
cfupdate: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test integration update interval."""
Expand Down

0 comments on commit 3115782

Please sign in to comment.