1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Use the orjson equivalent default encoder when save_json is passed the default encoder (#74377)

This commit is contained in:
J. Nick Koston
2022-07-04 08:41:23 -05:00
committed by GitHub
parent 4e1359e2cc
commit 8d0e54d776
2 changed files with 42 additions and 3 deletions

View File

@@ -5,12 +5,13 @@ from json import JSONEncoder, dumps
import math
import os
from tempfile import mkdtemp
from unittest.mock import Mock
from unittest.mock import Mock, patch
import pytest
from homeassistant.core import Event, State
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.json import JSONEncoder as DefaultHASSJSONEncoder
from homeassistant.helpers.template import TupleWrapper
from homeassistant.util.json import (
SerializationError,
@@ -127,6 +128,21 @@ def test_custom_encoder():
assert data == "9"
def test_default_encoder_is_passed():
"""Test we use orjson if they pass in the default encoder."""
fname = _path_for("test6")
with patch(
"homeassistant.util.json.orjson.dumps", return_value=b"{}"
) as mock_orjson_dumps:
save_json(fname, {"any": 1}, encoder=DefaultHASSJSONEncoder)
assert len(mock_orjson_dumps.mock_calls) == 1
# Patch json.dumps to make sure we are using the orjson path
with patch("homeassistant.util.json.json.dumps", side_effect=Exception):
save_json(fname, {"any": {1}}, encoder=DefaultHASSJSONEncoder)
data = load_json(fname)
assert data == {"any": [1]}
def test_find_unserializable_data():
"""Find unserializeable data."""
assert find_paths_unserializable_data(1) == {}