From patchwork Mon Dec 21 20:47:28 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Russell Bryant X-Patchwork-Id: 559725 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (li376-54.members.linode.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id 80E42140216 for ; Tue, 22 Dec 2015 07:53:15 +1100 (AEDT) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id BAA9310868; Mon, 21 Dec 2015 12:48:35 -0800 (PST) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx1e4.cudamail.com (mx1.cudamail.com [69.90.118.67]) by archives.nicira.com (Postfix) with ESMTPS id 6638B107EE for ; Mon, 21 Dec 2015 12:48:34 -0800 (PST) Received: from bar5.cudamail.com (unknown [192.168.21.12]) by mx1e4.cudamail.com (Postfix) with ESMTPS id EE76D1E0129 for ; Mon, 21 Dec 2015 13:48:33 -0700 (MST) X-ASG-Debug-ID: 1450730913-09eadd632cc6780001-byXFYA Received: from mx1-pf1.cudamail.com ([192.168.24.1]) by bar5.cudamail.com with ESMTP id qIfMv2YJM8MO3JUB (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 21 Dec 2015 13:48:33 -0700 (MST) X-Barracuda-Envelope-From: russell@ovn.org X-Barracuda-RBL-Trusted-Forwarder: 192.168.24.1 Received: from unknown (HELO mx1.redhat.com) (209.132.183.28) by mx1-pf1.cudamail.com with ESMTPS (DHE-RSA-AES256-SHA encrypted); 21 Dec 2015 20:48:33 -0000 Received-SPF: neutral (mx1-pf1.cudamail.com: 209.132.183.28 is neither permitted nor denied by SPF record at ovn.org) X-Barracuda-Apparent-Source-IP: 209.132.183.28 X-Barracuda-RBL-IP: 209.132.183.28 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 8E9D4C0B7E14; Mon, 21 Dec 2015 20:48:32 +0000 (UTC) Received: from x1c.redhat.com ([10.3.112.11]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tBLKm0dp002018; Mon, 21 Dec 2015 15:48:31 -0500 X-CudaMail-Envelope-Sender: russell@ovn.org From: Russell Bryant To: dev@openvswitch.org X-CudaMail-Whitelist-To: dev@openvswitch.org X-CudaMail-MID: CM-E1-1220081349 X-CudaMail-DTE: 122115 X-CudaMail-Originating-IP: 209.132.183.28 Date: Mon, 21 Dec 2015 15:47:28 -0500 X-ASG-Orig-Subj: [##CM-E1-1220081349##][PATCH 28/55] python: Add basic testing of ovs.json. Message-Id: <1450730875-18083-29-git-send-email-russell@ovn.org> In-Reply-To: <1450730875-18083-1-git-send-email-russell@ovn.org> References: <1450730875-18083-1-git-send-email-russell@ovn.org> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Barracuda-Connect: UNKNOWN[192.168.24.1] X-Barracuda-Start-Time: 1450730913 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://web.cudamail.com:443/cgi-mod/mark.cgi X-ASG-Whitelist: Header =?UTF-8?B?eFwtY3VkYW1haWxcLXdoaXRlbGlzdFwtdG8=?= X-Virus-Scanned: by bsmtpd at cudamail.com X-Barracuda-BRTS-Status: 1 Subject: [ovs-dev] [PATCH 28/55] python: Add basic testing of ovs.json. X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dev-bounces@openvswitch.org Sender: "dev" Add basic testing of the normal code path in the JSON parser. This at least shows how to add more Python unit tests that validate the code works on multiple Python versions. For things that are relatively simple and Python specific, this seems like an easier way to add test cases than autotest. Signed-off-by: Russell Bryant --- python/automake.mk | 3 ++- python/ovs/tests/test_json.py | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 python/ovs/tests/test_json.py diff --git a/python/automake.mk b/python/automake.mk index a86bcaa..15b91a8 100644 --- a/python/automake.mk +++ b/python/automake.mk @@ -35,7 +35,8 @@ ovs_pyfiles = \ python/ovs/version.py \ python/ovs/vlog.py \ python/ovs/tests/__init__.py \ - python/ovs/tests/test_ovs.py + python/ovs/tests/test_ovs.py \ + python/ovs/tests/test_json.py # These python files are used at build time but not runtime, # so they are not installed. diff --git a/python/ovs/tests/test_json.py b/python/ovs/tests/test_json.py new file mode 100644 index 0000000..c2adedb --- /dev/null +++ b/python/ovs/tests/test_json.py @@ -0,0 +1,58 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import unittest + +import ovs.json + + +class TestOVSJson(unittest.TestCase): + _test_dict = { + 1: 1, + 2: { + 'a': ['a', 'b', 'c'], + 'b': True, + 'c': False, + 'd': None, + 'e': 3.14159, + 'f': '1', + 'g': u'u', + 'h': 'abc\\123', + }, + } + + def test_from_string(self): + # Make sure the parser doesn't blow up on a really simple json string + ovs.json.from_string(json.dumps(self._test_dict)) + + def test_to_string(self): + # Make sure the serializer doesn't blow up on a really simple dict + ovs.json.to_string(self._test_dict) + ovs.json.to_string(self._test_dict, pretty=True) + ovs.json.to_string(self._test_dict, sort_keys=False) + + def test_to_and_from_string(self): + # Make sure it can parse what it spits out + s = ovs.json.to_string(self._test_dict) + ovs.json.from_string(s) + + s = ovs.json.to_string(self._test_dict, pretty=True) + ovs.json.from_string(s) + + def test_serialize_python_class(self): + try: + ovs.json.to_string(object()) + except Exception: + pass + else: + raise Exception('Serialization of object should fail')