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