Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Nov 15, 2023
1 parent 8597bd9 commit 675783a
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion tests/test_codecs/test_base_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@

from mashumaro.codecs import Decoder, Encoder, decode, encode
from mashumaro.dialect import Dialect
from tests.entities import DataClassWithoutMixin
from tests.entities import (
DataClassWithoutMixin,
GenericNamedTuple,
GenericTypedDict,
MyGenericDataClass,
)

T = TypeVar("T")


@dataclass
class Foo:
foo: str


@dataclass
class Bar:
bar: str


class MyDialect(Dialect):
serialization_strategy = {
date: {
Expand Down Expand Up @@ -160,3 +175,72 @@ def test_value_error_on_encode(shape_type, invalid_value):
with pytest.raises(ValueError) as e:
encoder.encode(invalid_value)
assert type(e.value) is ValueError


def test_with_fields_with_generated_methods():
@dataclass
class MyClass:
td1: GenericTypedDict[str]
td2: GenericTypedDict[date]
nt1: GenericNamedTuple[str]
nt2: GenericNamedTuple[date]
u1: List[Union[int, str]]
u2: List[Union[int, date]]
l1: Literal["l1"]
l2: Literal["l2"]

decoder = Decoder(MyClass)
encoder = Encoder(MyClass)
data = {
"td1": {"x": "2023-11-15", "y": 1},
"td2": {"x": "2023-11-15", "y": 2},
"nt1": ["2023-11-15", 3],
"nt2": ["2023-11-15", 4],
"u1": [5, "2023-11-15"],
"u2": [6, "2023-11-15"],
"l1": "l1",
"l2": "l2",
}
obj = MyClass(
td1={"x": "2023-11-15", "y": 1},
td2={"x": date(2023, 11, 15), "y": 2},
nt1=GenericNamedTuple("2023-11-15", 3),
nt2=GenericNamedTuple(date(2023, 11, 15), 4),
u1=[5, "2023-11-15"],
u2=[6, date(2023, 11, 15)],
l1="l1",
l2="l2",
)
assert decoder.decode(data) == obj
assert encoder.encode(obj) == data


def test_with_two_dataclass_fields():
@dataclass
class MyClass:
x1: Foo
x2: Bar

decoder = Decoder(MyClass)
encoder = Encoder(MyClass)
data = {"x1": {"foo": "foo"}, "x2": {"bar": "bar"}}
obj = MyClass(x1=Foo("foo"), x2=Bar("bar"))
assert decoder.decode(data) == obj
assert encoder.encode(obj) == data


def test_with_two_generic_dataclass_fields():
@dataclass
class MyClass:
x1: MyGenericDataClass[str]
x2: MyGenericDataClass[date]

decoder = Decoder(MyClass)
encoder = Encoder(MyClass)
data = {"x1": {"x": "2023-11-15"}, "x2": {"x": "2023-11-15"}}
obj = MyClass(
x1=MyGenericDataClass("2023-11-15"),
x2=MyGenericDataClass(date(2023, 11, 15)),
)
assert decoder.decode(data) == obj
assert encoder.encode(obj) == data

0 comments on commit 675783a

Please sign in to comment.