Skip to content

Commit

Permalink
Use meta methods in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pliner committed Sep 26, 2023
1 parent c15950a commit 267b9e3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
90 changes: 45 additions & 45 deletions tests/test_get_field_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,92 +68,92 @@ class Enum(str, enum.Enum):
[
# Any
(Any, {}, m.fields.Raw(allow_none=True, **default_fields(None))),
(Any, mr.metadata(name="i"), m.fields.Raw(allow_none=True, **default_fields(None), **data_key_fields("i"))),
(Any, mr.meta(name="i"), m.fields.Raw(allow_none=True, **default_fields(None), **data_key_fields("i"))),
# Annotated[Any]
(Annotated[Any, "$"], {}, m.fields.Raw(allow_none=True, **default_fields(None))),
(
Annotated[Any, "$"],
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Raw(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
Annotated[Any, mr.metadata(name="i")],
Annotated[Any, mr.meta(name="i")],
{},
m.fields.Raw(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# simple types: bool
(bool, {}, m.fields.Bool(required=True)),
(Optional[bool], {}, m.fields.Bool(allow_none=True, **default_fields(None))),
(bool | None, {}, m.fields.Bool(allow_none=True, **default_fields(None))),
(bool, mr.metadata(name="i"), m.fields.Bool(required=True, **data_key_fields("i"))),
(bool, mr.meta(name="i"), m.fields.Bool(required=True, **data_key_fields("i"))),
(
Optional[bool],
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Bool(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
bool | None,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Bool(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# simple types: str
(str, {}, m.fields.Str(required=True)),
(Optional[str], {}, m.fields.Str(allow_none=True, **default_fields(None))),
(str | None, {}, m.fields.Str(allow_none=True, **default_fields(None))),
(str, mr.metadata(name="i"), m.fields.Str(required=True, **data_key_fields("i"))),
(str, mr.meta(name="i"), m.fields.Str(required=True, **data_key_fields("i"))),
(
Optional[str],
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Str(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
str | None,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Str(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# simple types: int
(int, {}, m.fields.Int(required=True)),
(Optional[int], {}, m.fields.Int(allow_none=True, **default_fields(None))),
(int | None, {}, m.fields.Int(allow_none=True, **default_fields(None))),
(int, mr.metadata(name="i"), m.fields.Int(required=True, **data_key_fields("i"))),
(int, mr.meta(name="i"), m.fields.Int(required=True, **data_key_fields("i"))),
(
Optional[int],
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Int(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
int | None,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Int(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# simple types: float
(float, {}, m.fields.Float(required=True)),
(Optional[float], {}, m.fields.Float(allow_none=True, **default_fields(None))),
(float | None, {}, m.fields.Float(allow_none=True, **default_fields(None))),
(float, mr.metadata(name="i"), m.fields.Float(required=True, **data_key_fields("i"))),
(float, mr.meta(name="i"), m.fields.Float(required=True, **data_key_fields("i"))),
(
Optional[float],
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Float(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
float | None,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Float(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# simple types: uuid
(uuid.UUID, {}, m.fields.UUID(required=True)),
(Optional[uuid.UUID], {}, m.fields.UUID(allow_none=True, **default_fields(None))),
(uuid.UUID | None, {}, m.fields.UUID(allow_none=True, **default_fields(None))),
(uuid.UUID, mr.metadata(name="i"), m.fields.UUID(required=True, **data_key_fields("i"))),
(uuid.UUID, mr.meta(name="i"), m.fields.UUID(required=True, **data_key_fields("i"))),
(
Optional[uuid.UUID],
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.UUID(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
uuid.UUID | None,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.UUID(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# simple types: decimal
Expand All @@ -170,19 +170,19 @@ class Enum(str, enum.Enum):
),
(
decimal.Decimal,
mr.decimal_metadata(name="i", places=4, as_string=False),
mr.decimal_meta(name="i", places=4, as_string=False),
m.fields.Decimal(required=True, **data_key_fields("i"), places=4, as_string=False),
),
(
Optional[decimal.Decimal],
mr.decimal_metadata(name="i", places=4, as_string=False),
mr.decimal_meta(name="i", places=4, as_string=False),
m.fields.Decimal(
allow_none=True, **default_fields(None), places=4, as_string=False, **data_key_fields("i")
),
),
(
decimal.Decimal | None,
mr.decimal_metadata(name="i", places=4, as_string=False),
mr.decimal_meta(name="i", places=4, as_string=False),
m.fields.Decimal(
allow_none=True, **default_fields(None), places=4, as_string=False, **data_key_fields("i")
),
Expand All @@ -201,19 +201,19 @@ class Enum(str, enum.Enum):
),
(
datetime.datetime,
mr.datetime_metadata(name="i", format="%Y-%m-%dT%H:%M:%SZ"),
mr.datetime_meta(name="i", format="%Y-%m-%dT%H:%M:%SZ"),
mr.DateTimeField(required=True, **data_key_fields("i"), format="%Y-%m-%dT%H:%M:%SZ"),
),
(
Optional[datetime.datetime],
mr.datetime_metadata(name="i", format="%Y-%m-%dT%H:%M:%SZ"),
mr.datetime_meta(name="i", format="%Y-%m-%dT%H:%M:%SZ"),
mr.DateTimeField(
allow_none=True, **default_fields(None), **data_key_fields("i"), format="%Y-%m-%dT%H:%M:%SZ"
),
),
(
datetime.datetime | None,
mr.datetime_metadata(name="i", format="%Y-%m-%dT%H:%M:%SZ"),
mr.datetime_meta(name="i", format="%Y-%m-%dT%H:%M:%SZ"),
mr.DateTimeField(
allow_none=True, **default_fields(None), **data_key_fields("i"), format="%Y-%m-%dT%H:%M:%SZ"
),
Expand All @@ -232,32 +232,32 @@ class Enum(str, enum.Enum):
),
(
datetime.date,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Date(required=True, **data_key_fields("i")),
),
(
Optional[datetime.date],
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Date(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
datetime.date | None,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Date(allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# enum
(Enum, {}, mr.EnumField(enum_type=Enum, required=True)),
(Optional[Enum], {}, mr.EnumField(enum_type=Enum, allow_none=True, **default_fields(None))),
(Enum | None, {}, mr.EnumField(enum_type=Enum, allow_none=True, **default_fields(None))),
(Enum, mr.metadata(name="i"), mr.EnumField(enum_type=Enum, required=True, **data_key_fields("i"))),
(Enum, mr.meta(name="i"), mr.EnumField(enum_type=Enum, required=True, **data_key_fields("i"))),
(
Optional[Enum],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.EnumField(enum_type=Enum, allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
Enum | None,
mr.metadata(name="i"),
mr.meta(name="i"),
mr.EnumField(enum_type=Enum, allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# dataclass
Expand All @@ -266,17 +266,17 @@ class Enum(str, enum.Enum):
(EmptyDataclass | None, {}, m.fields.Nested(EMPTY_SCHEMA, allow_none=True, **default_fields(None))),
(
EmptyDataclass,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Nested(EMPTY_SCHEMA, required=True, **data_key_fields("i")),
),
(
Optional[EmptyDataclass],
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Nested(EMPTY_SCHEMA, allow_none=True, **default_fields(None), **data_key_fields("i")),
),
(
EmptyDataclass | None,
mr.metadata(name="i"),
mr.meta(name="i"),
m.fields.Nested(EMPTY_SCHEMA, allow_none=True, **default_fields(None), **data_key_fields("i")),
),
# containers: list[T]
Expand Down Expand Up @@ -703,7 +703,7 @@ class Enum(str, enum.Enum):
(dict[str, Any], {}, mr.DictField(required=True)),
(
dict[str, Any],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(required=True, **data_key_fields("i")),
),
(
Expand All @@ -713,7 +713,7 @@ class Enum(str, enum.Enum):
),
(
Optional[dict[str, Any]],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
allow_none=True,
**default_fields(None),
Expand All @@ -727,7 +727,7 @@ class Enum(str, enum.Enum):
),
(
dict[str, Any] | None,
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
allow_none=True,
**default_fields(None),
Expand All @@ -742,7 +742,7 @@ class Enum(str, enum.Enum):
),
(
dict[datetime.date, int],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
keys=m.fields.Date(required=True),
values=m.fields.Int(required=True),
Expand All @@ -762,7 +762,7 @@ class Enum(str, enum.Enum):
),
(
Optional[dict[datetime.date, int]],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
keys=m.fields.Date(required=True),
values=m.fields.Int(required=True),
Expand All @@ -783,7 +783,7 @@ class Enum(str, enum.Enum):
),
(
dict[datetime.date, int] | None,
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
keys=m.fields.Date(required=True),
values=m.fields.Int(required=True),
Expand All @@ -796,7 +796,7 @@ class Enum(str, enum.Enum):
(collections.abc.Mapping[str, Any], {}, mr.DictField(required=True)),
(
collections.abc.Mapping[str, Any],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(required=True, **data_key_fields("i")),
),
(
Expand All @@ -806,7 +806,7 @@ class Enum(str, enum.Enum):
),
(
Optional[collections.abc.Mapping[str, Any]],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
allow_none=True,
**default_fields(None),
Expand All @@ -820,7 +820,7 @@ class Enum(str, enum.Enum):
),
(
collections.abc.Mapping[str, Any] | None,
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
allow_none=True,
**default_fields(None),
Expand All @@ -835,7 +835,7 @@ class Enum(str, enum.Enum):
),
(
collections.abc.Mapping[datetime.date, int],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
keys=m.fields.Date(required=True),
values=m.fields.Int(required=True),
Expand All @@ -855,7 +855,7 @@ class Enum(str, enum.Enum):
),
(
Optional[collections.abc.Mapping[datetime.date, int]],
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
keys=m.fields.Date(required=True),
values=m.fields.Int(required=True),
Expand All @@ -876,7 +876,7 @@ class Enum(str, enum.Enum):
),
(
collections.abc.Mapping[datetime.date, int] | None,
mr.metadata(name="i"),
mr.meta(name="i"),
mr.DictField(
keys=m.fields.Date(required=True),
values=m.fields.Int(required=True),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Container:
def test_custom_name_bool() -> None:
@dataclasses.dataclass(frozen=True, slots=True, kw_only=True)
class BoolContainer:
bool_field: bool = dataclasses.field(metadata=mr.metadata(name="BoolField"))
bool_field: bool = dataclasses.field(metadata=mr.meta(name="BoolField"))

raw = dict(BoolField=False)

Expand All @@ -243,7 +243,7 @@ class BoolContainer:
def test_custom_name_uuid() -> None:
@dataclasses.dataclass(frozen=True, slots=True, kw_only=True)
class UuidContainer:
uuid_field: uuid.UUID = dataclasses.field(metadata=mr.metadata(name="UuidField"))
uuid_field: uuid.UUID = dataclasses.field(metadata=mr.meta(name="UuidField"))

raw = {"UuidField": "15f75b02-1c34-46a2-92a5-18363aadea05"}

Expand Down
4 changes: 2 additions & 2 deletions tests/test_unknown_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def test_unknown_fields_should_be_excluded() -> None:
@dataclasses.dataclass
class Example:
field_1: str = dataclasses.field(metadata=mr.metadata(name="field_2"))
field_1: str = dataclasses.field(metadata=mr.meta(name="field_2"))

data = {"field_1": "bad", "field_2": "good"}
data_copy = copy.deepcopy(data)
Expand All @@ -21,7 +21,7 @@ class Example:
def test_metadata_name_should_not_use_others_field_name() -> None:
@dataclasses.dataclass
class Example:
field_1: str = dataclasses.field(metadata=mr.metadata(name="field_2"))
field_1: str = dataclasses.field(metadata=mr.meta(name="field_2"))
field_2: str

with pytest.raises(ValueError) as e:
Expand Down
Loading

0 comments on commit 267b9e3

Please sign in to comment.