Skip to content

Commit

Permalink
Now marshalling OptionalExternalTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
tofarr committed Oct 17, 2023
1 parent 8c49b13 commit 49df0e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions marshy_config_default/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
from uuid import UUID

from marshy.factory.dataclass_marshaller_factory import DataclassMarshallerFactory
Expand Down Expand Up @@ -38,3 +39,4 @@ def configure(context: MarshallerContext):
context.register_factory(TupleMarshallerFactory())
context.register_marshaller(JsonStrMarshaller(ExternalType))
context.register_marshaller(JsonStrMarshaller(ExternalItemType))
context.register_marshaller(JsonStrMarshaller(Optional[ExternalItemType]))
34 changes: 34 additions & 0 deletions tests/test_marshall_json_str.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Optional
from unittest import TestCase

import marshy
from marshy import ExternalType
from marshy.types import ExternalItemType


class TestMarshallJsonStr(TestCase):

def test_marshall_external_type(self):
value = {"foo": [1, True, "bar"]}
dumped = marshy.dump(value, ExternalType)
self.assertEqual('{"foo": [1, true, "bar"]}', dumped)
result = marshy.load(ExternalType, dumped)
self.assertEqual(value, result)

def test_marshall_external_item_type(self):
value = {"foo": [1, True, "bar"]}
dumped = marshy.dump(value, ExternalItemType)
self.assertEqual('{"foo": [1, true, "bar"]}', dumped)
result = marshy.load(ExternalItemType, dumped)
self.assertEqual(value, result)

def test_marshall_optional_external_item_type(self):
value = {"foo": [1, True, "bar"]}
dumped = marshy.dump(value, Optional[ExternalItemType])
self.assertEqual('{"foo": [1, true, "bar"]}', dumped)
result = marshy.load(Optional[ExternalItemType], dumped)
self.assertEqual(value, result)
dumped = marshy.dump(None, Optional[ExternalItemType])
self.assertEqual("null", dumped)
result = marshy.load(Optional[ExternalType], dumped)
self.assertIsNone(result)

0 comments on commit 49df0e9

Please sign in to comment.