def test_import_protocol(self): # Try it with the filename namespace = {} extprot.import_protocol(pfile,namespace) assert issubclass(namespace["person"],Message) assert issubclass(namespace["address_book"],Message) # Try it with filelike object namespace = {} extprot.import_protocol(open(pfile,"r"),namespace) assert issubclass(namespace["person"],Message) assert issubclass(namespace["address_book"],Message)
p2 = person("Lauren",2,optional.Unset,[("123456",phone_type.Home)]) p3 = person("Aidan",3) book1 = address_book([p1,p2,p3]) book2 = address_book.from_string(book1.to_string()) assert book1 == book2 return TestAddressBook # test the hard-crafted translation at the start of this file Test_handcrafted = make_cases(**globals()) Test_handcrafted.__name__ = "Test_handcrafted" # test the dynamic in-memory compilation file = path.join(path.dirname(__file__),"../../examples/address_book.proto") dynamic = {} extprot.import_protocol(file,dynamic) Test_dynamic = make_cases(**dynamic) Test_dynamic.__name__ = "Test_dynamic" # test the to-source-code compilation file = path.join(path.dirname(__file__),"../../examples/address_book.proto") compiled = {} tdir = tempfile.mkdtemp() try: modfile = path.join(tdir,"address_book.py") with open(modfile,"wt") as f: extprot.compile_protocol(file,f) execfile(modfile,compiled) Test_compiled = make_cases(**compiled) Test_compiled.__name__ = "Test_compiled"
id = types.Field(types.Int) title = types.Field(types.String) actors = types.Field(types.List.build(types.String)) class recording(types.Union): class Vinyl(types.Message): title = types.Field(types.String) class CD(types.Message): title = types.Field(types.String) class OnOff(types.Message): is_on = types.Field(types.Bool) file = path.join(path.dirname(__file__),"../../examples/address_book.proto") extprot.import_protocol(file,globals(),__name__) class TestTypes(unittest.TestCase): def test_pickling_compiled(self): p1 = person("Guido",7) assert pickle.loads(pickle.dumps(p1)) == p1 def test_pickling_manual(self): m1 = movie(1,"Bad Eggs",["Mick Molloy","Judith Lucy"]) assert pickle.loads(pickle.dumps(m1)) == m1 def test_pickling_union(self): cd = recording.CD("Delta's Greated Hits") assert pickle.loads(pickle.dumps(cd)) == cd