mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-11-08 10:29:30 +00:00
Improve json performance by porting core orjson utils (#4816)
* Improve json performance by porting core orjson utils * port relevant tests * pylint * add test for read_json_file * add test for read_json_file * remove workaround for core issue we do not have here --------- Co-authored-by: Pascal Vizeli <pvizeli@syshack.ch>
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
"""test json."""
|
||||
from supervisor.utils.json import write_json_file
|
||||
import time
|
||||
from typing import NamedTuple
|
||||
|
||||
from supervisor.utils.json import json_dumps, read_json_file, write_json_file
|
||||
|
||||
|
||||
def test_file_permissions(tmp_path):
|
||||
@@ -18,3 +21,42 @@ def test_new_file_permissions(tmp_path):
|
||||
|
||||
write_json_file(tempfile, {"test": "data"})
|
||||
assert oct(tempfile.stat().st_mode)[-3:] == "600"
|
||||
|
||||
|
||||
def test_file_round_trip(tmp_path):
|
||||
"""Test file permissions."""
|
||||
tempfile = tmp_path / "test.json"
|
||||
write_json_file(tempfile, {"test": "data"})
|
||||
assert tempfile.is_file()
|
||||
assert oct(tempfile.stat().st_mode)[-3:] == "600"
|
||||
assert read_json_file(tempfile) == {"test": "data"}
|
||||
|
||||
|
||||
def test_json_dumps_float_subclass() -> None:
|
||||
"""Test the json dumps a float subclass."""
|
||||
|
||||
class FloatSubclass(float):
|
||||
"""A float subclass."""
|
||||
|
||||
assert json_dumps({"c": FloatSubclass(1.2)}) == '{"c":1.2}'
|
||||
|
||||
|
||||
def test_json_dumps_tuple_subclass() -> None:
|
||||
"""Test the json dumps a tuple subclass."""
|
||||
|
||||
tt = time.struct_time((1999, 3, 17, 32, 44, 55, 2, 76, 0))
|
||||
|
||||
assert json_dumps(tt) == "[1999,3,17,32,44,55,2,76,0]"
|
||||
|
||||
|
||||
def test_json_dumps_named_tuple_subclass() -> None:
|
||||
"""Test the json dumps a tuple subclass."""
|
||||
|
||||
class NamedTupleSubclass(NamedTuple):
|
||||
"""A NamedTuple subclass."""
|
||||
|
||||
name: str
|
||||
|
||||
nts = NamedTupleSubclass("a")
|
||||
|
||||
assert json_dumps(nts) == '["a"]'
|
||||
|
||||
Reference in New Issue
Block a user