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