Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 2 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 3 | import sys, glob |
| 4 | sys.path.insert(0, './gen-py') |
| 5 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 6 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 7 | from ThriftTest import ThriftTest |
| 8 | from ThriftTest.ttypes import * |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 9 | from thrift.transport import TTransport |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 10 | from thrift.transport import TSocket |
| 11 | from thrift.protocol import TBinaryProtocol |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 12 | import unittest |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 13 | import time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 14 | from optparse import OptionParser |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 15 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 16 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 17 | parser = OptionParser() |
| 18 | |
| 19 | parser.add_option("--port", type="int", dest="port", default=9090) |
| 20 | parser.add_option("--host", type="string", dest="host", default='localhost') |
| 21 | parser.add_option("--framed-input", action="store_true", dest="framed_input") |
| 22 | parser.add_option("--framed-output", action="store_false", dest="framed_output") |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 23 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 24 | (options, args) = parser.parse_args() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 25 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 26 | class AbstractTest(unittest.TestCase): |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 27 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 28 | def setUp(self): |
| 29 | global options |
| 30 | |
| 31 | socket = TSocket.TSocket(options.host, options.port) |
| 32 | |
| 33 | # Frame or buffer depending upon args |
| 34 | if options.framed_input or options.framed_output: |
| 35 | self.transport = TTransport.TFramedTransport(socket, options.framed_input, options.framed_output) |
| 36 | else: |
| 37 | self.transport = TTransport.TBufferedTransport(socket) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 38 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 39 | self.transport.open() |
| 40 | |
| 41 | protocol = self.protocol_factory.getProtocol(self.transport) |
| 42 | self.client = ThriftTest.Client(protocol) |
| 43 | |
| 44 | def tearDown(self): |
| 45 | # Close! |
| 46 | self.transport.close() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 47 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 48 | def testVoid(self): |
| 49 | self.client.testVoid() |
| 50 | |
| 51 | def testString(self): |
| 52 | self.assertEqual(self.client.testString('Python'), 'Python') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 53 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 54 | def testByte(self): |
| 55 | self.assertEqual(self.client.testByte(63), 63) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 56 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 57 | def testI32(self): |
| 58 | self.assertEqual(self.client.testI32(-1), -1) |
| 59 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 60 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 61 | def testI64(self): |
| 62 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 63 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 64 | def testDouble(self): |
| 65 | self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 66 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 67 | def testStruct(self): |
| 68 | x = Xtruct() |
| 69 | x.string_thing = "Zero" |
| 70 | x.byte_thing = 1 |
| 71 | x.i32_thing = -3 |
| 72 | x.i64_thing = -5 |
| 73 | y = self.client.testStruct(x) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 74 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 75 | self.assertEqual(y.string_thing, "Zero") |
| 76 | self.assertEqual(y.byte_thing, 1) |
| 77 | self.assertEqual(y.i32_thing, -3) |
| 78 | self.assertEqual(y.i64_thing, -5) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 79 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 80 | def testException(self): |
| 81 | self.client.testException('Safe') |
| 82 | try: |
| 83 | self.client.testException('Xception') |
| 84 | self.fail("should have gotten exception") |
| 85 | except Xception, x: |
| 86 | self.assertEqual(x.errorCode, 1001) |
| 87 | self.assertEqual(x.message, 'Xception') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 88 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 89 | class NormalBinaryTest(AbstractTest): |
| 90 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 91 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 92 | class AcceleratedBinaryTest(AbstractTest): |
| 93 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 94 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame^] | 95 | suite = unittest.TestSuite() |
| 96 | loader = unittest.TestLoader() |
| 97 | |
| 98 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 99 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
| 100 | |
| 101 | testRunner = unittest.TextTestRunner(verbosity=2) |
| 102 | testRunner.run(suite) |