mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 02:49:40 +00:00
Add created_at/modified_at for area registry (#122014)
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
"""Tests for the Area Registry."""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from functools import partial
|
||||
from typing import Any
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -11,6 +13,7 @@ from homeassistant.helpers import (
|
||||
floor_registry as fr,
|
||||
label_registry as lr,
|
||||
)
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from tests.common import ANY, async_capture_events, flush_store
|
||||
|
||||
@@ -24,7 +27,11 @@ 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) -> None:
|
||||
async def test_create_area(
|
||||
hass: HomeAssistant,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
area_registry: ar.AreaRegistry,
|
||||
) -> None:
|
||||
"""Make sure that we can create an area."""
|
||||
update_events = async_capture_events(hass, ar.EVENT_AREA_REGISTRY_UPDATED)
|
||||
|
||||
@@ -40,9 +47,13 @@ async def test_create_area(hass: HomeAssistant, area_registry: ar.AreaRegistry)
|
||||
name="mock",
|
||||
normalized_name=ANY,
|
||||
picture=None,
|
||||
created_at=utcnow(),
|
||||
modified_at=utcnow(),
|
||||
)
|
||||
assert len(area_registry.areas) == 1
|
||||
|
||||
freezer.tick(timedelta(minutes=5))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(update_events) == 1
|
||||
@@ -52,14 +63,14 @@ async def test_create_area(hass: HomeAssistant, area_registry: ar.AreaRegistry)
|
||||
}
|
||||
|
||||
# Create area with all parameters
|
||||
area = area_registry.async_create(
|
||||
area2 = area_registry.async_create(
|
||||
"mock 2",
|
||||
aliases={"alias_1", "alias_2"},
|
||||
labels={"label1", "label2"},
|
||||
picture="/image/example.png",
|
||||
)
|
||||
|
||||
assert area == ar.AreaEntry(
|
||||
assert area2 == ar.AreaEntry(
|
||||
aliases={"alias_1", "alias_2"},
|
||||
floor_id=None,
|
||||
icon=None,
|
||||
@@ -68,15 +79,19 @@ async def test_create_area(hass: HomeAssistant, area_registry: ar.AreaRegistry)
|
||||
name="mock 2",
|
||||
normalized_name=ANY,
|
||||
picture="/image/example.png",
|
||||
created_at=utcnow(),
|
||||
modified_at=utcnow(),
|
||||
)
|
||||
assert len(area_registry.areas) == 2
|
||||
assert area.created_at != area2.created_at
|
||||
assert area.modified_at != area2.modified_at
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(update_events) == 2
|
||||
assert update_events[-1].data == {
|
||||
"action": "create",
|
||||
"area_id": area.id,
|
||||
"area_id": area2.id,
|
||||
}
|
||||
|
||||
|
||||
@@ -150,11 +165,18 @@ async def test_update_area(
|
||||
area_registry: ar.AreaRegistry,
|
||||
floor_registry: fr.FloorRegistry,
|
||||
label_registry: lr.LabelRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Make sure that we can read areas."""
|
||||
created_at = datetime.fromisoformat("2024-01-01T01:00:00+00:00")
|
||||
freezer.move_to(created_at)
|
||||
update_events = async_capture_events(hass, ar.EVENT_AREA_REGISTRY_UPDATED)
|
||||
floor_registry.async_create("first")
|
||||
area = area_registry.async_create("mock")
|
||||
assert area.modified_at == created_at
|
||||
|
||||
modified_at = datetime.fromisoformat("2024-02-01T01:00:00+00:00")
|
||||
freezer.move_to(modified_at)
|
||||
|
||||
updated_area = area_registry.async_update(
|
||||
area.id,
|
||||
@@ -176,6 +198,8 @@ async def test_update_area(
|
||||
name="mock1",
|
||||
normalized_name=ANY,
|
||||
picture="/image/example.png",
|
||||
created_at=created_at,
|
||||
modified_at=modified_at,
|
||||
)
|
||||
assert len(area_registry.areas) == 1
|
||||
|
||||
@@ -285,6 +309,8 @@ async def test_loading_area_from_storage(
|
||||
"labels": ["mock-label1", "mock-label2"],
|
||||
"name": "mock",
|
||||
"picture": "blah",
|
||||
"created_at": utcnow().isoformat(),
|
||||
"modified_at": utcnow().isoformat(),
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -329,6 +355,8 @@ async def test_migration_from_1_1(
|
||||
"labels": [],
|
||||
"name": "mock",
|
||||
"picture": None,
|
||||
"created_at": "1970-01-01T00:00:00+00:00",
|
||||
"modified_at": "1970-01-01T00:00:00+00:00",
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user