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
|
import typing as T
from dataclasses import field
import marshmallow as mm
import marshmallow_dataclass as mmdc
import collections.abc
import unittest
# Test from https://github.com/lovasoa/marshmallow_dataclass/issues/125
class TestPostDump(unittest.TestCase):
def setUp(self) -> None:
class BaseSchema(mm.Schema):
SKIP_VALUES = {None}
@mm.post_dump
def remove_skip_values(self, data, many):
return {
key: value
for key, value in data.items()
if not isinstance(value, collections.abc.Hashable)
or value not in self.SKIP_VALUES
}
@mmdc.add_schema(base_schema=BaseSchema)
@mmdc.dataclass
class Item:
key: str
val: str
Schema: T.ClassVar[T.Type[mm.Schema]] = mm.Schema
@mmdc.add_schema(base_schema=BaseSchema)
@mmdc.dataclass
class Items:
other: T.Optional[str]
items: T.List[Item] = field(default_factory=list)
Schema: T.ClassVar[T.Type[mm.Schema]] = mm.Schema
self.BaseSchema = BaseSchema
self.Item = Item
self.Items = Items
self.data = {"items": [{"key": "any key", "val": "any value"}]}
def test_class_schema(self):
items_schema = mmdc.class_schema(self.Items, base_schema=self.BaseSchema)()
items = items_schema.load(self.data)
self.assertEqual(
{"items": [{"key": "any key", "val": "any value"}]},
items_schema.dump(items),
)
def test_inside_schema(self):
items = self.Items.Schema().load(self.data)
self.assertEqual(
{"items": [{"key": "any key", "val": "any value"}]},
self.Items.Schema().dump(items),
)
|