mirror of
https://github.com/home-assistant/core.git
synced 2025-08-31 02:11:32 +02:00
Improve test assertions to check log record attributes instead of text content
Co-authored-by: MartinHjelmare <3181692+MartinHjelmare@users.noreply.github.com>
This commit is contained in:
@@ -943,10 +943,13 @@ async def test_config_entry_custom_integration(
|
|||||||
# Default without context should be None
|
# Default without context should be None
|
||||||
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
|
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
|
||||||
assert crd.config_entry is None
|
assert crd.config_entry is None
|
||||||
assert (
|
# Should not log any warnings about ContextVar usage for custom integrations
|
||||||
"Detected that custom integration 'my_integration' relies on ContextVar"
|
frame_records = [
|
||||||
not in caplog.text
|
record for record in caplog.records
|
||||||
)
|
if record.name == "homeassistant.helpers.frame"
|
||||||
|
and "relies on ContextVar" in record.message
|
||||||
|
]
|
||||||
|
assert len(frame_records) == 0
|
||||||
|
|
||||||
# Explicit None is OK
|
# Explicit None is OK
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
@@ -954,10 +957,13 @@ async def test_config_entry_custom_integration(
|
|||||||
hass, _LOGGER, name="test", config_entry=None
|
hass, _LOGGER, name="test", config_entry=None
|
||||||
)
|
)
|
||||||
assert crd.config_entry is None
|
assert crd.config_entry is None
|
||||||
assert (
|
# Should not log any warnings about ContextVar usage for custom integrations
|
||||||
"Detected that custom integration 'my_integration' relies on ContextVar"
|
frame_records = [
|
||||||
not in caplog.text
|
record for record in caplog.records
|
||||||
)
|
if record.name == "homeassistant.helpers.frame"
|
||||||
|
and "relies on ContextVar" in record.message
|
||||||
|
]
|
||||||
|
assert len(frame_records) == 0
|
||||||
|
|
||||||
# Explicit entry is OK
|
# Explicit entry is OK
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
@@ -965,10 +971,13 @@ async def test_config_entry_custom_integration(
|
|||||||
hass, _LOGGER, name="test", config_entry=entry
|
hass, _LOGGER, name="test", config_entry=entry
|
||||||
)
|
)
|
||||||
assert crd.config_entry is entry
|
assert crd.config_entry is entry
|
||||||
assert (
|
# Should not log any warnings about ContextVar usage for custom integrations
|
||||||
"Detected that custom integration 'my_integration' relies on ContextVar"
|
frame_records = [
|
||||||
not in caplog.text
|
record for record in caplog.records
|
||||||
)
|
if record.name == "homeassistant.helpers.frame"
|
||||||
|
and "relies on ContextVar" in record.message
|
||||||
|
]
|
||||||
|
assert len(frame_records) == 0
|
||||||
|
|
||||||
# set ContextVar
|
# set ContextVar
|
||||||
config_entries.current_entry.set(entry)
|
config_entries.current_entry.set(entry)
|
||||||
@@ -977,10 +986,13 @@ async def test_config_entry_custom_integration(
|
|||||||
caplog.clear()
|
caplog.clear()
|
||||||
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
|
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
|
||||||
assert crd.config_entry is entry
|
assert crd.config_entry is entry
|
||||||
assert (
|
# Should not log any warnings about ContextVar usage for custom integrations
|
||||||
"Detected that custom integration 'my_integration' relies on ContextVar"
|
frame_records = [
|
||||||
not in caplog.text
|
record for record in caplog.records
|
||||||
)
|
if record.name == "homeassistant.helpers.frame"
|
||||||
|
and "relies on ContextVar" in record.message
|
||||||
|
]
|
||||||
|
assert len(frame_records) == 0
|
||||||
|
|
||||||
# Explicit entry different from ContextVar not recommended, but should work
|
# Explicit entry different from ContextVar not recommended, but should work
|
||||||
another_entry = MockConfigEntry()
|
another_entry = MockConfigEntry()
|
||||||
@@ -989,10 +1001,13 @@ async def test_config_entry_custom_integration(
|
|||||||
hass, _LOGGER, name="test", config_entry=another_entry
|
hass, _LOGGER, name="test", config_entry=another_entry
|
||||||
)
|
)
|
||||||
assert crd.config_entry is another_entry
|
assert crd.config_entry is another_entry
|
||||||
assert (
|
# Should not log any warnings about ContextVar usage for custom integrations
|
||||||
"Detected that custom integration 'my_integration' relies on ContextVar"
|
frame_records = [
|
||||||
not in caplog.text
|
record for record in caplog.records
|
||||||
)
|
if record.name == "homeassistant.helpers.frame"
|
||||||
|
and "relies on ContextVar" in record.message
|
||||||
|
]
|
||||||
|
assert len(frame_records) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_listener_unsubscribe_releases_coordinator(hass: HomeAssistant) -> None:
|
async def test_listener_unsubscribe_releases_coordinator(hass: HomeAssistant) -> None:
|
||||||
|
Reference in New Issue
Block a user