blob: 56bccbf8650ae463fb16e895c94d7c9a0c83e3fc [file] [log] [blame]
David Reiss9ff3b9d2008-02-15 01:10:23 +00001#!/usr/bin/env python
2
3import sys, glob
4sys.path.insert(0, './gen-py')
5sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
6
7from ThriftTest.ttypes import *
8from thrift.transport import TTransport
9from thrift.transport import TSocket
10from thrift.protocol import TBinaryProtocol
11import unittest
12import time
13
14class AbstractTest(unittest.TestCase):
15
16 def setUp(self):
17 self.v1obj = VersioningTestV1(d=dict(
18 begin_in_both=12345,
19 end_in_both=54321,
20 ))
21
22 self.v2obj = VersioningTestV2(d=dict(
23 begin_in_both=12345,
24 newint=1,
25 newbyte=2,
26 newshort=3,
27 newlong=4,
28 newdouble=5.0,
29 newstruct=Bonk(d=dict(message="Hello!", type=123)),
30 newlist=[7,8,9],
31 newset=[42,1,8],
32 newmap={1:2,2:3},
33 newstring="Hola!",
34 end_in_both=54321,
35 ))
36
37 def _serialize(self, obj):
38 trans = TTransport.TMemoryBuffer()
39 prot = self.protocol_factory.getProtocol(trans)
40 obj.write(prot)
41 return trans.getvalue()
42
43 def _deserialize(self, objtype, data):
44 prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
45 ret = objtype()
46 ret.read(prot)
47 return ret
48
49 def testForwards(self):
50 obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj))
51 assert obj.begin_in_both == self.v1obj.begin_in_both
52 assert obj.end_in_both == self.v1obj.end_in_both
53
54 def testBackwards(self):
55 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
56 assert obj.begin_in_both == self.v2obj.begin_in_both
57 assert obj.end_in_both == self.v2obj.end_in_both
58
59
60class NormalBinaryTest(AbstractTest):
61 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
62
63class AcceleratedBinaryTest(AbstractTest):
64 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
65
66
67def suite():
68 suite = unittest.TestSuite()
69 loader = unittest.TestLoader()
70
71 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
72 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
73 return suite
74
75if __name__ == "__main__":
76 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))