[Opensrf-commits] r2073 - in trunk/src/python: . osrf tests (dbs)
svn at svn.open-ils.org
svn at svn.open-ils.org
Tue Nov 9 22:33:17 EST 2010
Author: dbs
Date: 2010-11-09 22:33:15 -0500 (Tue, 09 Nov 2010)
New Revision: 2073
Added:
trunk/src/python/tests/
trunk/src/python/tests/json_test.py
Modified:
trunk/src/python/osrf/json.py
Log:
Commit some unit tests for Python osrf.json module
Recommendation: add *_test.py scripts that mirror each
osrf.* module in src/python/tests. Boo-yah.
Modified: trunk/src/python/osrf/json.py
===================================================================
--- trunk/src/python/osrf/json.py 2010-11-09 22:54:39 UTC (rev 2072)
+++ trunk/src/python/osrf/json.py 2010-11-10 03:33:15 UTC (rev 2073)
@@ -99,6 +99,11 @@
def __tabs(depth):
'''
Returns a string of spaces-not-tabs for the desired indentation level
+
+ >>> print '"' + __tabs(0) + '"'
+ ""
+ >>> print '"' + __tabs(4) + '"'
+ " "
'''
space = ' ' * depth
return space
@@ -189,3 +194,7 @@
result += char
return result
+
+if __name__ == "__main__":
+ import doctest
+ doctest.testmod()
Added: trunk/src/python/tests/json_test.py
===================================================================
--- trunk/src/python/tests/json_test.py (rev 0)
+++ trunk/src/python/tests/json_test.py 2010-11-10 03:33:15 UTC (rev 2073)
@@ -0,0 +1,102 @@
+import sys, os
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
+
+import osrf.json, osrf.net_obj, unittest
+
+class TestObject(object):
+ def __init__(self):
+ self.int = 1
+ self.string = "two"
+ self.array = [1,2,3,4]
+ self.dict = {'foo': 'bar', 'key': 'value'}
+ self.true = True
+ self.false = False
+ self.null = None
+
+class CheckNetworkEncoder(unittest.TestCase):
+ """Tests the NetworkEncoder JSON encoding extension"""
+
+ def setUp(self):
+ osrf.net_obj.register_hint('osrfMessage', ['threadTrace', 'locale', 'type', 'payload'], 'hash')
+ self.testo = TestObject()
+ self.ne = osrf.json.NetworkEncoder()
+
+ def test_connect(self):
+ test_json = self.ne.default(
+ osrf.net_obj.NetworkObject.osrfMessage({
+ 'threadTrace' : 0,
+ 'type' : "CONNECT"
+ }
+ )
+ )
+ self.assertEqual(test_json, {'__p': {'threadTrace': 0, 'type': 'CONNECT'}, '__c': 'osrfMessage'})
+
+class CheckObjectToJSON(unittest.TestCase):
+ """Tests the osrf.json.to_json() method that converts Python objects into JSON"""
+ def setUp(self):
+ self.testo = TestObject()
+
+ def test_int(self):
+ test_json = osrf.json.to_json(self.testo.int)
+ self.assertEqual(test_json, '1')
+
+ def test_string(self):
+ test_json = osrf.json.to_json(self.testo.string)
+ self.assertEqual(test_json, '"two"')
+
+ def test_array(self):
+ test_json = osrf.json.to_json(self.testo.array)
+ self.assertEqual(test_json, '[1, 2, 3, 4]')
+
+ def test_dict(self):
+ test_json = osrf.json.to_json(self.testo.dict)
+ self.assertEqual(test_json, '{"foo": "bar", "key": "value"}')
+
+ def test_true(self):
+ test_json = osrf.json.to_json(self.testo.true)
+ self.assertEqual(test_json, 'true')
+
+ def test_false(self):
+ test_json = osrf.json.to_json(self.testo.false)
+ self.assertEqual(test_json, 'false')
+
+ def test_null(self):
+ test_json = osrf.json.to_json(self.testo.null)
+ self.assertEqual(test_json, 'null')
+
+class CheckJSONToObject(unittest.TestCase):
+ """Tests that the osrf.json.to_object() method converts JSON into Python objects"""
+
+ def setUp(self):
+ self.testo = TestObject()
+
+ def test_int(self):
+ test_json = osrf.json.to_object('1')
+ self.assertEqual(test_json, self.testo.int)
+
+ def test_string(self):
+ test_json = osrf.json.to_object('"two"')
+ self.assertEqual(test_json, self.testo.string)
+
+ def test_array(self):
+ test_json = osrf.json.to_object('[1, 2, 3, 4]')
+ self.assertEqual(test_json, self.testo.array)
+
+ def test_dict(self):
+ test_json = osrf.json.to_object('{"foo": "bar", "key": "value"}')
+ self.assertEqual(test_json, self.testo.dict)
+
+ def test_true(self):
+ test_json = osrf.json.to_object('true')
+ self.assertEqual(test_json, self.testo.true)
+
+ def test_false(self):
+ test_json = osrf.json.to_object('false')
+ self.assertEqual(test_json, self.testo.false)
+
+ def test_null(self):
+ test_json = osrf.json.to_object('null')
+ self.assertEqual(test_json, self.testo.null)
+
+if __name__ == '__main__':
+ unittest.main()
More information about the opensrf-commits
mailing list