Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Nobuaki Sukegawa | 64b8f6c | 2015-10-10 02:12:48 +0900 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 22 | import glob |
| 23 | import os |
| 24 | import sys |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 25 | import time |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 26 | import unittest |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 27 | from optparse import OptionParser |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 28 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 29 | parser = OptionParser() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 30 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 31 | default='gen-py', |
| 32 | help='include this local directory in sys.path for locating generated code') |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 33 | parser.add_option("--port", type="int", dest="port", |
| 34 | help="connect to server at port") |
| 35 | parser.add_option("--host", type="string", dest="host", |
| 36 | help="connect to server") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 37 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 38 | help="use zlib wrapper for compressed transport") |
| 39 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 40 | help="use SSL for encrypted transport") |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 41 | parser.add_option("--http", dest="http_path", |
| 42 | help="Use the HTTP transport with the specified path") |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 43 | parser.add_option('-v', '--verbose', action="store_const", |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 44 | dest="verbose", const=2, |
| 45 | help="verbose output") |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 46 | parser.add_option('-q', '--quiet', action="store_const", |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 47 | dest="verbose", const=0, |
| 48 | help="minimal output") |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 49 | parser.add_option('--protocol', dest="proto", type="string", |
| 50 | help="protocol to use, one of: accel, binary, compact, json") |
| 51 | parser.add_option('--transport', dest="trans", type="string", |
| 52 | help="transport to use, one of: buffered, framed") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 53 | parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary') |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 54 | options, args = parser.parse_args() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 55 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 56 | script_dir = os.path.abspath(os.path.dirname(__file__)) |
| 57 | lib_dir = os.path.join(os.path.dirname(os.path.dirname(script_dir)), 'lib', 'py', 'build', 'lib.*') |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 58 | sys.path.insert(0, os.path.join(script_dir, options.genpydir)) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 59 | sys.path.insert(0, glob.glob(lib_dir)[0]) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 60 | |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 61 | from ThriftTest import ThriftTest, SecondService |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 62 | from ThriftTest.ttypes import * |
| 63 | from thrift.transport import TTransport |
| 64 | from thrift.transport import TSocket |
| 65 | from thrift.transport import THttpClient |
| 66 | from thrift.transport import TZlibTransport |
| 67 | from thrift.protocol import TBinaryProtocol |
| 68 | from thrift.protocol import TCompactProtocol |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 69 | from thrift.protocol import TJSONProtocol |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 70 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 71 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 72 | class AbstractTest(unittest.TestCase): |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 73 | def setUp(self): |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 74 | if options.http_path: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 75 | self.transport = THttpClient.THttpClient(options.host, port=options.port, path=options.http_path) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 76 | else: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 77 | if options.ssl: |
| 78 | from thrift.transport import TSSLSocket |
| 79 | socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False) |
| 80 | else: |
| 81 | socket = TSocket.TSocket(options.host, options.port) |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 82 | # frame or buffer depending upon args |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 83 | self.transport = TTransport.TBufferedTransport(socket) |
| 84 | if options.trans == 'framed': |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 85 | self.transport = TTransport.TFramedTransport(socket) |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 86 | elif options.trans == 'buffered': |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 87 | self.transport = TTransport.TBufferedTransport(socket) |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 88 | elif options.trans == '': |
| 89 | raise AssertionError('Unknown --transport option: %s' % options.trans) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 90 | if options.zlib: |
| 91 | self.transport = TZlibTransport.TZlibTransport(self.transport, 9) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 92 | self.transport.open() |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 93 | protocol = self.protocol_factory.getProtocol(self.transport) |
jfarrell | dbf2bb5 | 2014-07-09 23:37:12 -0400 | [diff] [blame] | 94 | self.client = ThriftTest.Client(protocol) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 95 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 96 | def tearDown(self): |
| 97 | # Close! |
| 98 | self.transport.close() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 99 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 100 | def testVoid(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 101 | print('testVoid') |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 102 | self.client.testVoid() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 103 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 104 | def testString(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 105 | print('testString') |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 106 | self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 107 | self.assertEqual(self.client.testString(''), '') |
Nobuaki Sukegawa | 64b8f6c | 2015-10-10 02:12:48 +0900 | [diff] [blame] | 108 | self.assertEqual(self.client.testString(u'パイソン'.encode('utf8')), u'パイソン'.encode('utf8')) |
| 109 | s = u"""Afrikaans, Alemannisch, Aragonés, العربية, مصرى, |
| 110 | Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, |
| 111 | Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, |
| 112 | বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, |
| 113 | Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, |
| 114 | Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, |
| 115 | Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, |
| 116 | Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, |
| 117 | Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, |
| 118 | Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, |
| 119 | Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, |
| 120 | ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, |
| 121 | Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, |
| 122 | Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa |
| 123 | Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa |
| 124 | Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, |
| 125 | Norsk (nynorsk), Norsk (bokmål), Nouormand, Diné bizaad, |
| 126 | Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, |
| 127 | Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, |
| 128 | Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple |
| 129 | English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, |
| 130 | Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, |
| 131 | Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, |
| 132 | Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, |
| 133 | Bân-lâm-gú, 粵語""" |
| 134 | self.assertEqual(self.client.testString(s.encode('utf8')), s.encode('utf8')) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 135 | |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 136 | def testBool(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 137 | print('testBool') |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 138 | self.assertEqual(self.client.testBool(True), True) |
| 139 | self.assertEqual(self.client.testBool(False), False) |
| 140 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 141 | def testByte(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 142 | print('testByte') |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 143 | self.assertEqual(self.client.testByte(63), 63) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 144 | self.assertEqual(self.client.testByte(-127), -127) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 145 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 146 | def testI32(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 147 | print('testI32') |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 148 | self.assertEqual(self.client.testI32(-1), -1) |
| 149 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 150 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 151 | def testI64(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 152 | print('testI64') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 153 | self.assertEqual(self.client.testI64(1), 1) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 154 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 155 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 156 | def testDouble(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 157 | print('testDouble') |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 158 | self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 159 | self.assertEqual(self.client.testDouble(0), 0) |
| 160 | self.assertEqual(self.client.testDouble(-1), -1) |
Nobuaki Sukegawa | 228b328 | 2015-10-10 03:11:49 +0900 | [diff] [blame^] | 161 | self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 162 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 163 | def testBinary(self): |
| 164 | if isinstance(self, JSONTest): |
| 165 | self.skipTest('JSON protocol does not handle binary correctly.') |
| 166 | print('testBinary') |
| 167 | val = bytearray([i for i in range(0, 256)]) |
| 168 | self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val) |
| 169 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 170 | def testStruct(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 171 | print('testStruct') |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 172 | x = Xtruct() |
| 173 | x.string_thing = "Zero" |
| 174 | x.byte_thing = 1 |
| 175 | x.i32_thing = -3 |
| 176 | x.i64_thing = -5 |
| 177 | y = self.client.testStruct(x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 178 | self.assertEqual(y, x) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 179 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 180 | def testNest(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 181 | print('testNest') |
| 182 | inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 183 | x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 184 | y = self.client.testNest(x) |
| 185 | self.assertEqual(y, x) |
| 186 | |
| 187 | def testMap(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 188 | print('testMap') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 189 | x = {0:1, 1:2, 2:3, 3:4, -1:-2} |
| 190 | y = self.client.testMap(x) |
| 191 | self.assertEqual(y, x) |
| 192 | |
| 193 | def testSet(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 194 | print('testSet') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 195 | x = set([8, 1, 42]) |
| 196 | y = self.client.testSet(x) |
| 197 | self.assertEqual(y, x) |
| 198 | |
| 199 | def testList(self): |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 200 | print('testList') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 201 | x = [1, 4, 9, -42] |
| 202 | y = self.client.testList(x) |
| 203 | self.assertEqual(y, x) |
| 204 | |
| 205 | def testEnum(self): |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 206 | print('testEnum') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 207 | x = Numberz.FIVE |
| 208 | y = self.client.testEnum(x) |
| 209 | self.assertEqual(y, x) |
| 210 | |
| 211 | def testTypedef(self): |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 212 | print('testTypedef') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 213 | x = 0xffffffffffffff # 7 bytes of 0xff |
| 214 | y = self.client.testTypedef(x) |
| 215 | self.assertEqual(y, x) |
| 216 | |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 217 | @unittest.skip('Cannot use dict as dict key') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 218 | def testMapMap(self): |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 219 | print('testMapMap') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 220 | # does not work: dict() is not a hashable type, so a dict() cannot be used as a key in another dict() |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 221 | x = {{1: 10, 2: 20}, {1: 100, 2: 200, 3: 300}, {1: 1000, 2: 2000, 3: 3000, 4: 4000}} |
| 222 | y = self.client.testMapMap(x) |
| 223 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 224 | |
| 225 | def testMulti(self): |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 226 | print('testMulti') |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 227 | xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 228 | y = self.client.testMulti(xpected.byte_thing, |
| 229 | xpected.i32_thing, |
| 230 | xpected.i64_thing, |
| 231 | { 0:'abc' }, |
| 232 | Numberz.FIVE, |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 233 | 0xf0f0f0) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 234 | self.assertEqual(y, xpected) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 235 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 236 | def testException(self): |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 237 | print('testException') |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 238 | self.client.testException('Safe') |
| 239 | try: |
| 240 | self.client.testException('Xception') |
| 241 | self.fail("should have gotten exception") |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 242 | except Xception as x: |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 243 | self.assertEqual(x.errorCode, 1001) |
| 244 | self.assertEqual(x.message, 'Xception') |
Roger Meier | 61188a4 | 2011-11-01 21:03:33 +0000 | [diff] [blame] | 245 | # TODO ensure same behavior for repr within generated python variants |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 246 | # ensure exception's repr method works |
Roger Meier | 61188a4 | 2011-11-01 21:03:33 +0000 | [diff] [blame] | 247 | #x_repr = repr(x) |
| 248 | #self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 249 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 250 | try: |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 251 | self.client.testException('TException') |
| 252 | self.fail("should have gotten exception") |
| 253 | except TException as x: |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 254 | pass |
| 255 | |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 256 | # Should not throw |
| 257 | self.client.testException('success') |
| 258 | |
| 259 | def testMultiException(self): |
| 260 | print('testMultiException') |
| 261 | try: |
| 262 | self.client.testMultiException('Xception', 'ignore') |
| 263 | except Xception as ex: |
| 264 | self.assertEqual(ex.errorCode, 1001) |
| 265 | self.assertEqual(ex.message, 'This is an Xception') |
| 266 | |
| 267 | try: |
| 268 | self.client.testMultiException('Xception2', 'ignore') |
| 269 | except Xception2 as ex: |
| 270 | self.assertEqual(ex.errorCode, 2002) |
| 271 | self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2') |
| 272 | |
| 273 | y = self.client.testMultiException('success', 'foobar') |
| 274 | self.assertEqual(y.string_thing, 'foobar') |
| 275 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 276 | def testOneway(self): |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 277 | print('testOneway') |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 278 | start = time.time() |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 279 | self.client.testOneway(1) # type is int, not float |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 280 | end = time.time() |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 281 | self.assertTrue(end - start < 3, |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 282 | "oneway sleep took %f sec" % (end - start)) |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 283 | |
Todd Lipcon | f5dea4c | 2009-12-03 01:18:44 +0000 | [diff] [blame] | 284 | def testOnewayThenNormal(self): |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 285 | print('testOnewayThenNormal') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 286 | self.client.testOneway(1) # type is int, not float |
Todd Lipcon | f5dea4c | 2009-12-03 01:18:44 +0000 | [diff] [blame] | 287 | self.assertEqual(self.client.testString('Python'), 'Python') |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 288 | |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 289 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 290 | class NormalBinaryTest(AbstractTest): |
| 291 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 292 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 293 | class CompactTest(AbstractTest): |
| 294 | protocol_factory = TCompactProtocol.TCompactProtocolFactory() |
| 295 | |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 296 | class JSONTest(AbstractTest): |
| 297 | protocol_factory = TJSONProtocol.TJSONProtocolFactory() |
| 298 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 299 | class AcceleratedBinaryTest(AbstractTest): |
| 300 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 301 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 302 | def suite(): |
| 303 | suite = unittest.TestSuite() |
| 304 | loader = unittest.TestLoader() |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 305 | if options.proto == 'binary': # look for --proto on cmdline |
| 306 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 307 | elif options.proto == 'accel': |
| 308 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
| 309 | elif options.proto == 'compact': |
| 310 | suite.addTest(loader.loadTestsFromTestCase(CompactTest)) |
Roger Meier | 85fb6de | 2012-11-02 00:05:42 +0000 | [diff] [blame] | 311 | elif options.proto == 'json': |
| 312 | suite.addTest(loader.loadTestsFromTestCase(JSONTest)) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 313 | else: |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 314 | raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 315 | return suite |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 316 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 317 | class OwnArgsTestProgram(unittest.TestProgram): |
| 318 | def parseArgs(self, argv): |
| 319 | if args: |
| 320 | self.testNames = args |
| 321 | else: |
| 322 | self.testNames = (self.defaultTest,) |
| 323 | self.createTests() |
| 324 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 325 | if __name__ == "__main__": |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 326 | OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1)) |