dify/api/tests/unit_tests/libs/test_orjson.py
2025-09-09 17:11:49 +08:00

26 lines
707 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import orjson
import pytest
from libs.orjson import orjson_dumps
def test_orjson_dumps_round_trip_basic():
obj = {"a": 1, "b": [1, 2, 3], "c": {"d": True}}
s = orjson_dumps(obj)
assert orjson.loads(s) == obj
def test_orjson_dumps_with_unicode_and_indent():
obj = {"msg": "你好Dify"}
s = orjson_dumps(obj, option=orjson.OPT_INDENT_2)
# contains indentation newline/spaces
assert "\n" in s
assert orjson.loads(s) == obj
def test_orjson_dumps_non_utf8_encoding_fails():
obj = {"msg": "你好"}
# orjson.dumps() always produces UTF-8 bytes; decoding with non-UTF8 fails.
with pytest.raises(UnicodeDecodeError):
orjson_dumps(obj, encoding="ascii")