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,28 +14,33 @@ 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.""" """Mock integration setup."""
with requests_mock.Mocker() as mock: # Mocking authentication endpoint
# Mocking authentication endpoint mock_nuki_requests.get("http://1.1.1.1:8080/info", json=MOCK_INFO)
mock.get("http://1.1.1.1:8080/info", json=MOCK_INFO) mock_nuki_requests.get(
mock.get( "http://1.1.1.1:8080/list",
"http://1.1.1.1:8080/list", json=await async_load_json_array_fixture(hass, "list.json", DOMAIN),
json=await async_load_json_array_fixture(hass, "list.json", DOMAIN), )
) callback_list_data = await async_load_json_object_fixture(
mock.get( hass, "callback_list.json", DOMAIN
"http://1.1.1.1:8080/callback/list", )
json=await async_load_json_object_fixture( mock_nuki_requests.get(
hass, "callback_list.json", DOMAIN "http://1.1.1.1:8080/callback/list",
), json=callback_list_data,
) )
mock.get( mock_nuki_requests.get(
"http://1.1.1.1:8080/callback/add", "http://1.1.1.1:8080/callback/add",
json=await async_load_json_object_fixture( json=await async_load_json_object_fixture(hass, "callback_add.json", DOMAIN),
hass, "callback_add.json", DOMAIN )
), # Mock the callback remove endpoint for teardown
) mock_nuki_requests.delete(
entry = await setup_nuki_integration(hass) requests_mock.ANY,
await hass.config_entries.async_setup(entry.entry_id) json={"success": True},
await hass.async_block_till_done() )
entry = await setup_nuki_integration(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry return entry

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

View File

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

View File

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