示例#1
0
def test_encode_repeated_uint_negative():
    with pytest.raises(ValueError) as einfo:
        msg = dict([
            ('counts', [1, -2, 3]),
        ])
        encoder.encode(encoding.sna, msg)
    want = 'ValueError: uint32 value cannot be negative: -2'
    assert einfo.exconly() == want
示例#2
0
def test_encode_uint64_negative():
    with pytest.raises(ValueError) as einfo:
        msg = dict([
            ('bar_id', -155496620801056360),
        ])
        encoder.encode(encoding.bar, msg)
    want = 'ValueError: uint64 value cannot be negative: -155496620801056360'
    assert einfo.exconly() == want
示例#3
0
def test_encode_string():
    # Don't check against data string since protolite doesn't use OrderedDict
    msg = dict([
        ('body', 'hello world'),
    ])
    data = encoder.encode(encoding.message_foo, msg)
    res = encoder.decode(decoding.message_foo, data)
    assert msg == res

    msg = dict([
        ('body', u'\u03b3\u03b5\u03b9\u03b1'),
    ])
    data = encoder.encode(encoding.message_foo, msg)
    res = encoder.decode(decoding.message_foo, data)
    assert msg == res
示例#4
0
def test_encode_string_repeated():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([
        ('messages', ['bar', 'baz']),
    ])
    data = encoder.encode(encoding.message_foo, msg)
    res = encoder.decode(decoding.message_foo, data)
    assert msg == res
示例#5
0
def test_encode_64bit_repeated():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([
        ('snas', [-122.39293670654297, 234.839472104348218943324]),
    ])
    data = encoder.encode(encoding.sna, msg)
    res = encoder.decode(decoding.sna, data)
    assert msg == res
示例#6
0
def test_encode_repeated_varint():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([
        ('sna_ids', [10, 20]),
    ])
    data = encoder.encode(encoding.sna, msg)
    res = encoder.decode(decoding.sna, data)
    assert msg == res
示例#7
0
def test_encode_32bit():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([
        ('bar_value', -122.39293670654297),
    ])
    data = encoder.encode(encoding.bar, msg)
    res = encoder.decode(decoding.bar, data)
    assert msg == res
示例#8
0
def test_encode_32bit_repeated():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([
        ('foos', [-122.39293670654297, 0.8393999934196472]),
    ])
    data = encoder.encode(encoding.sna, msg)
    res = encoder.decode(decoding.sna, data)
    assert msg == res
示例#9
0
def test_encode_uint64():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([
        ('bar_id', 1007843487950966784L),
    ])
    data = encoder.encode(encoding.bar, msg)
    res = encoder.decode(decoding.bar, data)
    assert msg == res
示例#10
0
def test_encode_key_as_varint():
    # Don't check against data string since protolite doesn't use OrderedDict
    msg = dict([
        ('foo_value', 8),
    ])
    data = encoder.encode(encoding.foo, msg)
    res = encoder.decode(decoding.foo, data)
    assert msg == res
示例#11
0
def test_encode_embedded_repeated():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([
        ('bar_id', 30),
        ('foos', [
            dict([('foo_id', 10)]),
            dict([('foo_id', 20)]),
        ]),
    ])
    data = encoder.encode(encoding.bar, msg)
    res = encoder.decode(decoding.bar, data)
    assert msg == res
示例#12
0
def test_encode_delimited_length_as_varint():
    # Don't check against data string since encoder doesn't use OrderedDict
    # We need lots of items to create a large length value
    def _index():
        for i in range(0, 22):
            for j in range(32, 127):
                yield j + (127 * i), chr(j) * (i + 1)

    enc_message = dict()
    for i, c in _index():
        enc_message[c] = dict([
            ('type', 'string'),
            ('field', i),
            ('scope', 'optional'),
        ])
    enc_proto = dict([
        ('message_foo',
         dict([
             ('type', 'embedded'),
             ('field', 305),
             ('message', enc_message),
             ('scope', 'optional'),
         ])),
    ])
    dec_message = dict()
    for i, c in _index():
        dec_message[i] = dict([
            ('type', 'string'),
            ('name', c),
            ('scope', 'optional'),
        ])
    dec_proto = dict([
        (305,
         dict([
             ('type', 'embedded'),
             ('name', 'message_foo'),
             ('message', dec_message),
             ('scope', 'optional'),
         ])),
    ])
    msg = dict()
    for i, c in _index():
        msg[c] = str(i)
    msg = dict([
        ('message_foo', msg),
    ])
    data = encoder.encode(enc_proto, msg)
    res = encoder.decode(dec_proto, data)
    assert msg == res
示例#13
0
def test_encode_embedded():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([
        ('message_baz',
         dict([
             ('baz_id', 12345),
             ('message_bar',
              dict([
                  ('message_foo', dict([
                      ('body', 'foobody'),
                  ])),
                  ('type', 4),
              ])),
         ])),
        ('type', 8),
    ])
    data = encoder.encode(encoding.message_sna, msg)
    res = encoder.decode(decoding.message_sna, data)
    assert msg == res
示例#14
0
 def encode(self, message):
     return encoder.encode(self.encoding, message)
示例#15
0
 def encode(self, message):
     return encoder.encode(self.encoding, message)
示例#16
0
def test_encode_bool():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([('is_foo', False)])
    data = encoder.encode(encoding.foo, msg)
    res = encoder.decode(decoding.foo, data)
    assert msg == res
示例#17
0
def test_encode_enum():
    # Don't check against data string since encoder doesn't use OrderedDict
    msg = dict([('type', 7)])
    data = encoder.encode(encoding.message_bar, msg)
    res = encoder.decode(decoding.message_bar, data)
    assert msg == res
示例#18
0
def test_encode_int32():
    # Don't check against data string since protolite doesn't use OrderedDict
    msg = dict([('foo_count', 127)])
    data = encoder.encode(encoding.foo, msg)
    res = encoder.decode(decoding.foo, data)
    assert msg == res