def test_basic_encode_decode(self): st, sp = self.get_st_sp() source = { "name": "crystal", "id": 1001, "email": "*****@*****.**", "phone": [ { "type": 1, "number": "10086", }, { "type": 2, "number": "10010", }, ], } result = sproto_encode(st, source) expected = a2b_hex( "04000000d40700000000070000006372797374616c130000006372797374616c406578616d706c652e636f6d260000000f0000000200000004000500000031303038360f000000020000000600050000003130303130" ) self.assertEqual(result, expected) #print b2a_hex(result) dest, r = sproto_decode(st, result) self.assertEqual(source, dest) self.assertEqual(r, len(expected))
def test_exception_catch(self): st, sp = self.get_st_sp() with self.assertRaises(pysproto.error) as se: tmp = sproto_encode(st, { "name":"t", "id":"fake_id", }) self.assertEqual(se.exception.message, "type mismatch, tag:id, expected int or long")
def test_exception_catch(self): st, sp = self.get_st_sp() with self.assertRaises(pysproto.error) as se: tmp = sproto_encode(st, { "name": "t", "id": "fake_id", }) self.assertEqual(se.exception.message, "type mismatch, tag:id, expected int or long")
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_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_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_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_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_basic_encode_decode(self): st, sp = self.get_st_sp() source = { "name": "crystal", "id":1001, "email":"*****@*****.**", "phone":[ { "type" : 1, "number": "10086", }, { "type" : 2, "number":"10010", }, ], } result = sproto_encode(st, source) expected = a2b_hex("04000000d40700000000070000006372797374616c130000006372797374616c406578616d706c652e636f6d260000000f0000000200000004000500000031303038360f000000020000000600050000003130303130") self.assertEqual(result, expected) #print b2a_hex(result) dest, r = sproto_decode(st, result) self.assertEqual(source, dest) self.assertEqual(r, len(expected))
import pysproto from pysproto import sproto_create, sproto_type, sproto_encode, sproto_decode import gc print "***************************" with open("person.pb", "r") as fh: content = fh.read() def get_st(content, tag): sp = sproto_create(content) st = sproto_type(sp, tag) return st gc.collect() st = get_st(content, "Person") result = sproto_encode(st, { "name": "crystal", "id":1001, "email":"*****@*****.**", "phone":[ { "type" : 1, "number": "10086", }, { "type" : 2, "number":"10010", }, ], }) print sproto_decode(st, result)
def encode(self, st, data): if isinstance(st, basestring): st = self.querytype(st) return core.sproto_encode(st, data)
def get_st(content, tag): sp = sproto_create(content) st = sproto_type(sp, tag) return st gc.collect() st = get_st(content, "Person") result = sproto_encode( st, { "name": "crystal", "id": 1001, "email": "*****@*****.**", "phone": [ { "type": 1, "number": "10086", }, { "type": 2, "number": "10010", }, ], }) print sproto_decode(st, result)
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 "-------------------------"