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