From 76a013c83b7c6937a946c2015b0edb6083e6d6a7 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 20 Aug 2024 08:22:13 +0200 Subject: [PATCH] Deduplicate otbr tests --- tests/components/otbr/conftest.py | 38 ++++++++++++++- tests/components/otbr/test_init.py | 76 ++++++++---------------------- 2 files changed, 56 insertions(+), 58 deletions(-) diff --git a/tests/components/otbr/conftest.py b/tests/components/otbr/conftest.py index 56f29bdc79b4d..8344c73457274 100644 --- a/tests/components/otbr/conftest.py +++ b/tests/components/otbr/conftest.py @@ -1,6 +1,8 @@ """Test fixtures for the Open Thread Border Router integration.""" -from unittest.mock import MagicMock, Mock, patch +from collections.abc import Generator +from typing import Any +from unittest.mock import AsyncMock, MagicMock, Mock, patch import pytest @@ -18,6 +20,40 @@ from tests.common import MockConfigEntry +@pytest.fixture(name="dataset") +def dataset_fixture() -> Any: + """Return the discovery info from the supervisor.""" + return DATASET_CH16 + + +@pytest.fixture(name="get_active_dataset_tlvs") +def get_active_dataset_tlvs_fixture(dataset: Any) -> Generator[AsyncMock]: + """Mock get_active_dataset_tlvs.""" + with patch( + "python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=dataset + ) as get_active_dataset_tlvs: + yield get_active_dataset_tlvs + + +@pytest.fixture(name="get_border_agent_id") +def get_border_agent_id_fixture() -> Generator[AsyncMock]: + """Mock get_border_agent_id.""" + with patch( + "python_otbr_api.OTBR.get_border_agent_id", return_value=TEST_BORDER_AGENT_ID + ) as get_border_agent_id: + yield get_border_agent_id + + +@pytest.fixture(name="get_extended_address") +def get_extended_address_fixture() -> Generator[AsyncMock]: + """Mock get_extended_address.""" + with patch( + "python_otbr_api.OTBR.get_extended_address", + return_value=TEST_BORDER_AGENT_EXTENDED_ADDRESS, + ) as get_extended_address: + yield get_extended_address + + @pytest.fixture(name="otbr_config_entry_multipan") async def otbr_config_entry_multipan_fixture(hass: HomeAssistant) -> None: """Mock Open Thread Border Router config entry.""" diff --git a/tests/components/otbr/test_init.py b/tests/components/otbr/test_init.py index 5cf29662be382..7d54dd16f568c 100644 --- a/tests/components/otbr/test_init.py +++ b/tests/components/otbr/test_init.py @@ -38,6 +38,15 @@ ) +@pytest.fixture(name="enable_mocks", autouse=True) +def enable_mocks_fixture( + get_active_dataset_tlvs: AsyncMock, + get_border_agent_id: AsyncMock, + get_extended_address: AsyncMock, +) -> None: + """Enable API mocks.""" + + async def test_import_dataset( hass: HomeAssistant, mock_async_zeroconf: MagicMock, @@ -66,17 +75,6 @@ async def mock_add_service_listener(type_: str, listener: Any): config_entry.add_to_hass(hass) with ( - patch( - "python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=DATASET_CH16 - ), - patch( - "python_otbr_api.OTBR.get_border_agent_id", - return_value=TEST_BORDER_AGENT_ID, - ), - patch( - "python_otbr_api.OTBR.get_extended_address", - return_value=TEST_BORDER_AGENT_EXTENDED_ADDRESS, - ), patch( "homeassistant.components.thread.dataset_store.BORDER_AGENT_DISCOVERY_TIMEOUT", 0.1, @@ -143,17 +141,6 @@ async def test_import_share_radio_channel_collision( ) config_entry.add_to_hass(hass) with ( - patch( - "python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=DATASET_CH16 - ), - patch( - "python_otbr_api.OTBR.get_border_agent_id", - return_value=TEST_BORDER_AGENT_ID, - ), - patch( - "python_otbr_api.OTBR.get_extended_address", - return_value=TEST_BORDER_AGENT_EXTENDED_ADDRESS, - ), patch( "homeassistant.components.thread.dataset_store.DatasetStore.async_add" ) as mock_add, @@ -193,15 +180,6 @@ async def test_import_share_radio_no_channel_collision( ) config_entry.add_to_hass(hass) with ( - patch("python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=dataset), - patch( - "python_otbr_api.OTBR.get_border_agent_id", - return_value=TEST_BORDER_AGENT_ID, - ), - patch( - "python_otbr_api.OTBR.get_extended_address", - return_value=TEST_BORDER_AGENT_EXTENDED_ADDRESS, - ), patch( "homeassistant.components.thread.dataset_store.DatasetStore.async_add" ) as mock_add, @@ -238,15 +216,6 @@ async def test_import_insecure_dataset( ) config_entry.add_to_hass(hass) with ( - patch("python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=dataset), - patch( - "python_otbr_api.OTBR.get_border_agent_id", - return_value=TEST_BORDER_AGENT_ID, - ), - patch( - "python_otbr_api.OTBR.get_extended_address", - return_value=TEST_BORDER_AGENT_EXTENDED_ADDRESS, - ), patch( "homeassistant.components.thread.dataset_store.DatasetStore.async_add" ) as mock_add, @@ -272,7 +241,9 @@ async def test_import_insecure_dataset( aiohttp.ClientError, ], ) -async def test_config_entry_not_ready(hass: HomeAssistant, error) -> None: +async def test_config_entry_not_ready( + hass: HomeAssistant, get_active_dataset_tlvs: AsyncMock, error +) -> None: """Test raising ConfigEntryNotReady .""" config_entry = MockConfigEntry( @@ -282,11 +253,13 @@ async def test_config_entry_not_ready(hass: HomeAssistant, error) -> None: title="My OTBR", ) config_entry.add_to_hass(hass) - with patch("python_otbr_api.OTBR.get_active_dataset_tlvs", side_effect=error): - assert not await hass.config_entries.async_setup(config_entry.entry_id) + get_active_dataset_tlvs.side_effect = error + assert not await hass.config_entries.async_setup(config_entry.entry_id) -async def test_border_agent_id_not_supported(hass: HomeAssistant) -> None: +async def test_border_agent_id_not_supported( + hass: HomeAssistant, get_border_agent_id: AsyncMock +) -> None: """Test border router does not support border agent ID.""" config_entry = MockConfigEntry( @@ -296,16 +269,8 @@ async def test_border_agent_id_not_supported(hass: HomeAssistant) -> None: title="My OTBR", ) config_entry.add_to_hass(hass) - with ( - patch( - "python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=DATASET_CH16 - ), - patch( - "python_otbr_api.OTBR.get_border_agent_id", - side_effect=python_otbr_api.GetBorderAgentIdNotSupportedError, - ), - ): - assert not await hass.config_entries.async_setup(config_entry.entry_id) + get_border_agent_id.side_effect = python_otbr_api.GetBorderAgentIdNotSupportedError + assert not await hass.config_entries.async_setup(config_entry.entry_id) async def test_config_entry_update(hass: HomeAssistant) -> None: @@ -369,9 +334,6 @@ async def test_remove_extra_entries( config_entry2.add_to_hass(hass) assert len(hass.config_entries.async_entries(otbr.DOMAIN)) == 2 with ( - patch( - "python_otbr_api.OTBR.get_active_dataset_tlvs", return_value=DATASET_CH16 - ), patch("homeassistant.components.otbr.util.compute_pskc"), ): # Patch to speed up tests assert await async_setup_component(hass, otbr.DOMAIN, {})