[ovs-dev,28/55] python: Add basic testing of ovs.json.
diff mbox

Message ID 1450730875-18083-29-git-send-email-russell@ovn.org
State Deferred
Headers show

Commit Message

Russell Bryant Dec. 21, 2015, 8:47 p.m. UTC
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 <russell@ovn.org>
---
 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

Patch
diff mbox

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')