mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Cleanups in area registry tests (#110785)
* Cleanups in area registry tests * Adjust typing
This commit is contained in:
parent
3491dd68b5
commit
df3556f0d8
@ -7,17 +7,20 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import area_registry as ar
|
||||
|
||||
from tests.common import ANY
|
||||
from tests.typing import MockHAClientWebSocket, WebSocketGenerator
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client(hass, hass_ws_client):
|
||||
@pytest.fixture(name="client")
|
||||
async def client_fixture(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> MockHAClientWebSocket:
|
||||
"""Fixture that can interact with the config manager API."""
|
||||
area_registry.async_setup(hass)
|
||||
return await hass_ws_client(hass)
|
||||
|
||||
|
||||
async def test_list_areas(
|
||||
hass: HomeAssistant, client, area_registry: ar.AreaRegistry
|
||||
client: MockHAClientWebSocket, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Test list entries."""
|
||||
area1 = area_registry.async_create("mock 1")
|
||||
@ -28,7 +31,7 @@ async def test_list_areas(
|
||||
picture="/image/example.png",
|
||||
)
|
||||
|
||||
await client.send_json({"id": 1, "type": "config/area_registry/list"})
|
||||
await client.send_json_auto_id({"type": "config/area_registry/list"})
|
||||
|
||||
msg = await client.receive_json()
|
||||
assert msg["result"] == [
|
||||
@ -50,12 +53,12 @@ async def test_list_areas(
|
||||
|
||||
|
||||
async def test_create_area(
|
||||
hass: HomeAssistant, client, area_registry: ar.AreaRegistry
|
||||
client: MockHAClientWebSocket, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Test create entry."""
|
||||
# Create area with only mandatory parameters
|
||||
await client.send_json(
|
||||
{"id": 1, "name": "mock", "type": "config/area_registry/create"}
|
||||
await client.send_json_auto_id(
|
||||
{"name": "mock", "type": "config/area_registry/create"}
|
||||
)
|
||||
|
||||
msg = await client.receive_json()
|
||||
@ -70,9 +73,8 @@ async def test_create_area(
|
||||
assert len(area_registry.areas) == 1
|
||||
|
||||
# Create area with all parameters
|
||||
await client.send_json(
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"id": 2,
|
||||
"aliases": ["alias_1", "alias_2"],
|
||||
"icon": "mdi:garage",
|
||||
"name": "mock 2",
|
||||
@ -94,13 +96,13 @@ async def test_create_area(
|
||||
|
||||
|
||||
async def test_create_area_with_name_already_in_use(
|
||||
hass: HomeAssistant, client, area_registry: ar.AreaRegistry
|
||||
client: MockHAClientWebSocket, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Test create entry that should fail."""
|
||||
area_registry.async_create("mock")
|
||||
|
||||
await client.send_json(
|
||||
{"id": 1, "name": "mock", "type": "config/area_registry/create"}
|
||||
await client.send_json_auto_id(
|
||||
{"name": "mock", "type": "config/area_registry/create"}
|
||||
)
|
||||
|
||||
msg = await client.receive_json()
|
||||
@ -112,7 +114,7 @@ async def test_create_area_with_name_already_in_use(
|
||||
|
||||
|
||||
async def test_delete_area(
|
||||
hass: HomeAssistant, client, area_registry: ar.AreaRegistry
|
||||
client: MockHAClientWebSocket, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Test delete entry."""
|
||||
area = area_registry.async_create("mock")
|
||||
@ -128,13 +130,13 @@ async def test_delete_area(
|
||||
|
||||
|
||||
async def test_delete_non_existing_area(
|
||||
hass: HomeAssistant, client, area_registry: ar.AreaRegistry
|
||||
client: MockHAClientWebSocket, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Test delete entry that should fail."""
|
||||
area_registry.async_create("mock")
|
||||
|
||||
await client.send_json(
|
||||
{"id": 1, "area_id": "", "type": "config/area_registry/delete"}
|
||||
await client.send_json_auto_id(
|
||||
{"area_id": "", "type": "config/area_registry/delete"}
|
||||
)
|
||||
|
||||
msg = await client.receive_json()
|
||||
@ -146,14 +148,13 @@ async def test_delete_non_existing_area(
|
||||
|
||||
|
||||
async def test_update_area(
|
||||
hass: HomeAssistant, client, area_registry: ar.AreaRegistry
|
||||
client: MockHAClientWebSocket, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Test update entry."""
|
||||
area = area_registry.async_create("mock 1")
|
||||
|
||||
await client.send_json(
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"id": 1,
|
||||
"aliases": ["alias_1", "alias_2"],
|
||||
"area_id": area.id,
|
||||
"icon": "mdi:garage",
|
||||
@ -174,9 +175,8 @@ async def test_update_area(
|
||||
}
|
||||
assert len(area_registry.areas) == 1
|
||||
|
||||
await client.send_json(
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"id": 2,
|
||||
"aliases": ["alias_1", "alias_1"],
|
||||
"area_id": area.id,
|
||||
"icon": None,
|
||||
@ -198,14 +198,13 @@ async def test_update_area(
|
||||
|
||||
|
||||
async def test_update_area_with_same_name(
|
||||
hass: HomeAssistant, client, area_registry: ar.AreaRegistry
|
||||
client: MockHAClientWebSocket, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Test update entry."""
|
||||
area = area_registry.async_create("mock 1")
|
||||
|
||||
await client.send_json(
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"id": 1,
|
||||
"area_id": area.id,
|
||||
"name": "mock 1",
|
||||
"type": "config/area_registry/update",
|
||||
@ -220,15 +219,14 @@ async def test_update_area_with_same_name(
|
||||
|
||||
|
||||
async def test_update_area_with_name_already_in_use(
|
||||
hass: HomeAssistant, client, area_registry: ar.AreaRegistry
|
||||
client: MockHAClientWebSocket, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Test update entry."""
|
||||
area = area_registry.async_create("mock 1")
|
||||
area_registry.async_create("mock 2")
|
||||
|
||||
await client.send_json(
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"id": 1,
|
||||
"area_id": area.id,
|
||||
"name": "mock 2",
|
||||
"type": "config/area_registry/update",
|
||||
|
@ -3,24 +3,10 @@ from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import area_registry as ar
|
||||
|
||||
from tests.common import ANY, flush_store
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def update_events(hass):
|
||||
"""Capture update events."""
|
||||
events = []
|
||||
|
||||
@callback
|
||||
def async_capture(event):
|
||||
events.append(event.data)
|
||||
|
||||
hass.bus.async_listen(ar.EVENT_AREA_REGISTRY_UPDATED, async_capture)
|
||||
|
||||
return events
|
||||
from tests.common import ANY, async_capture_events, flush_store
|
||||
|
||||
|
||||
async def test_list_areas(area_registry: ar.AreaRegistry) -> None:
|
||||
@ -32,10 +18,10 @@ async def test_list_areas(area_registry: ar.AreaRegistry) -> None:
|
||||
assert len(areas) == len(area_registry.areas)
|
||||
|
||||
|
||||
async def test_create_area(
|
||||
hass: HomeAssistant, area_registry: ar.AreaRegistry, update_events
|
||||
) -> None:
|
||||
async def test_create_area(hass: HomeAssistant, area_registry: ar.AreaRegistry) -> None:
|
||||
"""Make sure that we can create an area."""
|
||||
update_events = async_capture_events(hass, ar.EVENT_AREA_REGISTRY_UPDATED)
|
||||
|
||||
# Create area with only mandatory parameters
|
||||
area = area_registry.async_create("mock")
|
||||
|
||||
@ -52,8 +38,10 @@ async def test_create_area(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(update_events) == 1
|
||||
assert update_events[-1]["action"] == "create"
|
||||
assert update_events[-1]["area_id"] == area.id
|
||||
assert update_events[-1].data == {
|
||||
"action": "create",
|
||||
"area_id": area.id,
|
||||
}
|
||||
|
||||
# Create area with all parameters
|
||||
area = area_registry.async_create(
|
||||
@ -73,14 +61,17 @@ async def test_create_area(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(update_events) == 2
|
||||
assert update_events[-1]["action"] == "create"
|
||||
assert update_events[-1]["area_id"] == area.id
|
||||
assert update_events[-1].data == {
|
||||
"action": "create",
|
||||
"area_id": area.id,
|
||||
}
|
||||
|
||||
|
||||
async def test_create_area_with_name_already_in_use(
|
||||
hass: HomeAssistant, area_registry: ar.AreaRegistry, update_events
|
||||
hass: HomeAssistant, area_registry: ar.AreaRegistry
|
||||
) -> None:
|
||||
"""Make sure that we can't create an area with a name already in use."""
|
||||
update_events = async_capture_events(hass, ar.EVENT_AREA_REGISTRY_UPDATED)
|
||||
area1 = area_registry.async_create("mock")
|
||||
|
||||
with pytest.raises(ValueError) as e_info:
|
||||
@ -108,9 +99,11 @@ async def test_create_area_with_id_already_in_use(
|
||||
|
||||
|
||||
async def test_delete_area(
|
||||
hass: HomeAssistant, area_registry: ar.AreaRegistry, update_events
|
||||
hass: HomeAssistant,
|
||||
area_registry: ar.AreaRegistry,
|
||||
) -> None:
|
||||
"""Make sure that we can delete an area."""
|
||||
update_events = async_capture_events(hass, ar.EVENT_AREA_REGISTRY_UPDATED)
|
||||
area = area_registry.async_create("mock")
|
||||
|
||||
area_registry.async_delete(area.id)
|
||||
@ -120,10 +113,14 @@ async def test_delete_area(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(update_events) == 2
|
||||
assert update_events[0]["action"] == "create"
|
||||
assert update_events[0]["area_id"] == area.id
|
||||
assert update_events[1]["action"] == "remove"
|
||||
assert update_events[1]["area_id"] == area.id
|
||||
assert update_events[0].data == {
|
||||
"action": "create",
|
||||
"area_id": area.id,
|
||||
}
|
||||
assert update_events[1].data == {
|
||||
"action": "remove",
|
||||
"area_id": area.id,
|
||||
}
|
||||
|
||||
|
||||
async def test_delete_non_existing_area(area_registry: ar.AreaRegistry) -> None:
|
||||
@ -136,10 +133,9 @@ async def test_delete_non_existing_area(area_registry: ar.AreaRegistry) -> None:
|
||||
assert len(area_registry.areas) == 1
|
||||
|
||||
|
||||
async def test_update_area(
|
||||
hass: HomeAssistant, area_registry: ar.AreaRegistry, update_events
|
||||
) -> None:
|
||||
async def test_update_area(hass: HomeAssistant, area_registry: ar.AreaRegistry) -> None:
|
||||
"""Make sure that we can read areas."""
|
||||
update_events = async_capture_events(hass, ar.EVENT_AREA_REGISTRY_UPDATED)
|
||||
area = area_registry.async_create("mock")
|
||||
|
||||
updated_area = area_registry.async_update(
|
||||
@ -164,10 +160,14 @@ async def test_update_area(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(update_events) == 2
|
||||
assert update_events[0]["action"] == "create"
|
||||
assert update_events[0]["area_id"] == area.id
|
||||
assert update_events[1]["action"] == "update"
|
||||
assert update_events[1]["area_id"] == area.id
|
||||
assert update_events[0].data == {
|
||||
"action": "create",
|
||||
"area_id": area.id,
|
||||
}
|
||||
assert update_events[1].data == {
|
||||
"action": "update",
|
||||
"area_id": area.id,
|
||||
}
|
||||
|
||||
|
||||
async def test_update_area_with_same_name(area_registry: ar.AreaRegistry) -> None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user