|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | +# pylint: disable=unused-variable |
| 4 | + |
| 5 | +import json |
| 6 | +from copy import deepcopy |
| 7 | +from typing import Annotated, Any, TypeAlias |
| 8 | +from uuid import uuid4 |
| 9 | + |
| 10 | +import pytest |
| 11 | +from common_library.json_serialization import ( |
| 12 | + JsonNamespace, |
| 13 | + SeparatorTuple, |
| 14 | + json_dumps, |
| 15 | + json_loads, |
| 16 | +) |
| 17 | +from faker import Faker |
| 18 | +from pydantic import Field, TypeAdapter |
| 19 | +from pydantic.json import pydantic_encoder |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def fake_data_dict(faker: Faker) -> dict[str, Any]: |
| 24 | + data = { |
| 25 | + "uuid_as_UUID": faker.uuid4(cast_to=None), |
| 26 | + "uuid_as_str": faker.uuid4(), |
| 27 | + "int": faker.pyint(), |
| 28 | + "float": faker.pyfloat(), |
| 29 | + "str": faker.pystr(), |
| 30 | + "dict": faker.pydict(), |
| 31 | + "list": faker.pylist(), |
| 32 | + } |
| 33 | + data["object"] = deepcopy(data) |
| 34 | + return data |
| 35 | + |
| 36 | + |
| 37 | +def test_json_dump_variants(): |
| 38 | + |
| 39 | + uuid_obj = uuid4() |
| 40 | + |
| 41 | + with pytest.raises(TypeError) as exc_info: |
| 42 | + json.dumps(uuid_obj) |
| 43 | + |
| 44 | + assert str(exc_info.value) == "Object of type UUID is not JSON serializable" |
| 45 | + |
| 46 | + assert json_dumps(uuid_obj) == json.dumps(str(uuid_obj)) |
| 47 | + |
| 48 | + |
| 49 | +def test_serialized_non_str_dict_keys(): |
| 50 | + # tests orjson.OPT_NON_STR_KEYS option |
| 51 | + |
| 52 | + # if a dict has a key of a type other than str it will NOT raise |
| 53 | + json_dumps({1: "foo"}) |
| 54 | + |
| 55 | + |
| 56 | +ConstrainedFloat: TypeAlias = Annotated[float, Field(ge=0.0, le=1.0)] |
| 57 | + |
| 58 | + |
| 59 | +def test_serialized_constraint_floats(): |
| 60 | + # test extension of ENCODERS_BY_TYPE used in pydantic_encoder |
| 61 | + |
| 62 | + json_dumps({"value": 1.0}) |
| 63 | + |
| 64 | + # TypeError: Type is not JSON serializable: ProgressPercent |
| 65 | + json_dumps({"value": TypeAdapter(ConstrainedFloat).validate_python(1.0)}) |
| 66 | + |
| 67 | + |
| 68 | +def _expected_json_dumps(obj: Any, default=pydantic_encoder, **json_dumps_kwargs): |
| 69 | + if "indent" not in json_dumps_kwargs: |
| 70 | + json_dumps_kwargs.setdefault( |
| 71 | + "separators", |
| 72 | + SeparatorTuple(item_separator=",", key_separator=":"), # compact separators |
| 73 | + ) |
| 74 | + return json.dumps(obj, default=default, **json_dumps_kwargs) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "kwargs", |
| 79 | + [ |
| 80 | + pytest.param({}, id="no-kw"), |
| 81 | + pytest.param({"sort_keys": True}, id="sort_keys-kw"), |
| 82 | + pytest.param( |
| 83 | + {"separators": (",", ":")}, id="default_separators-kw" |
| 84 | + ), # NOTE: e.g. engineio.packet has `self.json.dumps(self.data, separators=(',', ':'))` |
| 85 | + pytest.param( |
| 86 | + {"indent": 2}, id="indent-kw" |
| 87 | + ), # NOTE: only one-to-one with indent=2 |
| 88 | + ], |
| 89 | +) |
| 90 | +def test_compatiblity_with_json_interface( |
| 91 | + fake_data_dict: dict[str, Any], kwargs: dict[str, Any] |
| 92 | +): |
| 93 | + orjson_dump = JsonNamespace.dumps(fake_data_dict, **kwargs) |
| 94 | + json_dump = _expected_json_dumps(fake_data_dict, **kwargs) |
| 95 | + |
| 96 | + # NOTE: cannot compare dumps directly because orjson compacts it more |
| 97 | + assert json_loads(orjson_dump) == json_loads(json_dump) |
0 commit comments