From 49df0e99cceeea15c10552ce4bcaefbf8911a5b3 Mon Sep 17 00:00:00 2001 From: Tim O'Farrell Date: Tue, 17 Oct 2023 17:34:05 -0600 Subject: [PATCH] Now marshalling OptionalExternalTypes --- marshy_config_default/__init__.py | 2 ++ tests/test_marshall_json_str.py | 34 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/test_marshall_json_str.py diff --git a/marshy_config_default/__init__.py b/marshy_config_default/__init__.py index 2ae3135..033b7ed 100644 --- a/marshy_config_default/__init__.py +++ b/marshy_config_default/__init__.py @@ -1,3 +1,4 @@ +from typing import Optional from uuid import UUID from marshy.factory.dataclass_marshaller_factory import DataclassMarshallerFactory @@ -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])) diff --git a/tests/test_marshall_json_str.py b/tests/test_marshall_json_str.py new file mode 100644 index 0000000..b6d781e --- /dev/null +++ b/tests/test_marshall_json_str.py @@ -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)