David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys, glob |
| 4 | sys.path.insert(0, './gen-py') |
| 5 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
| 6 | |
| 7 | from ThriftTest.ttypes import * |
| 8 | from thrift.transport import TTransport |
| 9 | from thrift.transport import TSocket |
| 10 | from thrift.protocol import TBinaryProtocol |
| 11 | import unittest |
| 12 | import time |
| 13 | |
| 14 | class AbstractTest(unittest.TestCase): |
| 15 | |
| 16 | def setUp(self): |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame^] | 17 | self.v1obj = VersioningTestV1( |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 18 | begin_in_both=12345, |
| 19 | end_in_both=54321, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame^] | 20 | ) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 21 | |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame^] | 22 | self.v2obj = VersioningTestV2( |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 23 | begin_in_both=12345, |
| 24 | newint=1, |
| 25 | newbyte=2, |
| 26 | newshort=3, |
| 27 | newlong=4, |
| 28 | newdouble=5.0, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame^] | 29 | newstruct=Bonk(message="Hello!", type=123), |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 30 | newlist=[7,8,9], |
| 31 | newset=[42,1,8], |
| 32 | newmap={1:2,2:3}, |
| 33 | newstring="Hola!", |
| 34 | end_in_both=54321, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame^] | 35 | ) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 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)) |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 51 | self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both) |
| 52 | self.assertEquals(obj.end_in_both, self.v1obj.end_in_both) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 53 | |
| 54 | def testBackwards(self): |
| 55 | obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj)) |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 56 | self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both) |
| 57 | self.assertEquals(obj.end_in_both, self.v2obj.end_in_both) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | class NormalBinaryTest(AbstractTest): |
| 61 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
| 62 | |
| 63 | class AcceleratedBinaryTest(AbstractTest): |
| 64 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
| 65 | |
| 66 | |
| 67 | def 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 | |
| 75 | if __name__ == "__main__": |
| 76 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |