]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/tests/json_test.py
4ce15f979a00984255b6495fbfa58dc881e9c833
[OpenSRF.git] / src / python / tests / json_test.py
1 import sys, os
2 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
3
4 import osrf.json, osrf.net_obj, unittest
5
6 class TestObject(object):
7     def __init__(self):
8         self.int = 1
9         self.string = "two"
10         self.array = [1,2,3,4]
11         self.dict = {'foo': 'bar', 'key': 'value'}
12         self.true = True
13         self.false = False
14         self.null = None
15
16 class CheckNetworkEncoder(unittest.TestCase):
17     """Tests the NetworkEncoder JSON encoding extension"""
18
19     def setUp(self):
20         osrf.net_obj.register_hint('osrfMessage', ['threadTrace', 'locale', 'type', 'payload'], 'hash')
21         self.testo = TestObject()
22         self.ne = osrf.json.NetworkEncoder()
23
24     def test_connect(self):
25         test_json = self.ne.default(
26             osrf.net_obj.NetworkObject.osrfMessage({
27                     'threadTrace' : 0,
28                     'type' : "CONNECT"
29                 } 
30             )
31         )
32         self.assertEqual(test_json, {'__p': {'threadTrace': 0, 'type': 'CONNECT'}, '__c': 'osrfMessage'})
33
34 class CheckObjectToJSON(unittest.TestCase):
35     """Tests the osrf.json.to_json() method that converts Python objects into JSON"""
36     def setUp(self):
37         self.testo = TestObject()
38
39     def test_int(self):
40         test_json = osrf.json.to_json(self.testo.int)
41         self.assertEqual(test_json, '1')
42
43     def test_string(self):
44         test_json = osrf.json.to_json(self.testo.string)
45         self.assertEqual(test_json, '"two"')
46
47     def test_array(self):
48         test_json = osrf.json.to_json(self.testo.array)
49         self.assertEqual(test_json, '[1, 2, 3, 4]')
50
51     def test_dict(self):
52         test_json = osrf.json.to_json(self.testo.dict)
53         self.assertEqual(test_json, '{"foo": "bar", "key": "value"}')
54
55     def test_true(self):
56         test_json = osrf.json.to_json(self.testo.true)
57         self.assertEqual(test_json, 'true')
58
59     def test_false(self):
60         test_json = osrf.json.to_json(self.testo.false)
61         self.assertEqual(test_json, 'false')
62
63     def test_null(self):
64         test_json = osrf.json.to_json(self.testo.null)
65         self.assertEqual(test_json, 'null')
66
67 class CheckJSONToObject(unittest.TestCase):
68     """Tests that the osrf.json.to_object() method converts JSON into Python objects"""
69
70     def setUp(self):
71         self.testo = TestObject()
72
73     def test_int(self):
74         test_json = osrf.json.to_object('1')
75         self.assertEqual(test_json, self.testo.int)
76
77     def test_string(self):
78         test_json = osrf.json.to_object('"two"')
79         self.assertEqual(test_json, self.testo.string)
80
81     def test_array(self):
82         test_json = osrf.json.to_object('[1, 2, 3, 4]')
83         self.assertEqual(test_json, self.testo.array)
84
85     def test_dict(self):
86         test_json = osrf.json.to_object('{"foo": "bar", "key": "value"}')
87         self.assertEqual(test_json, self.testo.dict)
88
89     def test_true(self):
90         test_json = osrf.json.to_object('true')
91         self.assertEqual(test_json, self.testo.true)
92
93     def test_false(self):
94         test_json = osrf.json.to_object('false')
95         self.assertEqual(test_json, self.testo.false)
96
97     def test_null(self):
98         test_json = osrf.json.to_object('null')
99         self.assertEqual(test_json, self.testo.null)
100
101 if __name__ == '__main__':
102     unittest.main()