From: dbs Date: Wed, 10 Nov 2010 03:33:15 +0000 (+0000) Subject: Commit some unit tests for Python osrf.json module X-Git-Tag: osrf_rel_2_0_1~71 X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=c0df40933b8f96bd82483936dba0a0b3152e5c49;p=OpenSRF.git 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. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@2073 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/python/osrf/json.py b/src/python/osrf/json.py index c6d0973..da0b609 100644 --- a/src/python/osrf/json.py +++ b/src/python/osrf/json.py @@ -99,6 +99,11 @@ def to_json_raw(obj): 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 @@ def pprint(json): result += char return result + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/src/python/tests/json_test.py b/src/python/tests/json_test.py new file mode 100644 index 0000000..4ce15f9 --- /dev/null +++ b/src/python/tests/json_test.py @@ -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()