Show More
@@ -1,433 +1,442 b'' | |||||
1 | """Test suite for our zeromq-based message specification. |
|
1 | """Test suite for our zeromq-based message specification. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (C) 2010 The IPython Development Team |
|
4 | # Copyright (C) 2010 The IPython Development Team | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
7 | # the file COPYING.txt, distributed as part of this software. |
|
7 | # the file COPYING.txt, distributed as part of this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | import re |
|
10 | import re | |
11 | from subprocess import PIPE |
|
11 | from subprocess import PIPE | |
12 | from Queue import Empty |
|
12 | from Queue import Empty | |
13 |
|
13 | |||
14 | import nose.tools as nt |
|
14 | import nose.tools as nt | |
15 |
|
15 | |||
16 | from IPython.kernel import KernelManager |
|
16 | from IPython.kernel import KernelManager | |
17 |
|
17 | |||
18 | from IPython.utils.traitlets import ( |
|
18 | from IPython.utils.traitlets import ( | |
19 | HasTraits, TraitError, Bool, Unicode, Dict, Integer, List, Enum, Any, |
|
19 | HasTraits, TraitError, Bool, Unicode, Dict, Integer, List, Enum, Any, | |
20 | ) |
|
20 | ) | |
21 |
|
21 | |||
22 | from .utils import TIMEOUT, start_global_kernel, flush_channels, execute |
|
22 | from .utils import TIMEOUT, start_global_kernel, flush_channels, execute | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Globals |
|
25 | # Globals | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | KC = None |
|
27 | KC = None | |
28 |
|
28 | |||
29 | def setup(): |
|
29 | def setup(): | |
30 | global KC |
|
30 | global KC | |
31 | KC = start_global_kernel() |
|
31 | KC = start_global_kernel() | |
32 |
|
32 | |||
33 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
34 | # Message Spec References |
|
34 | # Message Spec References | |
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 |
|
36 | |||
37 | class Reference(HasTraits): |
|
37 | class Reference(HasTraits): | |
38 |
|
38 | |||
39 | """ |
|
39 | """ | |
40 | Base class for message spec specification testing. |
|
40 | Base class for message spec specification testing. | |
41 |
|
41 | |||
42 | This class is the core of the message specification test. The |
|
42 | This class is the core of the message specification test. The | |
43 | idea is that child classes implement trait attributes for each |
|
43 | idea is that child classes implement trait attributes for each | |
44 | message keys, so that message keys can be tested against these |
|
44 | message keys, so that message keys can be tested against these | |
45 | traits using :meth:`check` method. |
|
45 | traits using :meth:`check` method. | |
46 |
|
46 | |||
47 | """ |
|
47 | """ | |
48 |
|
48 | |||
49 | def check(self, d): |
|
49 | def check(self, d): | |
50 | """validate a dict against our traits""" |
|
50 | """validate a dict against our traits""" | |
51 | for key in self.trait_names(): |
|
51 | for key in self.trait_names(): | |
52 | nt.assert_in(key, d) |
|
52 | nt.assert_in(key, d) | |
53 | # FIXME: always allow None, probably not a good idea |
|
53 | # FIXME: always allow None, probably not a good idea | |
54 | if d[key] is None: |
|
54 | if d[key] is None: | |
55 | continue |
|
55 | continue | |
56 | try: |
|
56 | try: | |
57 | setattr(self, key, d[key]) |
|
57 | setattr(self, key, d[key]) | |
58 | except TraitError as e: |
|
58 | except TraitError as e: | |
59 | nt.assert_true(False, str(e)) |
|
59 | nt.assert_true(False, str(e)) | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | class RMessage(Reference): |
|
62 | class RMessage(Reference): | |
63 | msg_id = Unicode() |
|
63 | msg_id = Unicode() | |
64 | msg_type = Unicode() |
|
64 | msg_type = Unicode() | |
65 | header = Dict() |
|
65 | header = Dict() | |
66 | parent_header = Dict() |
|
66 | parent_header = Dict() | |
67 | content = Dict() |
|
67 | content = Dict() | |
68 |
|
68 | |||
69 | class RHeader(Reference): |
|
69 | class RHeader(Reference): | |
70 | msg_id = Unicode() |
|
70 | msg_id = Unicode() | |
71 | msg_type = Unicode() |
|
71 | msg_type = Unicode() | |
72 | session = Unicode() |
|
72 | session = Unicode() | |
73 | username = Unicode() |
|
73 | username = Unicode() | |
74 |
|
74 | |||
75 | class RContent(Reference): |
|
75 | class RContent(Reference): | |
76 | status = Enum((u'ok', u'error')) |
|
76 | status = Enum((u'ok', u'error')) | |
77 |
|
77 | |||
78 |
|
78 | |||
79 | class ExecuteReply(Reference): |
|
79 | class ExecuteReply(Reference): | |
80 | execution_count = Integer() |
|
80 | execution_count = Integer() | |
81 | status = Enum((u'ok', u'error')) |
|
81 | status = Enum((u'ok', u'error')) | |
82 |
|
82 | |||
83 | def check(self, d): |
|
83 | def check(self, d): | |
84 | Reference.check(self, d) |
|
84 | Reference.check(self, d) | |
85 | if d['status'] == 'ok': |
|
85 | if d['status'] == 'ok': | |
86 | ExecuteReplyOkay().check(d) |
|
86 | ExecuteReplyOkay().check(d) | |
87 | elif d['status'] == 'error': |
|
87 | elif d['status'] == 'error': | |
88 | ExecuteReplyError().check(d) |
|
88 | ExecuteReplyError().check(d) | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | class ExecuteReplyOkay(Reference): |
|
91 | class ExecuteReplyOkay(Reference): | |
92 | payload = List(Dict) |
|
92 | payload = List(Dict) | |
93 | user_variables = Dict() |
|
93 | user_variables = Dict() | |
94 | user_expressions = Dict() |
|
94 | user_expressions = Dict() | |
95 |
|
95 | |||
96 |
|
96 | |||
97 | class ExecuteReplyError(Reference): |
|
97 | class ExecuteReplyError(Reference): | |
98 | ename = Unicode() |
|
98 | ename = Unicode() | |
99 | evalue = Unicode() |
|
99 | evalue = Unicode() | |
100 | traceback = List(Unicode) |
|
100 | traceback = List(Unicode) | |
101 |
|
101 | |||
102 |
|
102 | |||
103 | class OInfoReply(Reference): |
|
103 | class OInfoReply(Reference): | |
104 | name = Unicode() |
|
104 | name = Unicode() | |
105 | found = Bool() |
|
105 | found = Bool() | |
106 | ismagic = Bool() |
|
106 | ismagic = Bool() | |
107 | isalias = Bool() |
|
107 | isalias = Bool() | |
108 | namespace = Enum((u'builtin', u'magics', u'alias', u'Interactive')) |
|
108 | namespace = Enum((u'builtin', u'magics', u'alias', u'Interactive')) | |
109 | type_name = Unicode() |
|
109 | type_name = Unicode() | |
110 | string_form = Unicode() |
|
110 | string_form = Unicode() | |
111 | base_class = Unicode() |
|
111 | base_class = Unicode() | |
112 | length = Integer() |
|
112 | length = Integer() | |
113 | file = Unicode() |
|
113 | file = Unicode() | |
114 | definition = Unicode() |
|
114 | definition = Unicode() | |
115 | argspec = Dict() |
|
115 | argspec = Dict() | |
116 | init_definition = Unicode() |
|
116 | init_definition = Unicode() | |
117 | docstring = Unicode() |
|
117 | docstring = Unicode() | |
118 | init_docstring = Unicode() |
|
118 | init_docstring = Unicode() | |
119 | class_docstring = Unicode() |
|
119 | class_docstring = Unicode() | |
120 | call_def = Unicode() |
|
120 | call_def = Unicode() | |
121 | call_docstring = Unicode() |
|
121 | call_docstring = Unicode() | |
122 | source = Unicode() |
|
122 | source = Unicode() | |
123 |
|
123 | |||
124 | def check(self, d): |
|
124 | def check(self, d): | |
125 | Reference.check(self, d) |
|
125 | Reference.check(self, d) | |
126 | if d['argspec'] is not None: |
|
126 | if d['argspec'] is not None: | |
127 | ArgSpec().check(d['argspec']) |
|
127 | ArgSpec().check(d['argspec']) | |
128 |
|
128 | |||
129 |
|
129 | |||
130 | class ArgSpec(Reference): |
|
130 | class ArgSpec(Reference): | |
131 | args = List(Unicode) |
|
131 | args = List(Unicode) | |
132 | varargs = Unicode() |
|
132 | varargs = Unicode() | |
133 | varkw = Unicode() |
|
133 | varkw = Unicode() | |
134 | defaults = List() |
|
134 | defaults = List() | |
135 |
|
135 | |||
136 |
|
136 | |||
137 | class Status(Reference): |
|
137 | class Status(Reference): | |
138 | execution_state = Enum((u'busy', u'idle', u'starting')) |
|
138 | execution_state = Enum((u'busy', u'idle', u'starting')) | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | class CompleteReply(Reference): |
|
141 | class CompleteReply(Reference): | |
142 | matches = List(Unicode) |
|
142 | matches = List(Unicode) | |
143 |
|
143 | |||
144 |
|
144 | |||
145 | def Version(num, trait=Integer): |
|
145 | def Version(num, trait=Integer): | |
146 | return List(trait, default_value=[0] * num, minlen=num, maxlen=num) |
|
146 | return List(trait, default_value=[0] * num, minlen=num, maxlen=num) | |
147 |
|
147 | |||
148 |
|
148 | |||
149 | class KernelInfoReply(Reference): |
|
149 | class KernelInfoReply(Reference): | |
150 |
|
150 | |||
151 | protocol_version = Version(2) |
|
151 | protocol_version = Version(2) | |
152 | ipython_version = Version(4, Any) |
|
152 | ipython_version = Version(4, Any) | |
153 | language_version = Version(3) |
|
153 | language_version = Version(3) | |
154 | language = Unicode() |
|
154 | language = Unicode() | |
155 |
|
155 | |||
156 | def _ipython_version_changed(self, name, old, new): |
|
156 | def _ipython_version_changed(self, name, old, new): | |
157 | for v in new: |
|
157 | for v in new: | |
158 | assert isinstance(v, int) or isinstance(v, basestring), \ |
|
158 | assert isinstance(v, int) or isinstance(v, basestring), \ | |
159 | 'expected int or string as version component, got {0!r}'.format(v) |
|
159 | 'expected int or string as version component, got {0!r}'.format(v) | |
160 |
|
160 | |||
161 |
|
161 | |||
162 | # IOPub messages |
|
162 | # IOPub messages | |
163 |
|
163 | |||
164 | class PyIn(Reference): |
|
164 | class PyIn(Reference): | |
165 | code = Unicode() |
|
165 | code = Unicode() | |
166 | execution_count = Integer() |
|
166 | execution_count = Integer() | |
167 |
|
167 | |||
168 |
|
168 | |||
169 | PyErr = ExecuteReplyError |
|
169 | PyErr = ExecuteReplyError | |
170 |
|
170 | |||
171 |
|
171 | |||
172 | class Stream(Reference): |
|
172 | class Stream(Reference): | |
173 | name = Enum((u'stdout', u'stderr')) |
|
173 | name = Enum((u'stdout', u'stderr')) | |
174 | data = Unicode() |
|
174 | data = Unicode() | |
175 |
|
175 | |||
176 |
|
176 | |||
177 | mime_pat = re.compile(r'\w+/\w+') |
|
177 | mime_pat = re.compile(r'\w+/\w+') | |
178 |
|
178 | |||
179 | class DisplayData(Reference): |
|
179 | class DisplayData(Reference): | |
180 | source = Unicode() |
|
180 | source = Unicode() | |
181 | metadata = Dict() |
|
181 | metadata = Dict() | |
182 | data = Dict() |
|
182 | data = Dict() | |
183 | def _data_changed(self, name, old, new): |
|
183 | def _data_changed(self, name, old, new): | |
184 | for k,v in new.iteritems(): |
|
184 | for k,v in new.iteritems(): | |
185 | assert mime_pat.match(k) |
|
185 | assert mime_pat.match(k) | |
186 | nt.assert_is_instance(v, basestring) |
|
186 | nt.assert_is_instance(v, basestring) | |
187 |
|
187 | |||
188 |
|
188 | |||
189 | class PyOut(Reference): |
|
189 | class PyOut(Reference): | |
190 | execution_count = Integer() |
|
190 | execution_count = Integer() | |
191 | data = Dict() |
|
191 | data = Dict() | |
192 | def _data_changed(self, name, old, new): |
|
192 | def _data_changed(self, name, old, new): | |
193 | for k,v in new.iteritems(): |
|
193 | for k,v in new.iteritems(): | |
194 | assert mime_pat.match(k) |
|
194 | assert mime_pat.match(k) | |
195 | nt.assert_is_instance(v, basestring) |
|
195 | nt.assert_is_instance(v, basestring) | |
196 |
|
196 | |||
197 |
|
197 | |||
198 | references = { |
|
198 | references = { | |
199 | 'execute_reply' : ExecuteReply(), |
|
199 | 'execute_reply' : ExecuteReply(), | |
200 | 'object_info_reply' : OInfoReply(), |
|
200 | 'object_info_reply' : OInfoReply(), | |
201 | 'status' : Status(), |
|
201 | 'status' : Status(), | |
202 | 'complete_reply' : CompleteReply(), |
|
202 | 'complete_reply' : CompleteReply(), | |
203 | 'kernel_info_reply': KernelInfoReply(), |
|
203 | 'kernel_info_reply': KernelInfoReply(), | |
204 | 'pyin' : PyIn(), |
|
204 | 'pyin' : PyIn(), | |
205 | 'pyout' : PyOut(), |
|
205 | 'pyout' : PyOut(), | |
206 | 'pyerr' : PyErr(), |
|
206 | 'pyerr' : PyErr(), | |
207 | 'stream' : Stream(), |
|
207 | 'stream' : Stream(), | |
208 | 'display_data' : DisplayData(), |
|
208 | 'display_data' : DisplayData(), | |
209 | } |
|
209 | } | |
210 | """ |
|
210 | """ | |
211 | Specifications of `content` part of the reply messages. |
|
211 | Specifications of `content` part of the reply messages. | |
212 | """ |
|
212 | """ | |
213 |
|
213 | |||
214 |
|
214 | |||
215 | def validate_message(msg, msg_type=None, parent=None): |
|
215 | def validate_message(msg, msg_type=None, parent=None): | |
216 | """validate a message |
|
216 | """validate a message | |
217 |
|
217 | |||
218 | This is a generator, and must be iterated through to actually |
|
218 | This is a generator, and must be iterated through to actually | |
219 | trigger each test. |
|
219 | trigger each test. | |
220 |
|
220 | |||
221 | If msg_type and/or parent are given, the msg_type and/or parent msg_id |
|
221 | If msg_type and/or parent are given, the msg_type and/or parent msg_id | |
222 | are compared with the given values. |
|
222 | are compared with the given values. | |
223 | """ |
|
223 | """ | |
224 | RMessage().check(msg) |
|
224 | RMessage().check(msg) | |
225 | if msg_type: |
|
225 | if msg_type: | |
226 | nt.assert_equal(msg['msg_type'], msg_type) |
|
226 | nt.assert_equal(msg['msg_type'], msg_type) | |
227 | if parent: |
|
227 | if parent: | |
228 | nt.assert_equal(msg['parent_header']['msg_id'], parent) |
|
228 | nt.assert_equal(msg['parent_header']['msg_id'], parent) | |
229 | content = msg['content'] |
|
229 | content = msg['content'] | |
230 | ref = references[msg['msg_type']] |
|
230 | ref = references[msg['msg_type']] | |
231 | ref.check(content) |
|
231 | ref.check(content) | |
232 |
|
232 | |||
233 |
|
233 | |||
234 | #----------------------------------------------------------------------------- |
|
234 | #----------------------------------------------------------------------------- | |
235 | # Tests |
|
235 | # Tests | |
236 | #----------------------------------------------------------------------------- |
|
236 | #----------------------------------------------------------------------------- | |
237 |
|
237 | |||
238 | # Shell channel |
|
238 | # Shell channel | |
239 |
|
239 | |||
240 | def test_execute(): |
|
240 | def test_execute(): | |
241 | flush_channels() |
|
241 | flush_channels() | |
242 |
|
242 | |||
243 | msg_id = KC.execute(code='x=1') |
|
243 | msg_id = KC.execute(code='x=1') | |
244 | reply = KC.get_shell_msg(timeout=TIMEOUT) |
|
244 | reply = KC.get_shell_msg(timeout=TIMEOUT) | |
245 | validate_message(reply, 'execute_reply', msg_id) |
|
245 | validate_message(reply, 'execute_reply', msg_id) | |
246 |
|
246 | |||
247 |
|
247 | |||
248 | def test_execute_silent(): |
|
248 | def test_execute_silent(): | |
249 | flush_channels() |
|
249 | flush_channels() | |
250 | msg_id, reply = execute(code='x=1', silent=True) |
|
250 | msg_id, reply = execute(code='x=1', silent=True) | |
251 |
|
251 | |||
252 | # flush status=idle |
|
252 | # flush status=idle | |
253 | status = KC.iopub_channel.get_msg(timeout=TIMEOUT) |
|
253 | status = KC.iopub_channel.get_msg(timeout=TIMEOUT) | |
254 | validate_message(status, 'status', msg_id) |
|
254 | validate_message(status, 'status', msg_id) | |
255 | nt.assert_equal(status['content']['execution_state'], 'idle') |
|
255 | nt.assert_equal(status['content']['execution_state'], 'idle') | |
256 |
|
256 | |||
257 | nt.assert_raises(Empty, KC.iopub_channel.get_msg, timeout=0.1) |
|
257 | nt.assert_raises(Empty, KC.iopub_channel.get_msg, timeout=0.1) | |
258 | count = reply['execution_count'] |
|
258 | count = reply['execution_count'] | |
259 |
|
259 | |||
260 | msg_id, reply = execute(code='x=2', silent=True) |
|
260 | msg_id, reply = execute(code='x=2', silent=True) | |
261 |
|
261 | |||
262 | # flush status=idle |
|
262 | # flush status=idle | |
263 | status = KC.iopub_channel.get_msg(timeout=TIMEOUT) |
|
263 | status = KC.iopub_channel.get_msg(timeout=TIMEOUT) | |
264 | validate_message(status, 'status', msg_id) |
|
264 | validate_message(status, 'status', msg_id) | |
265 | nt.assert_equal(status['content']['execution_state'], 'idle') |
|
265 | nt.assert_equal(status['content']['execution_state'], 'idle') | |
266 |
|
266 | |||
267 | nt.assert_raises(Empty, KC.iopub_channel.get_msg, timeout=0.1) |
|
267 | nt.assert_raises(Empty, KC.iopub_channel.get_msg, timeout=0.1) | |
268 | count_2 = reply['execution_count'] |
|
268 | count_2 = reply['execution_count'] | |
269 | nt.assert_equal(count_2, count) |
|
269 | nt.assert_equal(count_2, count) | |
270 |
|
270 | |||
271 |
|
271 | |||
272 | def test_execute_error(): |
|
272 | def test_execute_error(): | |
273 | flush_channels() |
|
273 | flush_channels() | |
274 |
|
274 | |||
275 | msg_id, reply = execute(code='1/0') |
|
275 | msg_id, reply = execute(code='1/0') | |
276 | nt.assert_equal(reply['status'], 'error') |
|
276 | nt.assert_equal(reply['status'], 'error') | |
277 | nt.assert_equal(reply['ename'], 'ZeroDivisionError') |
|
277 | nt.assert_equal(reply['ename'], 'ZeroDivisionError') | |
278 |
|
278 | |||
279 | pyerr = KC.iopub_channel.get_msg(timeout=TIMEOUT) |
|
279 | pyerr = KC.iopub_channel.get_msg(timeout=TIMEOUT) | |
280 | validate_message(pyerr, 'pyerr', msg_id) |
|
280 | validate_message(pyerr, 'pyerr', msg_id) | |
281 |
|
281 | |||
282 |
|
282 | |||
283 | def test_execute_inc(): |
|
283 | def test_execute_inc(): | |
284 | """execute request should increment execution_count""" |
|
284 | """execute request should increment execution_count""" | |
285 | flush_channels() |
|
285 | flush_channels() | |
286 |
|
286 | |||
287 | msg_id, reply = execute(code='x=1') |
|
287 | msg_id, reply = execute(code='x=1') | |
288 | count = reply['execution_count'] |
|
288 | count = reply['execution_count'] | |
289 |
|
289 | |||
290 | flush_channels() |
|
290 | flush_channels() | |
291 |
|
291 | |||
292 | msg_id, reply = execute(code='x=2') |
|
292 | msg_id, reply = execute(code='x=2') | |
293 | count_2 = reply['execution_count'] |
|
293 | count_2 = reply['execution_count'] | |
294 | nt.assert_equal(count_2, count+1) |
|
294 | nt.assert_equal(count_2, count+1) | |
295 |
|
295 | |||
296 |
|
296 | |||
297 | def test_user_variables(): |
|
297 | def test_user_variables(): | |
298 | flush_channels() |
|
298 | flush_channels() | |
299 |
|
299 | |||
300 | msg_id, reply = execute(code='x=1', user_variables=['x']) |
|
300 | msg_id, reply = execute(code='x=1', user_variables=['x']) | |
301 | user_variables = reply['user_variables'] |
|
301 | user_variables = reply['user_variables'] | |
302 | nt.assert_equal(user_variables, {u'x': { |
|
302 | nt.assert_equal(user_variables, {u'x': { | |
303 | u'status': u'ok', |
|
303 | u'status': u'ok', | |
304 | u'data': {u'text/plain': u'1'}, |
|
304 | u'data': {u'text/plain': u'1'}, | |
305 | u'metadata': {}, |
|
305 | u'metadata': {}, | |
306 | }}) |
|
306 | }}) | |
307 |
|
307 | |||
308 |
|
308 | |||
309 | def test_user_variables_fail(): |
|
309 | def test_user_variables_fail(): | |
310 | flush_channels() |
|
310 | flush_channels() | |
311 |
|
311 | |||
312 | msg_id, reply = execute(code='x=1', user_variables=['nosuchname']) |
|
312 | msg_id, reply = execute(code='x=1', user_variables=['nosuchname']) | |
313 | user_variables = reply['user_variables'] |
|
313 | user_variables = reply['user_variables'] | |
314 | foo = user_variables['nosuchname'] |
|
314 | foo = user_variables['nosuchname'] | |
315 | nt.assert_equal(foo['status'], 'error') |
|
315 | nt.assert_equal(foo['status'], 'error') | |
316 | nt.assert_equal(foo['ename'], 'KeyError') |
|
316 | nt.assert_equal(foo['ename'], 'KeyError') | |
317 |
|
317 | |||
318 |
|
318 | |||
319 | def test_user_expressions(): |
|
319 | def test_user_expressions(): | |
320 | flush_channels() |
|
320 | flush_channels() | |
321 |
|
321 | |||
322 | msg_id, reply = execute(code='x=1', user_expressions=dict(foo='x+1')) |
|
322 | msg_id, reply = execute(code='x=1', user_expressions=dict(foo='x+1')) | |
323 | user_expressions = reply['user_expressions'] |
|
323 | user_expressions = reply['user_expressions'] | |
324 | nt.assert_equal(user_expressions, {u'foo': { |
|
324 | nt.assert_equal(user_expressions, {u'foo': { | |
325 | u'status': u'ok', |
|
325 | u'status': u'ok', | |
326 | u'data': {u'text/plain': u'2'}, |
|
326 | u'data': {u'text/plain': u'2'}, | |
327 | u'metadata': {}, |
|
327 | u'metadata': {}, | |
328 | }}) |
|
328 | }}) | |
329 |
|
329 | |||
330 |
|
330 | |||
331 | def test_user_expressions_fail(): |
|
331 | def test_user_expressions_fail(): | |
332 | flush_channels() |
|
332 | flush_channels() | |
333 |
|
333 | |||
334 | msg_id, reply = execute(code='x=0', user_expressions=dict(foo='nosuchname')) |
|
334 | msg_id, reply = execute(code='x=0', user_expressions=dict(foo='nosuchname')) | |
335 | user_expressions = reply['user_expressions'] |
|
335 | user_expressions = reply['user_expressions'] | |
336 | foo = user_expressions['foo'] |
|
336 | foo = user_expressions['foo'] | |
337 | nt.assert_equal(foo['status'], 'error') |
|
337 | nt.assert_equal(foo['status'], 'error') | |
338 | nt.assert_equal(foo['ename'], 'NameError') |
|
338 | nt.assert_equal(foo['ename'], 'NameError') | |
339 |
|
339 | |||
340 |
|
340 | |||
341 | def test_oinfo(): |
|
341 | def test_oinfo(): | |
342 | flush_channels() |
|
342 | flush_channels() | |
343 |
|
343 | |||
344 | msg_id = KC.object_info('a') |
|
344 | msg_id = KC.object_info('a') | |
345 | reply = KC.get_shell_msg(timeout=TIMEOUT) |
|
345 | reply = KC.get_shell_msg(timeout=TIMEOUT) | |
346 | validate_message(reply, 'object_info_reply', msg_id) |
|
346 | validate_message(reply, 'object_info_reply', msg_id) | |
347 |
|
347 | |||
348 |
|
348 | |||
349 | def test_oinfo_found(): |
|
349 | def test_oinfo_found(): | |
350 | flush_channels() |
|
350 | flush_channels() | |
351 |
|
351 | |||
352 | msg_id, reply = execute(code='a=5') |
|
352 | msg_id, reply = execute(code='a=5') | |
353 |
|
353 | |||
354 | msg_id = KC.object_info('a') |
|
354 | msg_id = KC.object_info('a') | |
355 | reply = KC.get_shell_msg(timeout=TIMEOUT) |
|
355 | reply = KC.get_shell_msg(timeout=TIMEOUT) | |
356 | validate_message(reply, 'object_info_reply', msg_id) |
|
356 | validate_message(reply, 'object_info_reply', msg_id) | |
357 | content = reply['content'] |
|
357 | content = reply['content'] | |
358 | assert content['found'] |
|
358 | assert content['found'] | |
359 | argspec = content['argspec'] |
|
359 | argspec = content['argspec'] | |
360 | nt.assert_is(argspec, None) |
|
360 | nt.assert_is(argspec, None) | |
361 |
|
361 | |||
362 |
|
362 | |||
363 | def test_oinfo_detail(): |
|
363 | def test_oinfo_detail(): | |
364 | flush_channels() |
|
364 | flush_channels() | |
365 |
|
365 | |||
366 | msg_id, reply = execute(code='ip=get_ipython()') |
|
366 | msg_id, reply = execute(code='ip=get_ipython()') | |
367 |
|
367 | |||
368 | msg_id = KC.object_info('ip.object_inspect', detail_level=2) |
|
368 | msg_id = KC.object_info('ip.object_inspect', detail_level=2) | |
369 | reply = KC.get_shell_msg(timeout=TIMEOUT) |
|
369 | reply = KC.get_shell_msg(timeout=TIMEOUT) | |
370 | validate_message(reply, 'object_info_reply', msg_id) |
|
370 | validate_message(reply, 'object_info_reply', msg_id) | |
371 | content = reply['content'] |
|
371 | content = reply['content'] | |
372 | assert content['found'] |
|
372 | assert content['found'] | |
373 | argspec = content['argspec'] |
|
373 | argspec = content['argspec'] | |
374 | nt.assert_is_instance(argspec, dict, "expected non-empty argspec dict, got %r" % argspec) |
|
374 | nt.assert_is_instance(argspec, dict, "expected non-empty argspec dict, got %r" % argspec) | |
375 | nt.assert_equal(argspec['defaults'], [0]) |
|
375 | nt.assert_equal(argspec['defaults'], [0]) | |
376 |
|
376 | |||
377 |
|
377 | |||
378 | def test_oinfo_not_found(): |
|
378 | def test_oinfo_not_found(): | |
379 | flush_channels() |
|
379 | flush_channels() | |
380 |
|
380 | |||
381 | msg_id = KC.object_info('dne') |
|
381 | msg_id = KC.object_info('dne') | |
382 | reply = KC.get_shell_msg(timeout=TIMEOUT) |
|
382 | reply = KC.get_shell_msg(timeout=TIMEOUT) | |
383 | validate_message(reply, 'object_info_reply', msg_id) |
|
383 | validate_message(reply, 'object_info_reply', msg_id) | |
384 | content = reply['content'] |
|
384 | content = reply['content'] | |
385 | nt.assert_false(content['found']) |
|
385 | nt.assert_false(content['found']) | |
386 |
|
386 | |||
387 |
|
387 | |||
388 | def test_complete(): |
|
388 | def test_complete(): | |
389 | flush_channels() |
|
389 | flush_channels() | |
390 |
|
390 | |||
391 | msg_id, reply = execute(code="alpha = albert = 5") |
|
391 | msg_id, reply = execute(code="alpha = albert = 5") | |
392 |
|
392 | |||
393 | msg_id = KC.complete('al', 'al', 2) |
|
393 | msg_id = KC.complete('al', 'al', 2) | |
394 | reply = KC.get_shell_msg(timeout=TIMEOUT) |
|
394 | reply = KC.get_shell_msg(timeout=TIMEOUT) | |
395 | validate_message(reply, 'complete_reply', msg_id) |
|
395 | validate_message(reply, 'complete_reply', msg_id) | |
396 | matches = reply['content']['matches'] |
|
396 | matches = reply['content']['matches'] | |
397 | for name in ('alpha', 'albert'): |
|
397 | for name in ('alpha', 'albert'): | |
398 | nt.assert_in(name, matches) |
|
398 | nt.assert_in(name, matches) | |
399 |
|
399 | |||
400 |
|
400 | |||
401 | def test_kernel_info_request(): |
|
401 | def test_kernel_info_request(): | |
402 | flush_channels() |
|
402 | flush_channels() | |
403 |
|
403 | |||
404 | msg_id = KC.kernel_info() |
|
404 | msg_id = KC.kernel_info() | |
405 | reply = KC.get_shell_msg(timeout=TIMEOUT) |
|
405 | reply = KC.get_shell_msg(timeout=TIMEOUT) | |
406 | validate_message(reply, 'kernel_info_reply', msg_id) |
|
406 | validate_message(reply, 'kernel_info_reply', msg_id) | |
407 |
|
407 | |||
408 |
|
408 | |||
|
409 | def test_single_payload(): | |||
|
410 | flush_channels() | |||
|
411 | msg_id, reply = execute(code="for i in range(3):\n"+ | |||
|
412 | " x=range?\n") | |||
|
413 | payload = reply['payload'] | |||
|
414 | next_input_pls = [pl for pl in payload if pl["source"] == "set_next_input"] | |||
|
415 | nt.assert_equal(len(next_input_pls), 1) | |||
|
416 | ||||
|
417 | ||||
409 | # IOPub channel |
|
418 | # IOPub channel | |
410 |
|
419 | |||
411 |
|
420 | |||
412 | def test_stream(): |
|
421 | def test_stream(): | |
413 | flush_channels() |
|
422 | flush_channels() | |
414 |
|
423 | |||
415 | msg_id, reply = execute("print('hi')") |
|
424 | msg_id, reply = execute("print('hi')") | |
416 |
|
425 | |||
417 | stdout = KC.iopub_channel.get_msg(timeout=TIMEOUT) |
|
426 | stdout = KC.iopub_channel.get_msg(timeout=TIMEOUT) | |
418 | validate_message(stdout, 'stream', msg_id) |
|
427 | validate_message(stdout, 'stream', msg_id) | |
419 | content = stdout['content'] |
|
428 | content = stdout['content'] | |
420 | nt.assert_equal(content['name'], u'stdout') |
|
429 | nt.assert_equal(content['name'], u'stdout') | |
421 | nt.assert_equal(content['data'], u'hi\n') |
|
430 | nt.assert_equal(content['data'], u'hi\n') | |
422 |
|
431 | |||
423 |
|
432 | |||
424 | def test_display_data(): |
|
433 | def test_display_data(): | |
425 | flush_channels() |
|
434 | flush_channels() | |
426 |
|
435 | |||
427 | msg_id, reply = execute("from IPython.core.display import display; display(1)") |
|
436 | msg_id, reply = execute("from IPython.core.display import display; display(1)") | |
428 |
|
437 | |||
429 | display = KC.iopub_channel.get_msg(timeout=TIMEOUT) |
|
438 | display = KC.iopub_channel.get_msg(timeout=TIMEOUT) | |
430 | validate_message(display, 'display_data', parent=msg_id) |
|
439 | validate_message(display, 'display_data', parent=msg_id) | |
431 | data = display['content']['data'] |
|
440 | data = display['content']['data'] | |
432 | nt.assert_equal(data['text/plain'], u'1') |
|
441 | nt.assert_equal(data['text/plain'], u'1') | |
433 |
|
442 |
General Comments 0
You need to be logged in to leave comments.
Login now