Fix unit tests for Python in a twistd instance
[OpenSRF.git] / src / python / tests / net_obj_test.py
1 """
2 Unit tests for the osrf.net_obj 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 CheckNetworkEncoder(unittest.TestCase):
11     """Tests the NetworkEncoder JSON encoding extension"""
12
13     def setUp(self):
14         osrf.net_obj.register_hint('osrfMessage', ['threadTrace', 'locale', 'type', 'payload'], 'hash')
15         self.testo = TestObject()
16         self.ne = osrf.json.NetworkEncoder()
17
18     def test_connect(self):
19         test_json = self.ne.default(
20             osrf.net_obj.NetworkObject.osrfMessage({
21                     'threadTrace' : 0,
22                     'type' : "CONNECT"
23                 } 
24             )
25         )
26         self.assertEqual(test_json, {'__p': 
27             {'threadTrace': 0, 'type': 'CONNECT'},
28             '__c': 'osrfMessage'}
29         )
30
31     def test_connect_array(self):
32         test_json = self.ne.default(
33             osrf.net_obj.NetworkObject.osrfMessage({
34                     'threadTrace' : 0,
35                     'type' : "CONNECT",
36                     'protocol' : "array"
37                 } 
38             )
39         )
40         self.assertEqual(test_json, {'__p':
41             {'threadTrace': 0, 'protocol': 'array', 'type': 'CONNECT'},
42             '__c': 'osrfMessage'}
43         )
44
45     def test_connect_to_xml(self):
46         test_json = self.ne.default(
47             osrf.net_obj.NetworkObject.osrfMessage({
48                     'threadTrace' : 0,
49                     'type' : "CONNECT"
50                 } 
51             )
52         )
53         self.assertEqual(
54             osrf.net_obj.to_xml(test_json), 
55             "<object><element key='__p'><object><element key='threadTrace'>"
56             "<number>0</number></element><element key='type'>"
57             "<string>CONNECT</string></element></object></element>"
58             "<element key='__c'><string>osrfMessage</string></element></object>"
59         )
60
61
62 if __name__ == '__main__':
63     unittest.main()
64