You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2020/04/26 11:32:44 UTC

[thrift] branch master updated: supplementary testcase for TJSONProtocol Client: py Patch: zeshuai007 <51382517@qq.com>

This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new b83ae2c  supplementary testcase for TJSONProtocol Client: py Patch: zeshuai007 <51...@qq.com>
b83ae2c is described below

commit b83ae2cedf8a345142c3b96e9b529a65fe41bc16
Author: zeshuai007 <51...@qq.com>
AuthorDate: Fri Apr 24 11:35:55 2020 +0800

    supplementary testcase for TJSONProtocol
    Client: py
    Patch: zeshuai007 <51...@qq.com>
    
    This closes #2113
---
 lib/py/test/thrift_json.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/lib/py/test/thrift_json.py b/lib/py/test/thrift_json.py
index 40e7a47..125ea59 100644
--- a/lib/py/test/thrift_json.py
+++ b/lib/py/test/thrift_json.py
@@ -46,6 +46,71 @@ class TestJSONString(unittest.TestCase):
             unicode_text = unicode_text.encode('utf8')
         self.assertEqual(protocol.readString(), unicode_text)
 
+    def test_TJSONProtocol_write(self):
+        write_data = '{"software":"thrift","1":[23,1.2010000000000001,32767,2147483647,9223372036854775807],"base64":"aGVsbG8gdGhyaWZ0","bool":0}'
+
+        buff = TTransport.TMemoryBuffer()
+        transport = TTransport.TBufferedTransportFactory().getTransport(buff)
+        protocol = TJSONProtocol(transport)
+        protocol.writeJSONObjectStart()
+        protocol.writeJSONString("software")
+        protocol.writeJSONString("thrift")
+        protocol.writeJSONString("1")
+        protocol.writeJSONArrayStart()
+        protocol.writeJSONNumber(23)
+        protocol.writeDouble(1.201)
+        protocol.writeI16(32767)
+        protocol.writeI32(2147483647)
+        protocol.writeI64(9223372036854775807)
+        protocol.writeJSONArrayEnd()
+        protocol.writeJSONString("base64")
+        protocol.writeJSONBase64("hello thrift".encode('utf-8'))
+        protocol.writeJSONString("bool")
+        protocol.writeBool(0)
+        protocol.writeJSONObjectEnd()
+
+        transport.flush()
+        value = buff.getvalue()
+
+        self.assertEqual(write_data, value.decode('utf-8'))
+
+    def test_TJSONProtol_read(self):
+        expected = "{'software':'thrift','1':[23,1.2010000000000001,32767,2147483647,9223372036854775807],'base64':'hello thrift','bool':False}"
+        read_data = '{"software":"thrift","1":[23,1.2010000000000001,32767,2147483647,9223372036854775807],"base64":"aGVsbG8gdGhyaWZ0","bool":0}'
+
+        buff = TTransport.TMemoryBuffer(read_data.encode('utf-8'))
+        transport = TTransport.TBufferedTransportFactory().getTransport(buff)
+        protocol = TJSONProtocol(transport)
+        protocol.readJSONObjectStart()
+        u_1 = protocol.readString()
+        u_2 = protocol.readString()
+        u_3 = protocol.readString()
+        protocol.readJSONArrayStart()
+        u_4 = protocol.readNumber()
+        u_5 = protocol.readDouble()
+        u_6 = protocol.readI16()
+        u_7 = protocol.readI32()
+        u_8 = protocol.readI64()
+        protocol.readJSONArrayEnd()
+        u_9 = protocol.readString()
+        u_10 = protocol.readJSONBase64()
+        u_11 = protocol.readString()
+        u_12 = protocol.readBool()
+        protocol.writeJSONObjectEnd()
+
+        result_read = {}
+        result_read[u_1] = u_2
+        result_read[u_3] = []
+        result_read[u_3].append(u_4)
+        result_read[u_3].append(u_5)
+        result_read[u_3].append(u_6)
+        result_read[u_3].append(u_7)
+        result_read[u_3].append(u_8)
+        result_read[u_9] = u_10.decode('utf-8')
+        result_read[u_11] = u_12
+
+        self.assertEqual(eval(expected), result_read)
+
 
 if __name__ == '__main__':
     unittest.main()