##// END OF EJS Templates
begin testing msg spec adapter
MinRK -
Show More
@@ -0,0 +1,189 b''
1 """Tests for adapting IPython msg spec versions"""
2
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
5
6 import copy
7
8 from unittest import TestCase
9 import nose.tools as nt
10
11 from IPython.kernel.adapter import adapt, V4toV5, V5toV4
12 from IPython.kernel.zmq.session import Session
13
14
15 def test_default_version():
16 s = Session()
17 msg = s.msg("msg_type")
18 msg['header'].pop('version')
19 original = copy.deepcopy(msg)
20 adapted = adapt(original)
21 nt.assert_equal(adapted['header'].version, V4toV5.version)
22
23
24 class AdapterTest(TestCase):
25
26 def setUp(self):
27 print("hi")
28 self.session = Session()
29
30 def adapt(self, msg, version=None):
31 original = copy.deepcopy(msg)
32 adapted = adapt(msg, version or self.to_version)
33 return original, adapted
34
35 def check_header(self, msg):
36 pass
37
38
39 class V4toV5TestCase(AdapterTest):
40 from_version = 4
41 to_version = 5
42
43 def msg(self, msg_type, content):
44 """Create a v4 msg (same as v5, minus version header)"""
45 msg = self.session.msg(msg_type, content)
46 msg['header'].pop('version')
47 return msg
48
49 def test_same_version(self):
50 msg = self.msg("execute_result",
51 content={'status' : 'ok'}
52 )
53 original, adapted = self.adapt(msg, self.from_version)
54
55 self.assertEqual(original, adapted)
56
57 def test_no_adapt(self):
58 msg = self.msg("input_reply", {'value' : 'some text'})
59 v4, v5 = self.adapt(msg)
60 self.assertEqual(v5['header']['version'], V4toV5.version)
61 v5['header'].pop('version')
62 self.assertEqual(v4, v5)
63
64 def test_rename_type(self):
65 for v5_type, v4_type in [
66 ('execute_result', 'pyout'),
67 ('execute_input', 'pyin'),
68 ('error', 'pyerr'),
69 ]:
70 msg = self.msg(v4_type, {'key' : 'value'})
71 v4, v5 = self.adapt(msg)
72 self.assertEqual(v5['header']['version'], V4toV5.version)
73 self.assertEqual(v5['header']['msg_type'], v5_type)
74 self.assertEqual(v4['content'], v5['content'])
75
76 def test_execute_request(self):
77 msg = self.msg("execute_request", {
78 'code' : 'a=5',
79 'silent' : False,
80 'user_expressions' : {'a' : 'apple'},
81 'user_variables' : ['b'],
82 })
83 v4, v5 = self.adapt(msg)
84 self.assertEqual(v4['header']['msg_type'], v5['header']['msg_type'])
85 v4c = v4['content']
86 v5c = v5['content']
87 self.assertEqual(v5c['user_expressions'], {'a' : 'apple', 'b': 'b'})
88 nt.assert_not_in('user_variables', v5c)
89 self.assertEqual(v5c['code'], v4c['code'])
90
91 def test_complete_request(self):
92 pass
93
94 def test_complete_reply(self):
95 pass
96
97 def test_object_info_request(self):
98 pass
99
100 def test_object_info_reply(self):
101 pass
102
103 # iopub channel
104
105 def test_display_data(self):
106 pass
107
108 # stdin channel
109
110 def test_input_request(self):
111 msg = self.msg('input_request', {'prompt': "$>"})
112 v4, v5 = self.adapt(msg)
113 self.assertEqual(v5['content']['prompt'], v4['content']['prompt'])
114 self.assertFalse(v5['content']['password'])
115
116
117 class V5toV4TestCase(AdapterTest):
118 from_version = 5
119 to_version = 4
120
121 def msg(self, msg_type, content):
122 return self.session.msg(msg_type, content)
123
124 def test_same_version(self):
125 msg = self.msg("execute_result",
126 content={'status' : 'ok'}
127 )
128 original, adapted = self.adapt(msg, self.from_version)
129
130 self.assertEqual(original, adapted)
131
132 def test_no_adapt(self):
133 msg = self.msg("input_reply", {'value' : 'some text'})
134 v5, v4 = self.adapt(msg)
135 self.assertNotIn('version', v4['header'])
136 v5['header'].pop('version')
137 self.assertEqual(v4, v5)
138
139 def test_rename_type(self):
140 for v5_type, v4_type in [
141 ('execute_result', 'pyout'),
142 ('execute_input', 'pyin'),
143 ('error', 'pyerr'),
144 ]:
145 msg = self.msg(v5_type, {'key' : 'value'})
146 v5, v4 = self.adapt(msg)
147 self.assertEqual(v4['header']['msg_type'], v4_type)
148 nt.assert_not_in('version', v4['header'])
149 self.assertEqual(v4['content'], v5['content'])
150
151 def test_execute_request(self):
152 msg = self.msg("execute_request", {
153 'code' : 'a=5',
154 'silent' : False,
155 'user_expressions' : {'a' : 'apple'},
156 })
157 v5, v4 = self.adapt(msg)
158 self.assertEqual(v4['header']['msg_type'], v5['header']['msg_type'])
159 v4c = v4['content']
160 v5c = v5['content']
161 self.assertEqual(v4c['user_variables'], [])
162 self.assertEqual(v5c['code'], v4c['code'])
163
164 def test_complete_request(self):
165 pass
166
167 def test_complete_reply(self):
168 pass
169
170 def test_inspect_request(self):
171 pass
172
173 def test_inspect_reply(self):
174 pass
175
176 # iopub channel
177
178 def test_display_data(self):
179 pass
180
181 # stdin channel
182
183 def test_input_request(self):
184 msg = self.msg('input_request', {'prompt': "$>", 'password' : True})
185 v5, v4 = self.adapt(msg)
186 self.assertEqual(v5['content']['prompt'], v4['content']['prompt'])
187 self.assertNotIn('password', v4['content'])
188
189
General Comments 0
You need to be logged in to leave comments. Login now