|
24 | 24 | from elastic_transport import (
|
25 | 25 | JsonSerializer,
|
26 | 26 | NdjsonSerializer,
|
| 27 | + OrjsonSerializer, |
27 | 28 | SerializationError,
|
28 | 29 | SerializerCollection,
|
29 | 30 | TextSerializer,
|
|
33 | 34 | serializers = SerializerCollection(DEFAULT_SERIALIZERS)
|
34 | 35 |
|
35 | 36 |
|
36 |
| -def test_date_serialization(): |
37 |
| - assert b'{"d":"2010-10-01"}' == JsonSerializer().dumps({"d": date(2010, 10, 1)}) |
| 37 | +@pytest.fixture(params=[JsonSerializer, OrjsonSerializer]) |
| 38 | +def json_serializer(request: pytest.FixtureRequest): |
| 39 | + yield request.param() |
38 | 40 |
|
39 | 41 |
|
40 |
| -def test_decimal_serialization(): |
41 |
| - assert b'{"d":3.8}' == JsonSerializer().dumps({"d": Decimal("3.8")}) |
| 42 | +def test_date_serialization(json_serializer): |
| 43 | + assert b'{"d":"2010-10-01"}' == json_serializer.dumps({"d": date(2010, 10, 1)}) |
42 | 44 |
|
43 | 45 |
|
44 |
| -def test_uuid_serialization(): |
45 |
| - assert b'{"d":"00000000-0000-0000-0000-000000000003"}' == JsonSerializer().dumps( |
| 46 | +def test_decimal_serialization(json_serializer): |
| 47 | + assert b'{"d":3.8}' == json_serializer.dumps({"d": Decimal("3.8")}) |
| 48 | + |
| 49 | + |
| 50 | +def test_uuid_serialization(json_serializer): |
| 51 | + assert b'{"d":"00000000-0000-0000-0000-000000000003"}' == json_serializer.dumps( |
46 | 52 | {"d": uuid.UUID("00000000-0000-0000-0000-000000000003")}
|
47 | 53 | )
|
48 | 54 |
|
49 | 55 |
|
50 | 56 | def test_serializes_nan():
|
51 | 57 | assert b'{"d":NaN}' == JsonSerializer().dumps({"d": float("NaN")})
|
| 58 | + # NaN is invalid JSON, and orjson silently converts it to null |
| 59 | + assert b'{"d":null}' == OrjsonSerializer().dumps({"d": float("NaN")}) |
52 | 60 |
|
53 | 61 |
|
54 |
| -def test_raises_serialization_error_on_dump_error(): |
| 62 | +def test_raises_serialization_error_on_dump_error(json_serializer): |
55 | 63 | with pytest.raises(SerializationError):
|
56 |
| - JsonSerializer().dumps(object()) |
| 64 | + json_serializer.dumps(object()) |
57 | 65 | with pytest.raises(SerializationError):
|
58 | 66 | TextSerializer().dumps({})
|
59 | 67 |
|
60 | 68 |
|
61 |
| -def test_raises_serialization_error_on_load_error(): |
| 69 | +def test_raises_serialization_error_on_load_error(json_serializer): |
62 | 70 | with pytest.raises(SerializationError):
|
63 |
| - JsonSerializer().loads(object()) |
| 71 | + json_serializer.loads(object()) |
64 | 72 | with pytest.raises(SerializationError):
|
65 |
| - JsonSerializer().loads(b"{{") |
| 73 | + json_serializer.loads(b"{{") |
66 | 74 |
|
67 | 75 |
|
68 |
| -def test_unicode_is_handled(): |
69 |
| - j = JsonSerializer() |
| 76 | +def test_json_unicode_is_handled(json_serializer): |
70 | 77 | assert (
|
71 |
| - j.dumps({"你好": "你好"}) |
| 78 | + json_serializer.dumps({"你好": "你好"}) |
72 | 79 | == b'{"\xe4\xbd\xa0\xe5\xa5\xbd":"\xe4\xbd\xa0\xe5\xa5\xbd"}'
|
73 | 80 | )
|
74 |
| - assert j.loads(b'{"\xe4\xbd\xa0\xe5\xa5\xbd":"\xe4\xbd\xa0\xe5\xa5\xbd"}') == { |
75 |
| - "你好": "你好" |
76 |
| - } |
| 81 | + assert json_serializer.loads( |
| 82 | + b'{"\xe4\xbd\xa0\xe5\xa5\xbd":"\xe4\xbd\xa0\xe5\xa5\xbd"}' |
| 83 | + ) == {"你好": "你好"} |
| 84 | + |
77 | 85 |
|
78 |
| - t = TextSerializer() |
79 |
| - assert t.dumps("你好") == b"\xe4\xbd\xa0\xe5\xa5\xbd" |
80 |
| - assert t.loads(b"\xe4\xbd\xa0\xe5\xa5\xbd") == "你好" |
| 86 | +def test_text_unicode_is_handled(): |
| 87 | + text_serializer = TextSerializer() |
| 88 | + assert text_serializer.dumps("你好") == b"\xe4\xbd\xa0\xe5\xa5\xbd" |
| 89 | + assert text_serializer.loads(b"\xe4\xbd\xa0\xe5\xa5\xbd") == "你好" |
81 | 90 |
|
82 | 91 |
|
83 |
| -def test_unicode_surrogates_handled(): |
84 |
| - j = JsonSerializer() |
| 92 | +def test_json_unicode_surrogates_handled(): |
85 | 93 | assert (
|
86 |
| - j.dumps({"key": "你好\uda6a"}) |
| 94 | + JsonSerializer().dumps({"key": "你好\uda6a"}) |
87 | 95 | == b'{"key":"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa"}'
|
88 | 96 | )
|
89 |
| - assert j.loads(b'{"key":"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa"}') == { |
90 |
| - "key": "你好\uda6a" |
91 |
| - } |
| 97 | + assert JsonSerializer().loads( |
| 98 | + b'{"key":"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa"}' |
| 99 | + ) == {"key": "你好\uda6a"} |
| 100 | + |
| 101 | + # orjson is strict about UTF-8 |
| 102 | + with pytest.raises(SerializationError): |
| 103 | + OrjsonSerializer().dumps({"key": "你好\uda6a"}) |
| 104 | + |
| 105 | + with pytest.raises(SerializationError): |
| 106 | + OrjsonSerializer().loads(b'{"key":"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa"}') |
| 107 | + |
92 | 108 |
|
93 |
| - t = TextSerializer() |
94 |
| - assert t.dumps("你好\uda6a") == b"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa" |
95 |
| - assert t.loads(b"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa") == "你好\uda6a" |
| 109 | +def test_text_unicode_surrogates_handled(json_serializer): |
| 110 | + text_serializer = TextSerializer() |
| 111 | + assert ( |
| 112 | + text_serializer.dumps("你好\uda6a") == b"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa" |
| 113 | + ) |
| 114 | + assert ( |
| 115 | + text_serializer.loads(b"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa") == "你好\uda6a" |
| 116 | + ) |
96 | 117 |
|
97 | 118 |
|
98 | 119 | def test_deserializes_json_by_default():
|
|
0 commit comments