Ejemplo n.º 1
0
def test_write_decode_error():
    t = TCyMemoryBuffer()
    p = proto.TCyBinaryProtocol(t)

    class T(TPayload):
        thrift_spec = {
            1: (TType.I32, "id", False),
            2: (TType.LIST, "phones", TType.STRING, False),
            3: (TType.STRUCT, "item", TItem, False),
            4: (TType.MAP, "mm", (TType.STRING, (TType.STRUCT, TItem)), False)
        }
        default_spec = [("id", None), ("phones", None), ("item", None),
                        ("mm", None)]

    cases = [
        (T(id="hello"),
         "Field 'id(1)' of 'T' needs type 'I32', but the value is `'hello'`"
         ),  # noqa
        (T(phones=[90, 12]),
         "Field 'phones(2)' of 'T' needs type 'LIST<STRING>', but the value is `[90, 12]`"
         ),  # noqa
        (T(item=12),
         "Field 'item(3)' of 'T' needs type 'TItem', but the value is `12`"
         ),  # noqa
        (T(mm=[45, 56]),
         "Field 'mm(4)' of 'T' needs type 'MAP<STRING, TItem>', but the value is `[45, 56]`"
         )  # noqa
    ]

    for obj, res in cases:
        with pytest.raises(TDecodeException) as exc:
            p.write_struct(obj)
        assert str(exc.value) == res
Ejemplo n.º 2
0
def test_type_tolerance():
    t = TCyMemoryBuffer()
    p = proto.TCyBinaryProtocol(t)

    class T(TPayload):
        thrift_spec = {
            1: (TType.LIST, "phones", TType.STRING, False),
            2: (TType.MAP, "mm", (TType.I32, (TType.LIST, TType.I32)), False)
        }
        default_spec = [("phones", None), ("mm", None)]

    defaultdict = collections.defaultdict(list)
    defaultdict.update({234: [3, 4, 5], 123: [6, 7, 8]})

    cases = [
        T(phones=["123", "234"]),
        T(phones=("123", "234")),
        T(phones={"123", "234"}),
        T(phones={
            "123": 'a',
            "234": 'b'
        }),
        T(mm={
            234: [3, 4, 5],
            123: [6, 7, 8]
        }),
        T(mm=collections.defaultdict(list)),
        T(mm=defaultdict)
    ]

    for obj in cases:
        p.write_struct(obj)
Ejemplo n.º 3
0
def test_read_wrong_arg_type():
    class TWrongTypeItem(TPayload):
        thrift_spec = {
            1: (TType.STRING, "id", False),
            2: (TType.LIST, "phones", TType.STRING, False),
        }
        default_spec = [("id", None), ("phones", None)]

    trans = TCyMemoryBuffer()
    b = proto.TCyBinaryProtocol(trans)
    item = TItem(id=58, phones=["123456", "abcdef"])
    b.write_struct(item)
    b.write_message_end()

    item2 = TWrongTypeItem()
    try:
        b.read_struct(item2)
    except Exception:
        pass

    item3 = TItem(id=123, phones=["123456", "abcdef"])
    b.write_struct(item3)
    b.write_message_end()

    item4 = TItem()
    b.read_struct(item4)

    assert item3 == item4
Ejemplo n.º 4
0
def test_write_empty_struct():
    trans = TCyMemoryBuffer()
    b = proto.TCyBinaryProtocol(trans)
    item = TItem()
    b.write_struct(item)
    b.write_message_end()
    assert "00" == hexlify(trans.getvalue())
Ejemplo n.º 5
0
def test_read_empty_struct():
    b = TCyMemoryBuffer(b"\x00")
    b = proto.TCyBinaryProtocol(b)
    _item = TItem()
    _item2 = TItem()
    b.read_struct(_item2)
    assert _item == _item2
Ejemplo n.º 6
0
def test_write_message_begin_no_strict():
    trans = TCyMemoryBuffer()
    b = proto.TCyBinaryProtocol(trans, strict_write=False)
    b.write_message_begin("test", TType.STRING, 1)
    b.write_message_end()
    assert "00 00 00 04 74 65 73 74 0b 00 00 00 01" == \
        hexlify(trans.getvalue())
Ejemplo n.º 7
0
def test_write_struct():
    trans = TCyMemoryBuffer()
    b = proto.TCyBinaryProtocol(trans)
    item = TItem(id=123, phones=["123456", "abcdef"])
    b.write_struct(item)
    b.write_message_end()
    assert ("08 00 01 00 00 00 7b 0f 00 02 0b 00 00 00 02 00 00 00 "
            "06 31 32 33 34 35 36 00 00 00 06 61 62 63 64 65 66 00") == \
        hexlify(trans.getvalue())
Ejemplo n.º 8
0
def test_read_struct():
    b = TCyMemoryBuffer(b"\x08\x00\x01\x00\x00\x00{"
                        b"\x0f\x00\x02\x0b\x00\x00\x00"
                        b"\x02\x00\x00\x00\x06123456"
                        b"\x00\x00\x00\x06abcdef\x00")
    b = proto.TCyBinaryProtocol(b)
    _item = TItem(id=123, phones=["123456", "abcdef"])
    _item2 = TItem()
    b.read_struct(_item2)
    assert _item == _item2
Ejemplo n.º 9
0
def test_skip_struct():
    b = TCyMemoryBuffer()
    p = proto.TCyBinaryProtocol(b)
    item = TItem(id=123, phones=["123456", "abcdef"])
    p.write_struct(item)
    p.write_message_end()

    proto.write_val(b, TType.I32, 123)
    b.flush()

    proto.skip(b, TType.STRUCT)
    assert 123 == proto.read_val(b, TType.I32)
Ejemplo n.º 10
0
def test_multiple_read_struct():
    t = TCyMemoryBuffer()
    p = proto.TCyBinaryProtocol(t)

    item1 = TItem(id=123, phones=["123456", "abcdef"])
    item2 = TItem(id=234, phones=["110", "120"])
    p.write_struct(item1)
    p.write_struct(item2)
    p.write_message_end()

    _item1 = TItem()
    _item2 = TItem()
    p.read_struct(_item1)
    p.read_struct(_item2)

    assert _item1 == item1 and _item2 == item2
Ejemplo n.º 11
0
def test_write_wrong_arg_type():
    trans = TCyMemoryBuffer()
    b = proto.TCyBinaryProtocol(trans)
    item = TItem(id="wrong type", phones=["123456", "abcdef"])
    try:
        b.write_struct(item)
    except Exception:
        pass
    b.write_message_end()

    item2 = TItem(id=123, phones=["123456", "abcdef"])
    b.write_struct(item2)
    b.write_message_end()
    assert ("08 00 01 00 00 00 7b 0f 00 02 0b 00 00 00 02 00 00 00 "
            "06 31 32 33 34 35 36 00 00 00 06 61 62 63 64 65 66 00") == \
        hexlify(trans.getvalue())
Ejemplo n.º 12
0
def test_read_huge_args():
    class Hello(TPayload):
        thrift_spec = {
            1: (TType.STRING, "name", False),
            2: (TType.STRING, "world", False),
        }
        default_spec = [("name", None), ("world", None)]

    b = TCyMemoryBuffer()
    item = Hello(name='我' * 326, world='你' * 1365)
    p = proto.TCyBinaryProtocol(b)
    p.write_struct(item)
    p.write_message_end()

    item2 = Hello()
    p.read_struct(item2)
Ejemplo n.º 13
0
def test_write_huge_struct():
    b = TCyMemoryBuffer()
    b = proto.TCyBinaryProtocol(b)
    item = TItem(id=12345, phones=["1234567890"] * 100000)
    b.write_struct(item)
    b.write_message_end()
Ejemplo n.º 14
0
def test_read_message_begin_not_strict():
    b = TCyMemoryBuffer(b"\x00\x00\x00\x04test\x0b\x00\x00\x00\x01")
    res = proto.TCyBinaryProtocol(b, strict_read=False).read_message_begin()
    assert res == ("test", TType.STRING, 1)
Ejemplo n.º 15
0
def test_read_message_begin():
    b = TCyMemoryBuffer(b"\x80\x01\x00\x0b\x00\x00\x00\x04test"
                        b"\x00\x00\x00\x01")
    res = proto.TCyBinaryProtocol(b).read_message_begin()
    assert res == ("test", TType.STRING, 1)