Exemplo n.º 1
0
    def test_dict_serialization_serialization_func(self):
        with self.subTest("Serialize dict key serialization function"):
            self.assertEqual({"0": 1},
                             dict_serialization({0: 1},
                                                key_serialization_func=str))

        with self.subTest("Serialize dict value serialization function"):
            self.assertEqual({0: "1"},
                             dict_serialization({0: 1},
                                                value_serialization_func=str))
Exemplo n.º 2
0
    def test_serializer_unpickleable_dataclass(self):
        from _thread import LockType
        from threading import Lock

        locks = [Lock(), Lock()]

        @dataclass
        class UnpickleableDataclass:
            lock: LockType

        dataclass_serializer = Serializer(
            {
                LockType:
                locks.index,
                dict:
                lambda obj: dict_serialization(obj,
                                               value_serialization_func=
                                               dataclass_serializer.serialize),
            },
            {
                LockType: lambda cls, lock_index: locks[lock_index]
            },
        )

        with self.subTest("Serialize unpickleable dataclass"):
            self.assertEqual(
                {"lock": 0},
                dataclass_serializer.serialize(UnpickleableDataclass(
                    locks[0])),
            )

        with self.subTest("Deserialize unpickleable dataclass"):
            self.assertEqual(
                UnpickleableDataclass(locks[1]),
                dataclass_serializer.deserialize(UnpickleableDataclass,
                                                 {"lock": 1}),
            )
    except:
        coerced_obj = None

    if coerced_obj == obj:
        return coerced_obj

    raise DeserializationError("Cannot deserialize {} {!r} to type {}".format(
        type(obj).__name__,
        obj,
        cls.__name__
    ))


BSONSerializer = Serializer(
    serialization_functions={
        dict: lambda dct: dict_serialization(dct, key_serialization_func=BSONSerializer.serialize, value_serialization_func=BSONSerializer.serialize),
        list: lambda lst: list(map(BSONSerializer.serialize, lst)),
        (str, int, float, datetime, bytes, bson.ObjectId, bool, type(None)): noop_serialization
    },
    deserialization_functions={
        dict: lambda cls, dct: dict_deserialization(cls, dct, key_deserialization_func=BSONSerializer.deserialize, value_deserialization_func=BSONSerializer.deserialize),
        list: lambda cls, lst: list_deserialization(cls, lst, deserialization_func=BSONSerializer.deserialize),
        int: bson_int_deserializer,
        bool: noop_deserialization,
        (str, float, datetime, bytes, bson.ObjectId, type(None)): noop_deserialization
    }
)


class BSONSerializerMixin:
    def as_bson(self):
Exemplo n.º 4
0
 def test_dict_serialization_basic(self):
     self.assertEqual({"key": "Value"}, dict_serialization({"key":
                                                            "Value"}))