Make backends store bytes instead of strings

This is, for the majority of backends, the native format anyway, and so
we save encoding and decoding when using the PickleCodec or if (in future)
a orjson Coder was to be added.

For the JsonCodec, the only thing that changed is the location where the
JSON data is encoded to bytes and decoded back again to a string.
This commit is contained in:
Martijn Pieters
2023-05-09 18:17:28 +01:00
parent 5f2fcf3581
commit 23d439f83a
8 changed files with 54 additions and 45 deletions

View File

@@ -36,7 +36,7 @@ class PDItem(BaseModel):
)
def test_pickle_coder(value: Any) -> None:
encoded_value = PickleCoder.encode(value)
assert isinstance(encoded_value, str)
assert isinstance(encoded_value, bytes)
decoded_value = PickleCoder.decode(encoded_value)
assert decoded_value == value
@@ -55,12 +55,12 @@ def test_pickle_coder(value: Any) -> None:
)
def test_json_coder(value: Any, return_type) -> None:
encoded_value = JsonCoder.encode(value)
assert isinstance(encoded_value, str)
assert isinstance(encoded_value, bytes)
decoded_value = JsonCoder.decode_as_type(encoded_value, type_=return_type)
assert decoded_value == value
def test_json_coder_validation_error() -> None:
invalid = '{"name": "incomplete"}'
invalid = b'{"name": "incomplete"}'
with pytest.raises(ValidationError):
JsonCoder.decode_as_type(invalid, type_=PDItem)