示例#1
0
def test_packer_translation():
    import msgpack
    from pseud.packer import Packer

    class A(object):
        def __init__(self, arg):
            self.arg = arg

        def __eq__(self, other):
            return self.arg == other.arg

    table = {
        5: (A, lambda obj: msgpack.packb(obj.arg),
            lambda data: A(msgpack.unpackb(data)))
    }
    packer = Packer(translation_table=table)
    assert packer.packb({b'key':
                         A(b'--')}) == (b'\x81\xc4\x03key\xc7\x03\x05\xa2--')
    assert packer.unpackb(packer.packb({'key': A(b'arg')})) == {
        'key': A(b'arg')
    }

    packer.register_ext_handler(0, A, lambda obj: b'overidden',
                                lambda data: A(b'arbitrary'))
    # Checks pack_cache is valid
    assert packer.unpackb(packer.packb({'key': A(b'arg')})) != {
        'key': A(b'arg')
    }

    # Mostly for coverage of error paths.
    class B(object):
        pass

    # Two different error paths.
    with pytest.raises(TypeError):
        packer.packb(B())
    with pytest.raises(TypeError):
        packer.packb(B())

    dumb_packer = Packer()
    dumb_packer.unpackb(packer.packb(A('')))
示例#2
0
def test_packer_translation():
    import msgpack
    from pseud.packer import Packer

    class A(object):
        def __init__(self, arg):
            self.arg = arg

        def __eq__(self, other):
            return self.arg == other.arg

    table = {5: (A,
                 lambda obj: msgpack.packb(obj.arg),
                 lambda data: A(msgpack.unpackb(data)))}
    packer = Packer(translation_table=table)
    assert packer.packb({b'key': A(b'--')}) == (
        b'\x81\xc4\x03key\xc7\x03\x05\xa2--')
    assert packer.unpackb(
        packer.packb({'key': A(b'arg')})) == {'key': A(b'arg')}

    packer.register_ext_handler(
        0, A, lambda obj: b'overidden', lambda data: A(b'arbitrary'))
    # Checks pack_cache is valid
    assert packer.unpackb(
        packer.packb({'key': A(b'arg')})) != {'key': A(b'arg')}

    # Mostly for coverage of error paths.
    class B(object):
        pass

    # Two different error paths.
    with pytest.raises(TypeError):
        packer.packb(B())
    with pytest.raises(TypeError):
        packer.packb(B())

    dumb_packer = Packer()
    dumb_packer.unpackb(packer.packb(A('')))
示例#3
0
def test_packer_failure():
    import msgpack
    from pseud.packer import Packer
    packer = Packer()
    with pytest.raises(msgpack.ExtraData):
        packer.unpackb(b'--')
示例#4
0
def test_packer_normal():
    from pseud.packer import Packer
    packer = Packer()
    assert packer.unpackb(packer.packb('data')) == 'data'
示例#5
0
def test_packer_failure():
    import msgpack
    from pseud.packer import Packer
    packer = Packer()
    with pytest.raises(msgpack.ExtraData):
        packer.unpackb(b'--')
示例#6
0
def test_packer_normal():
    from pseud.packer import Packer
    packer = Packer()
    assert packer.unpackb(packer.packb('data')) == 'data'