Fail tests which JSON serialize mocks (#144261)

* Fail tests which JSON serialize mocks

* Patch JSON helper earlier

* Check type instead of attribute
This commit is contained in:
Erik Montnemery
2025-05-05 19:06:54 +02:00
committed by GitHub
parent 8a95fffbab
commit 36a08d04c5
2 changed files with 47 additions and 1 deletions

View File

@ -42,11 +42,14 @@ import respx
from syrupy.assertion import SnapshotAssertion
from syrupy.session import SnapshotSession
# Setup patching of JSON functions before any other Home Assistant imports
from . import patch_json # isort:skip
from homeassistant import block_async_io
from homeassistant.exceptions import ServiceNotFound
# Setup patching of recorder functions before any other Home Assistant imports
from . import patch_recorder
from . import patch_recorder # isort:skip
# Setup patching of dt_util time functions before any other Home Assistant imports
from . import patch_time # noqa: F401, isort:skip
@ -449,6 +452,12 @@ def reset_globals() -> Generator[None]:
frame.async_setup(None)
frame._REPORTED_INTEGRATIONS.clear()
# Reset patch_json
if patch_json.mock_objects:
obj = patch_json.mock_objects.pop()
patch_json.mock_objects.clear()
pytest.fail(f"Test attempted to serialize mock object {obj}")
@pytest.fixture(autouse=True, scope="session")
def bcrypt_cost() -> Generator[None]:

37
tests/patch_json.py Normal file
View File

@ -0,0 +1,37 @@
"""Patch JSON related functions."""
from __future__ import annotations
import functools
from typing import Any
from unittest import mock
import orjson
from homeassistant.helpers import json as json_helper
real_json_encoder_default = json_helper.json_encoder_default
mock_objects = []
def json_encoder_default(obj: Any) -> Any:
"""Convert Home Assistant objects.
Hand other objects to the original method.
"""
if isinstance(obj, mock.Base):
mock_objects.append(obj)
raise TypeError(f"Attempting to serialize mock object {obj}")
return real_json_encoder_default(obj)
json_helper.json_encoder_default = json_encoder_default
json_helper.json_bytes = functools.partial(
orjson.dumps, option=orjson.OPT_NON_STR_KEYS, default=json_encoder_default
)
json_helper.json_bytes_sorted = functools.partial(
orjson.dumps,
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS,
default=json_encoder_default,
)