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:
37
tests/patch_json.py
Normal file
37
tests/patch_json.py
Normal 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,
|
||||
)
|
||||
Reference in New Issue
Block a user