Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing mocking in nextdns tests #114541

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions tests/components/nextdns/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for the NextDNS integration."""

from contextlib import contextmanager
from unittest.mock import patch

from nextdns import (
Expand Down Expand Up @@ -113,16 +114,9 @@
)


async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
"""Set up the NextDNS integration in Home Assistant."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Fake Profile",
unique_id="xyz12",
data={CONF_API_KEY: "fake_api_key", CONF_PROFILE_ID: "xyz12"},
entry_id="d9aa37407ddac7b964a99e86312288d6",
)

@contextmanager
def mock_nextdns():
"""Mock the NextDNS class."""
with (
patch(
"homeassistant.components.nextdns.NextDns.get_profiles",
Expand Down Expand Up @@ -157,6 +151,20 @@ async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
return_value=CONNECTION_STATUS,
),
):
yield


async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
"""Set up the NextDNS integration in Home Assistant."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Fake Profile",
unique_id="xyz12",
data={CONF_API_KEY: "fake_api_key", CONF_PROFILE_ID: "xyz12"},
entry_id="d9aa37407ddac7b964a99e86312288d6",
)

with mock_nextdns():
entry.add_to_hass(hass)
bdraco marked this conversation as resolved.
Show resolved Hide resolved
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
Expand Down
11 changes: 4 additions & 7 deletions tests/components/nextdns/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow

from . import CONNECTION_STATUS, init_integration
from . import init_integration, mock_nextdns

from tests.common import async_fire_time_changed

Expand Down Expand Up @@ -57,19 +57,16 @@ async def test_availability(hass: HomeAssistant) -> None:
side_effect=ApiError("API Error"),
):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)

state = hass.states.get("binary_sensor.fake_profile_device_connection_status")
assert state
assert state.state == STATE_UNAVAILABLE

future = utcnow() + timedelta(minutes=20)
with patch(
"homeassistant.components.nextdns.NextDns.connection_status",
return_value=CONNECTION_STATUS,
):
with mock_nextdns():
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)

state = hass.states.get("binary_sensor.fake_profile_device_connection_status")
assert state
Expand Down
29 changes: 4 additions & 25 deletions tests/components/nextdns/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow

from . import DNSSEC, ENCRYPTION, IP_VERSIONS, PROTOCOLS, STATUS, init_integration
from . import init_integration, mock_nextdns

from tests.common import async_fire_time_changed

Expand Down Expand Up @@ -332,7 +332,7 @@ async def test_availability(
),
):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)

state = hass.states.get("sensor.fake_profile_dns_queries")
assert state
Expand All @@ -355,30 +355,9 @@ async def test_availability(
assert state.state == STATE_UNAVAILABLE

future = utcnow() + timedelta(minutes=20)
with (
patch(
"homeassistant.components.nextdns.NextDns.get_analytics_status",
return_value=STATUS,
),
patch(
"homeassistant.components.nextdns.NextDns.get_analytics_encryption",
return_value=ENCRYPTION,
),
patch(
"homeassistant.components.nextdns.NextDns.get_analytics_dnssec",
return_value=DNSSEC,
),
patch(
"homeassistant.components.nextdns.NextDns.get_analytics_ip_versions",
return_value=IP_VERSIONS,
),
patch(
"homeassistant.components.nextdns.NextDns.get_analytics_protocols",
return_value=PROTOCOLS,
),
):
with mock_nextdns():
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)

state = hass.states.get("sensor.fake_profile_dns_queries")
assert state
Expand Down
11 changes: 4 additions & 7 deletions tests/components/nextdns/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow

from . import SETTINGS, init_integration
from . import init_integration, mock_nextdns

from tests.common import async_fire_time_changed

Expand Down Expand Up @@ -693,19 +693,16 @@ async def test_availability(hass: HomeAssistant) -> None:
side_effect=ApiError("API Error"),
):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)

state = hass.states.get("switch.fake_profile_web3")
assert state
assert state.state == STATE_UNAVAILABLE

future = utcnow() + timedelta(minutes=20)
with patch(
"homeassistant.components.nextdns.NextDns.get_settings",
return_value=SETTINGS,
):
with mock_nextdns():
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)

state = hass.states.get("switch.fake_profile_web3")
assert state
Expand Down