Ejemplo n.º 1
0
    def _dequeue_message_from_stream(self, f):
        """Returns next deserialized message in queue or None."""
        assert self.packer, 'Cannot unpack stream data without packer.'

        obj = self.packer.load(f)
        datatypename = obj[MESSAGE_KEY]
        message = deserialize_from_builtins(obj, Message.class_by_name(datatypename))
        return message
Ejemplo n.º 2
0
def test_deserialize_from_builtins_class():
    class A(Serializable):
        def __init__(self, a: int, b: str):
            super().__init__()
            self.a = a
            self.b = b

        def __eq__(self, other):
            return self.a == other.a and self.b == other.b

    s = {'a': 1, 'b': 'one'}
    expected = A(1, 'one')
    assert deserialize_from_builtins(s, A) == expected
Ejemplo n.º 3
0
def test_deserialize_from_buitlins_default_value():
    class A(Serializable):
        def __init__(self, a: int=42, b: str='forty-two'):
            super().__init__()
            self.a = a
            self.b = b

        def __eq__(self, other):
            return self.a == other.a and self.b == other.b

    a = deserialize_from_builtins({}, A)
    assert a == A()
    assert a.a is 42
    assert a.b == 'forty-two'
Ejemplo n.º 4
0
def test_deserialize_from_builtins_class_of_class():
    class A(Serializable):
        def __init__(self, a: int, b: str):
            super().__init__()
            self.a = a
            self.b = b

        def __eq__(self, other):
            return self.a == other.a and self.b == other.b

    class B(Serializable):
        def __init__(self, a_list: [A], a_dict: {int: A}):
            super().__init__()
            self.a_list = a_list
            self.a_dict = a_dict

        def __eq__(self, other):
            return self.a_dict == other.a_dict and self.a_list == other.a_list

    assert deserialize_from_builtins(
        {
            'a_list': [{'a': 1, 'b': 'one'}, {'a': 2, 'b': 'two'}],
            'a_dict': {1: {'a': 1, 'b': 'one'}, 2: {'a': 2, 'b': 'two'}}
        }, B) == B([A(1, 'one'), A(2, 'two')], {1: A(1, 'one'), 2: A(2, 'two')})
Ejemplo n.º 5
0
def test_deserialize_from_builtins_builtins():
    assert deserialize_from_builtins(5, int) is 5
    assert deserialize_from_builtins('one', str) is 'one'
    assert deserialize_from_builtins([1, 2, 3], list, int) == [1, 2, 3]
    assert deserialize_from_builtins({1: 'one', 2: 'two'}, dict, str) == {1: 'one', 2: 'two'}
Ejemplo n.º 6
0
def test_deserialize_from_builtins_enum():
    class MyEnum(enum.Enum):
        Literal1 = 1
        Literal2 = 2

    assert deserialize_from_builtins('Literal2', MyEnum) == MyEnum.Literal2
Ejemplo n.º 7
0
def test_deserialize_from_builtins_none():
    assert deserialize_from_builtins(None, None) is None