1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
from dataclasses import dataclass, field
from datetime import date, datetime
import pytest
from mashumaro import DataClassDictMixin, field_options
from mashumaro.helper import pass_through
from mashumaro.types import SerializationStrategy
def test_field_options_helper():
assert field_options() == {
"serialize": None,
"deserialize": None,
"serialization_strategy": None,
"alias": None,
}
def serialize(x):
return x # pragma: no cover
def deserialize(x):
return x # pragma: no cover
class TestSerializationStrategy(SerializationStrategy): # pragma: no cover
def deserialize(self, value):
return value
def serialize(self, value):
return value
serialization_strategy = TestSerializationStrategy()
alias = "alias"
assert field_options(
serialize=serialize,
deserialize=deserialize,
serialization_strategy=serialization_strategy,
alias=alias,
) == {
"serialize": serialize,
"deserialize": deserialize,
"serialization_strategy": serialization_strategy,
"alias": alias,
}
def test_pass_through():
with pytest.raises(NotImplementedError):
pass_through()
assert pass_through.serialize(123) == 123
assert pass_through.deserialize(123) == 123
def test_dataclass_with_pass_through():
@dataclass
class DataClass(DataClassDictMixin):
x: datetime = field(
metadata=field_options(
serialize=pass_through,
deserialize=pass_through,
)
)
y: date = field(
metadata=field_options(serialization_strategy=pass_through)
)
x = datetime.now()
y = x.date()
instance = DataClass(x, y)
assert instance.to_dict() == {"x": x, "y": y}
assert instance.from_dict({"x": x, "y": y}) == instance
|