def test_sproto_protocal(self): with open("protocol.spb", "r") as fh: content = fh.read() sp = sproto_create(content) tag, req, resp = sproto_protocol(sp, "foo") self.assertEqual(req, None) self.assertNotEqual(resp, None)
def test_fixed_point(self): with open("person.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "Person") number = 3.1415 msg = sproto_encode(st, { 'pi': number, }) decode_result, r = sproto_decode(st, msg) self.assertEqual(decode_result['pi'], number)
def test_unicode_string(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "Person") origin_str = u'hello' msg = sproto_encode(st, { 'name': origin_str, }) decode_result, r = sproto_decode(st, msg) self.assertEqual(decode_result['name'], origin_str)
def test_long_long_cutoff(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") b = pow(2, 63) - 1 msg = sproto_encode(st, { "b" : b, }) decode_result, r = sproto_decode(st, msg) self.assertEqual(decode_result["b"], b)
def test_long_string_encode(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") origin_str = "hello"*100000 msg = sproto_encode(st, { "a" : origin_str, }) decode_result, r = sproto_decode(st, msg) self.assertEqual(decode_result["a"], origin_str)
def test_long_long_cutoff(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") b = pow(2, 63) - 1 msg = sproto_encode(st, { "b": b, }) decode_result, r = sproto_decode(st, msg) self.assertEqual(decode_result["b"], b)
def test_long_string_encode(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") origin_str = "hello" * 100000 msg = sproto_encode(st, { "a": origin_str, }) decode_result, r = sproto_decode(st, msg) self.assertEqual(decode_result["a"], origin_str)
def test_complex_encode_decode(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") source = { "a": "hello", "b": 1000000, "c": True, "d": { "world": { "a": "world", #skip b "c": -1, }, "two": { "a": "two", "b": True, }, "": { "a": "", "b": False, "c": 1, }, }, "e": ["ABC", "", "def"], "f": [-3, -2, -1, 0, 1, 2], "g": [True, False, True], "h": [ { "b": 100 }, {}, { "b": -100, "c": False }, { "b": 0, "e": ["test"] }, ], } msg = sproto_encode(st, source) dest, r = sproto_decode(st, msg) #import pprint #pprint.pprint(sproto_decode(st, msg)) self.assertEqual(dest, source) self.assertEqual(r, len(msg))
def test_complex_encode_decode(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") source = { "g" : [True, False, True, False], } msg = sproto_encode(st, source) for i in xrange(1000): dest, r = sproto_decode(st, msg) self.assertEqual(dest, source) self.assertEqual(r, len(msg))
def test_empty_table(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") source = { "d": {}, "e": [], "f": [], "g": [], "h": [], } msg = sproto_encode(st, source) dest, r = sproto_decode(st, msg) #import pprint #pprint.pprint(sproto_decode(st, msg)) self.assertEqual(dest, source) self.assertEqual(r, len(msg))
def test_complex_encode_decode(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") source = { "a" : "hello", "b" : 1000000, "c" : True, "d" : { "world":{ "a" : "world", #skip b "c" : -1, }, "two":{ "a" : "two", "b" : True, }, "":{ "a" : "", "b" : False, "c" : 1, }, }, "e" : ["ABC", "", "def"], "f" : [-3, -2, -1, 0, 1, 2], "g" : [True, False, True], "h" : [ {"b" : 100}, {}, {"b" : -100, "c" : False}, {"b" : 0, "e" : ["test"]}, ], } msg = sproto_encode(st, source) dest, r = sproto_decode(st, msg) #import pprint #pprint.pprint(sproto_decode(st, msg)) self.assertEqual(dest, source) self.assertEqual(r, len(msg))
def test_empty_table(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "foobar") source = { "d" : { }, "e" : [], "f" : [], "g" : [], "h" : [ ], } msg = sproto_encode(st, source) with open("foobar_empty", "w") as fout: fout.write(msg) dest, r = sproto_decode(st, msg) #import pprint #pprint.pprint(sproto_decode(st, msg)) self.assertEqual(dest, source) self.assertEqual(r, len(msg))
def test_map_key_is_first_one(self): with open("protocol.spb", "r") as fh: content = fh.read() sp = sproto_create(content) tag, req, resp = sproto_protocol(sp, "synheroinfos") source = { "herolist": { 1: { "id": 1, "lv": 2, "cfgid": 3, }, 2: { "id": 2, "lv": 2, "cfgid": 4, }, } } msg = sproto_encode(req, source) dest, r = sproto_decode(req, msg) self.assertEqual(dest, source) self.assertEqual(r, len(msg))
def test_sproto_dump(self): with open("testall.spb", "r") as fh: content = fh.read() sp = sproto_create(content) sproto_dump(sp)
def get_st_sp(self): with open("person.spb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "Person") return st, sp
def test_sproto_protocal_refcount(self): with open("protocol.spb", "r") as fh: content = fh.read() sp = sproto_create(content) proto = sproto_protocol(sp, "foobar") self.assertEqual(getrefcount(proto[1])-1, 1)
def get_st_sp(self): with open("person.pb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "Person") return st, sp
def __init__(self, chunk): self.sp = core.sproto_create(chunk) self.st = {} self.proto = {}
def get_st(content, tag): sp = sproto_create(content) st = sproto_type(sp, tag) return st
def test_sproto_protocal_refcount(self): with open("protocol.spb", "r") as fh: content = fh.read() sp = sproto_create(content) proto = sproto_protocol(sp, "foobar") self.assertEqual(getrefcount(proto[1]) - 1, 1)
import pysproto from pysproto import sproto_create, sproto_type, sproto_encode, sproto_decode, sproto_pack, sproto_unpack, sproto_protocol with open("person.pb", "r") as fh: content = fh.read() sp = sproto_create(content) st = sproto_type(sp, "Person") result = sproto_encode(st, { "name": "crystal", "id":1001, "email":"*****@*****.**", "phone":[ { "type" : 1, "number": "10086", }, { "type" : 2, "number":"10010", }, ], }) print "result length:", len(result) print ''.join(["%02x" %ord(x) for x in result]) print "-------------------------" print sproto_decode(st, result) print "=========================" pack_result = sproto_pack(result) print len(pack_result) print ''.join(["%02x" %ord(x) for x in pack_result]) print "-------------------------"