Use fixtures in UniFi service tests (#118838)

* Use fixtures in UniFi service tests

* Fix comments
This commit is contained in:
Robert Svensson 2024-06-05 17:04:28 +02:00 committed by GitHub
parent c4cfd9adf0
commit 862c04a4b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,34 +1,35 @@
"""deCONZ service tests.""" """deCONZ service tests."""
from typing import Any
from unittest.mock import PropertyMock, patch
import pytest
from homeassistant.components.unifi.const import CONF_SITE_ID, DOMAIN as UNIFI_DOMAIN from homeassistant.components.unifi.const import CONF_SITE_ID, DOMAIN as UNIFI_DOMAIN
from homeassistant.components.unifi.services import ( from homeassistant.components.unifi.services import (
SERVICE_RECONNECT_CLIENT, SERVICE_RECONNECT_CLIENT,
SERVICE_REMOVE_CLIENTS, SERVICE_REMOVE_CLIENTS,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_DEVICE_ID, CONF_HOST from homeassistant.const import ATTR_DEVICE_ID, CONF_HOST
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from .test_hub import setup_unifi_integration
from tests.test_util.aiohttp import AiohttpClientMocker from tests.test_util.aiohttp import AiohttpClientMocker
@pytest.mark.parametrize(
"client_payload", [[{"is_wired": False, "mac": "00:00:00:00:00:01"}]]
)
async def test_reconnect_client( async def test_reconnect_client(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
client_payload: list[dict[str, Any]],
) -> None: ) -> None:
"""Verify call to reconnect client is performed as expected.""" """Verify call to reconnect client is performed as expected."""
clients = [ config_entry = config_entry_setup
{
"is_wired": False,
"mac": "00:00:00:00:00:01",
}
]
config_entry = await setup_unifi_integration(
hass, aioclient_mock, clients_response=clients
)
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
aioclient_mock.post( aioclient_mock.post(
@ -38,7 +39,7 @@ async def test_reconnect_client(
device_entry = device_registry.async_get_or_create( device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, clients[0]["mac"])}, connections={(dr.CONNECTION_NETWORK_MAC, client_payload[0]["mac"])},
) )
await hass.services.async_call( await hass.services.async_call(
@ -50,12 +51,11 @@ async def test_reconnect_client(
assert aioclient_mock.call_count == 1 assert aioclient_mock.call_count == 1
@pytest.mark.usefixtures("config_entry_setup")
async def test_reconnect_non_existant_device( async def test_reconnect_non_existant_device(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Verify no call is made if device does not exist.""" """Verify no call is made if device does not exist."""
await setup_unifi_integration(hass, aioclient_mock)
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
await hass.services.async_call( await hass.services.async_call(
@ -71,9 +71,10 @@ async def test_reconnect_device_without_mac(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
) -> None: ) -> None:
"""Verify no call is made if device does not have a known mac.""" """Verify no call is made if device does not have a known mac."""
config_entry = await setup_unifi_integration(hass, aioclient_mock) config_entry = config_entry_setup
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
@ -91,23 +92,18 @@ async def test_reconnect_device_without_mac(
assert aioclient_mock.call_count == 0 assert aioclient_mock.call_count == 0
@pytest.mark.parametrize(
"client_payload", [[{"is_wired": False, "mac": "00:00:00:00:00:01"}]]
)
async def test_reconnect_client_hub_unavailable( async def test_reconnect_client_hub_unavailable(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
client_payload: list[dict[str, Any]],
) -> None: ) -> None:
"""Verify no call is made if hub is unavailable.""" """Verify no call is made if hub is unavailable."""
clients = [ config_entry = config_entry_setup
{
"is_wired": False,
"mac": "00:00:00:00:00:01",
}
]
config_entry = await setup_unifi_integration(
hass, aioclient_mock, clients_response=clients
)
hub = config_entry.runtime_data
hub.websocket.available = False
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
aioclient_mock.post( aioclient_mock.post(
@ -117,9 +113,13 @@ async def test_reconnect_client_hub_unavailable(
device_entry = device_registry.async_get_or_create( device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, clients[0]["mac"])}, connections={(dr.CONNECTION_NETWORK_MAC, client_payload[0]["mac"])},
) )
with patch(
"homeassistant.components.unifi.UnifiHub.available", new_callable=PropertyMock
) as ws_mock:
ws_mock.return_value = False
await hass.services.async_call( await hass.services.async_call(
UNIFI_DOMAIN, UNIFI_DOMAIN,
SERVICE_RECONNECT_CLIENT, SERVICE_RECONNECT_CLIENT,
@ -133,9 +133,10 @@ async def test_reconnect_client_unknown_mac(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
) -> None: ) -> None:
"""Verify no call is made if trying to reconnect a mac unknown to hub.""" """Verify no call is made if trying to reconnect a mac unknown to hub."""
config_entry = await setup_unifi_integration(hass, aioclient_mock) config_entry = config_entry_setup
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
@ -153,27 +154,24 @@ async def test_reconnect_client_unknown_mac(
assert aioclient_mock.call_count == 0 assert aioclient_mock.call_count == 0
@pytest.mark.parametrize(
"client_payload", [[{"is_wired": True, "mac": "00:00:00:00:00:01"}]]
)
async def test_reconnect_wired_client( async def test_reconnect_wired_client(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
client_payload: list[dict[str, Any]],
) -> None: ) -> None:
"""Verify no call is made if client is wired.""" """Verify no call is made if client is wired."""
clients = [ config_entry = config_entry_setup
{
"is_wired": True,
"mac": "00:00:00:00:00:01",
}
]
config_entry = await setup_unifi_integration(
hass, aioclient_mock, clients_response=clients
)
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
device_entry = device_registry.async_get_or_create( device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, clients[0]["mac"])}, connections={(dr.CONNECTION_NETWORK_MAC, client_payload[0]["mac"])},
) )
await hass.services.async_call( await hass.services.async_call(
@ -185,24 +183,15 @@ async def test_reconnect_wired_client(
assert aioclient_mock.call_count == 0 assert aioclient_mock.call_count == 0
async def test_remove_clients( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "clients_all_payload",
) -> None: [
"""Verify removing different variations of clients work.""" [
clients = [
{ {
"mac": "00:00:00:00:00:00", "mac": "00:00:00:00:00:00",
}, },
{ {"first_seen": 100, "last_seen": 500, "mac": "00:00:00:00:00:01"},
"first_seen": 100, {"first_seen": 100, "last_seen": 1100, "mac": "00:00:00:00:00:02"},
"last_seen": 500,
"mac": "00:00:00:00:00:01",
},
{
"first_seen": 100,
"last_seen": 1100,
"mac": "00:00:00:00:00:02",
},
{ {
"first_seen": 100, "first_seen": 100,
"last_seen": 500, "last_seen": 500,
@ -222,9 +211,15 @@ async def test_remove_clients(
"mac": "00:00:00:00:00:05", "mac": "00:00:00:00:00:05",
}, },
] ]
config_entry = await setup_unifi_integration( ],
hass, aioclient_mock, clients_all_response=clients
) )
async def test_remove_clients(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
) -> None:
"""Verify removing different variations of clients work."""
config_entry = config_entry_setup
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
aioclient_mock.post( aioclient_mock.post(
@ -241,42 +236,52 @@ async def test_remove_clients(
assert await hass.config_entries.async_unload(config_entry.entry_id) assert await hass.config_entries.async_unload(config_entry.entry_id)
async def test_remove_clients_hub_unavailable( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "clients_all_payload",
) -> None: [
"""Verify no call is made if UniFi Network is unavailable.""" [
clients = [
{ {
"first_seen": 100, "first_seen": 100,
"last_seen": 500, "last_seen": 500,
"mac": "00:00:00:00:00:01", "mac": "00:00:00:00:00:01",
} }
] ]
config_entry = await setup_unifi_integration( ],
hass, aioclient_mock, clients_all_response=clients
) )
hub = config_entry.runtime_data @pytest.mark.usefixtures("config_entry_setup")
hub.websocket.available = False async def test_remove_clients_hub_unavailable(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Verify no call is made if UniFi Network is unavailable."""
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True) with patch(
"homeassistant.components.unifi.UnifiHub.available", new_callable=PropertyMock
) as ws_mock:
ws_mock.return_value = False
await hass.services.async_call(
UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True
)
assert aioclient_mock.call_count == 0 assert aioclient_mock.call_count == 0
async def test_remove_clients_no_call_on_empty_list( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker "clients_all_payload",
) -> None: [
"""Verify no call is made if no fitting client has been added to the list.""" [
clients = [
{ {
"first_seen": 100, "first_seen": 100,
"last_seen": 1100, "last_seen": 1100,
"mac": "00:00:00:00:00:01", "mac": "00:00:00:00:00:01",
} }
] ]
await setup_unifi_integration(hass, aioclient_mock, clients_all_response=clients) ],
)
@pytest.mark.usefixtures("config_entry_setup")
async def test_remove_clients_no_call_on_empty_list(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Verify no call is made if no fitting client has been added to the list."""
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True) await hass.services.async_call(UNIFI_DOMAIN, SERVICE_REMOVE_CLIENTS, blocking=True)