|
1 | 1 | import datetime |
2 | | -import json |
3 | 2 |
|
4 | 3 | import pytest |
5 | 4 | import sqlalchemy as sa |
| 5 | +from crate.client.http import cratedb_json_encoder |
| 6 | +from orjson import orjson |
6 | 7 |
|
7 | | -from cratedb_toolkit.util.sqlalchemy import CrateJsonEncoderWithNumPy |
8 | 8 | from tests.conftest import TESTDRIVE_DATA_SCHEMA |
9 | 9 |
|
10 | 10 |
|
@@ -48,18 +48,36 @@ def test_inspector_patched(database, needs_sqlalchemy2): |
48 | 48 | def test_json_encoder_date(): |
49 | 49 | """ |
50 | 50 | Verify the extended JSON encoder also accepts Python's `date` types. |
| 51 | +
|
| 52 | + TODO: Move to different test file, as this no longer requires |
| 53 | + monkeypatching after using orjson for marshalling. |
51 | 54 | """ |
52 | 55 | data = {"date": datetime.date(2024, 6, 4)} |
53 | | - encoded = json.dumps(data, cls=CrateJsonEncoderWithNumPy) |
54 | | - assert encoded == '{"date": 1717459200000}' |
| 56 | + # TODO: Refactor core marshalling method out of crate-python's |
| 57 | + # `_create_sql_payload`, to make it reusable here. |
| 58 | + encoded = orjson.dumps( |
| 59 | + data, |
| 60 | + default=cratedb_json_encoder, |
| 61 | + option=(orjson.OPT_PASSTHROUGH_DATETIME | orjson.OPT_SERIALIZE_NUMPY), |
| 62 | + ) |
| 63 | + assert encoded == b'{"date":1717459200000}' |
55 | 64 |
|
56 | 65 |
|
57 | 66 | def test_json_encoder_numpy(): |
58 | 67 | """ |
59 | 68 | Verify the extended JSON encoder also accepts NumPy types. |
| 69 | +
|
| 70 | + TODO: Move to different test file, as this no longer requires |
| 71 | + monkeypatching after using orjson for marshalling. |
60 | 72 | """ |
61 | 73 | np = pytest.importorskip("numpy") |
62 | 74 |
|
63 | 75 | data = {"scalar-int": np.float32(42.42).astype(int), "scalar-float": np.float32(42.42), "ndarray": np.ndarray([1])} |
64 | | - encoded = json.dumps(data, cls=CrateJsonEncoderWithNumPy) |
65 | | - assert encoded == """{"scalar-int": 42, "scalar-float": 42.41999816894531, "ndarray": [2.08e-322]}""" |
| 76 | + # TODO: Refactor core marshalling method out of crate-python's |
| 77 | + # `_create_sql_payload`, to make it reusable here. |
| 78 | + encoded = orjson.dumps( |
| 79 | + data, |
| 80 | + default=cratedb_json_encoder, |
| 81 | + option=(orjson.OPT_PASSTHROUGH_DATETIME | orjson.OPT_SERIALIZE_NUMPY), |
| 82 | + ) |
| 83 | + assert encoded == b"""{"scalar-int":42,"scalar-float":42.42,"ndarray":[2.08e-322]}""" |
0 commit comments