summaryrefslogtreecommitdiff
path: root/test/test_stricttype.py
blob: 87e7c1ce1df9e0b18956d03d944c20178f9bcdfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# coding: utf-8

from collections import namedtuple
from msgpack import packb, unpackb, ExtType


def test_namedtuple():
    T = namedtuple('T', "foo bar")
    def default(o):
        if isinstance(o, T):
            return dict(o._asdict())
        raise TypeError('Unsupported type %s' % (type(o),))
    packed = packb(T(1, 42), strict_types=True, use_bin_type=True, default=default)
    unpacked = unpackb(packed, raw=False)
    assert unpacked == {'foo': 1, 'bar': 42}


def test_tuple():
    t = ('one', 2, b'three', (4, ))

    def default(o):
        if isinstance(o, tuple):
            return {
                '__type__': 'tuple',
                'value': list(o),
                }
        raise TypeError('Unsupported type %s' % (type(o),))

    def convert(o):
        if o.get('__type__') == 'tuple':
            return tuple(o['value'])
        return o

    data = packb(t, strict_types=True, use_bin_type=True, default=default)
    expected = unpackb(data, raw=False, object_hook=convert)

    assert expected == t


def test_tuple_ext():
    t = ('one', 2, b'three', (4, ))

    MSGPACK_EXT_TYPE_TUPLE = 0

    def default(o):
        if isinstance(o, tuple):
            # Convert to list and pack
            payload = packb(
                list(o), strict_types=True, use_bin_type=True, default=default)
            return ExtType(MSGPACK_EXT_TYPE_TUPLE, payload)
        raise TypeError(repr(o))

    def convert(code, payload):
        if code == MSGPACK_EXT_TYPE_TUPLE:
            # Unpack and convert to tuple
            return tuple(unpackb(payload, raw=False, ext_hook=convert))
        raise ValueError('Unknown Ext code {}'.format(code))

    data = packb(t, strict_types=True, use_bin_type=True, default=default)
    expected = unpackb(data, raw=False, ext_hook=convert)

    assert expected == t