Exemplo n.º 1
0
    def test_can_serialize_recursive(self):
        """
        When serializing, descend through arrays and subschemas.
        """
        class Some(Schema):
            wow = ArrayField()

        some_full = Some(wow=[1, 2])
        some_nullish = Some(wow=[])

        inner1 = GenericSchema()
        inner2 = GenericSchema(foo=["wow", inner1, some_full, some_nullish],
                               bar=inner1,
                               baz=[inner1, inner1])
        outer = GenericSchema(inner=inner2, cool="ok")
        expected_implicit_nulls = {
            "inner": {
                "foo": ["wow", {
                    "wow": [1, 2]
                }],
            },
            "cool": "ok"
        }
        expected_explicit_nulls = {
            "inner": {
                "foo": ["wow", {}, {
                    "wow": [1, 2]
                }, {
                    "wow": []
                }],
                "bar": {},
                "baz": [{}, {}]
            },
            "cool": "ok"
        }
        self.assertEqual(expected_implicit_nulls,
                         outer.serialize(implicit_nulls=True))
        self.assertEqual(expected_explicit_nulls,
                         outer.serialize(implicit_nulls=False))
Exemplo n.º 2
0
    def test_can_serialize_recursive(self):
        """
        When serializing, descend through arrays and subschemas.
        """
        class Some(Schema):
            wow = ArrayField()

        some_full = Some(wow=[1, 2])
        some_nullish = Some(wow=[])

        inner1 = GenericSchema()
        inner2 = GenericSchema(foo=["wow", inner1, some_full, some_nullish],
                               bar=inner1,
                               baz=[inner1, inner1])
        outer = GenericSchema(inner=inner2, cool="ok")
        expected_implicit_nulls = {
            "inner": {
                "foo": ["wow",
                        {"wow": [1, 2]}],
            },
            "cool": "ok"
        }
        expected_explicit_nulls = {
            "inner": {
                "foo": ["wow",
                        {},
                        {"wow": [1, 2]},
                        {"wow": []}],
                "bar": {},
                "baz": [{}, {}]
            },
            "cool": "ok"
        }
        self.assertEqual(expected_implicit_nulls,
                         outer.serialize(implicit_nulls=True))
        self.assertEqual(expected_explicit_nulls,
                         outer.serialize(implicit_nulls=False))
Exemplo n.º 3
0
 def test_can_serialize_generic(self):
     source = {"foo": 1, "bar": 2, "baz": [3, 4, 5]}
     generic = GenericSchema(**source)
     self.assertEqual(source, generic.serialize())
Exemplo n.º 4
0
 def test_can_serialize_generic(self):
     source = {"foo": 1, "bar": 2, "baz": [3, 4, 5]}
     generic = GenericSchema(**source)
     self.assertEqual(source, generic.serialize())