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_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_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_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 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 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 get_st(content, tag): sp = sproto_create(content) st = sproto_type(sp, tag) return st
def querytype(self, tagname): if not tagname in self.st: self.st[tagname] = core.sproto_type(self.sp, tagname) return self.st[tagname]
def get_st(content, tag): sp = sproto_create(content) st = sproto_type(sp, tag) return st
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 "-------------------------"