Fix flaky nuki tests by preventing teardown race condition (#148795)

This commit is contained in:
J. Nick Koston 2025-07-14 21:03:49 -10:00 committed by GitHub
parent e2cc51f21d
commit 5e883cfb12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 50 additions and 26 deletions

View File

@ -14,26 +14,31 @@ from tests.common import (
)
async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
async def init_integration(
hass: HomeAssistant, mock_nuki_requests: requests_mock.Mocker
) -> MockConfigEntry:
"""Mock integration setup."""
with requests_mock.Mocker() as mock:
# Mocking authentication endpoint
mock.get("http://1.1.1.1:8080/info", json=MOCK_INFO)
mock.get(
mock_nuki_requests.get("http://1.1.1.1:8080/info", json=MOCK_INFO)
mock_nuki_requests.get(
"http://1.1.1.1:8080/list",
json=await async_load_json_array_fixture(hass, "list.json", DOMAIN),
)
mock.get(
"http://1.1.1.1:8080/callback/list",
json=await async_load_json_object_fixture(
callback_list_data = await async_load_json_object_fixture(
hass, "callback_list.json", DOMAIN
),
)
mock.get(
mock_nuki_requests.get(
"http://1.1.1.1:8080/callback/list",
json=callback_list_data,
)
mock_nuki_requests.get(
"http://1.1.1.1:8080/callback/add",
json=await async_load_json_object_fixture(
hass, "callback_add.json", DOMAIN
),
json=await async_load_json_object_fixture(hass, "callback_add.json", DOMAIN),
)
# Mock the callback remove endpoint for teardown
mock_nuki_requests.delete(
requests_mock.ANY,
json={"success": True},
)
entry = await setup_nuki_integration(hass)
await hass.config_entries.async_setup(entry.entry_id)

View File

@ -0,0 +1,13 @@
"""Fixtures for nuki tests."""
from collections.abc import Generator
import pytest
import requests_mock
@pytest.fixture
def mock_nuki_requests() -> Generator[requests_mock.Mocker]:
"""Mock nuki HTTP requests."""
with requests_mock.Mocker() as mock:
yield mock

View File

@ -3,6 +3,7 @@
from unittest.mock import patch
import pytest
import requests_mock
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import Platform
@ -19,9 +20,10 @@ async def test_binary_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_nuki_requests: requests_mock.Mocker,
) -> None:
"""Test binary sensors."""
with patch("homeassistant.components.nuki.PLATFORMS", [Platform.BINARY_SENSOR]):
entry = await init_integration(hass)
entry = await init_integration(hass, mock_nuki_requests)
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)

View File

@ -2,6 +2,7 @@
from unittest.mock import patch
import requests_mock
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import Platform
@ -17,9 +18,10 @@ async def test_locks(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_nuki_requests: requests_mock.Mocker,
) -> None:
"""Test locks."""
with patch("homeassistant.components.nuki.PLATFORMS", [Platform.LOCK]):
entry = await init_integration(hass)
entry = await init_integration(hass, mock_nuki_requests)
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)

View File

@ -2,6 +2,7 @@
from unittest.mock import patch
import requests_mock
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import Platform
@ -17,9 +18,10 @@ async def test_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_nuki_requests: requests_mock.Mocker,
) -> None:
"""Test sensors."""
with patch("homeassistant.components.nuki.PLATFORMS", [Platform.SENSOR]):
entry = await init_integration(hass)
entry = await init_integration(hass, mock_nuki_requests)
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)