blob: 4be8b8c35afde64d0e472971bbe2db35a81eb1c8 [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):
David Reiss46bb4ae2009-01-14 22:34:15 +000017 self.v1obj = VersioningTestV1(
David Reiss9ff3b9d2008-02-15 01:10:23 +000018 begin_in_both=12345,
19 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000020 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000021
David Reiss46bb4ae2009-01-14 22:34:15 +000022 self.v2obj = VersioningTestV2(
David Reiss9ff3b9d2008-02-15 01:10:23 +000023 begin_in_both=12345,
24 newint=1,
25 newbyte=2,
26 newshort=3,
27 newlong=4,
28 newdouble=5.0,
David Reiss46bb4ae2009-01-14 22:34:15 +000029 newstruct=Bonk(message="Hello!", type=123),
David Reiss9ff3b9d2008-02-15 01:10:23 +000030 newlist=[7,8,9],
31 newset=[42,1,8],
32 newmap={1:2,2:3},
33 newstring="Hola!",
34 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000035 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000036
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))
David Reiss1cc0c5e2008-10-17 19:30:35 +000051 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
52 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +000053
54 def testBackwards(self):
55 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
David Reiss1cc0c5e2008-10-17 19:30:35 +000056 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
57 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +000058
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))