]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/tests/json_test.py
Add Python unit testing and coverage report to "make check"
[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 from testobj import TestObject
6
7 class CheckObjectToJSON(unittest.TestCase):
8     """Tests the osrf.json.to_json() method that converts Python objects into JSON"""
9     def setUp(self):
10         self.testo = TestObject()
11
12     def test_int(self):
13         test_json = osrf.json.to_json(self.testo.int)
14         self.assertEqual(test_json, '1')
15
16     def test_string(self):
17         test_json = osrf.json.to_json(self.testo.string)
18         self.assertEqual(test_json, '"two"')
19
20     def test_array(self):
21         test_json = osrf.json.to_json(self.testo.array)
22         self.assertEqual(test_json, '[1, 2, 3, 4]')
23
24     def test_dict(self):
25         test_json = osrf.json.to_json(self.testo.dict)
26         self.assertEqual(test_json, '{"foo": "bar", "key": "value"}')
27
28     def test_true(self):
29         test_json = osrf.json.to_json(self.testo.true)
30         self.assertEqual(test_json, 'true')
31
32     def test_false(self):
33         test_json = osrf.json.to_json(self.testo.false)
34         self.assertEqual(test_json, 'false')
35
36     def test_null(self):
37         test_json = osrf.json.to_json(self.testo.null)
38         self.assertEqual(test_json, 'null')
39
40 class CheckJSONToObject(unittest.TestCase):
41     """Tests that the osrf.json.to_object() method converts JSON into Python objects"""
42
43     def setUp(self):
44         self.testo = TestObject()
45
46     def test_int(self):
47         test_json = osrf.json.to_object('1')
48         self.assertEqual(test_json, self.testo.int)
49
50     def test_string(self):
51         test_json = osrf.json.to_object('"two"')
52         self.assertEqual(test_json, self.testo.string)
53
54     def test_array(self):
55         test_json = osrf.json.to_object('[1, 2, 3, 4]')
56         self.assertEqual(test_json, self.testo.array)
57
58     def test_dict(self):
59         test_json = osrf.json.to_object('{"foo": "bar", "key": "value"}')
60         self.assertEqual(test_json, self.testo.dict)
61
62     def test_true(self):
63         test_json = osrf.json.to_object('true')
64         self.assertEqual(test_json, self.testo.true)
65
66     def test_false(self):
67         test_json = osrf.json.to_object('false')
68         self.assertEqual(test_json, self.testo.false)
69
70     def test_null(self):
71         test_json = osrf.json.to_object('null')
72         self.assertEqual(test_json, self.testo.null)
73
74 if __name__ == '__main__':
75     unittest.main()