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

Deduplicate otbr tests #124270

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 37 additions & 1 deletion tests/components/otbr/conftest.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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."""
Expand Down
76 changes: 19 additions & 57 deletions tests/components/otbr/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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, {})
Expand Down