##// END OF EJS Templates
make InteractiveShell.banner a property
make InteractiveShell.banner a property

File last commit:

r16591:6a91fb65
r16595:1787af65
Show More
test_message_spec.py
400 lines | 10.3 KiB | text/x-python | PythonLexer
/ IPython / kernel / tests / test_message_spec.py
MinRK
kernel_info versions are strings
r16566 """Test suite for our zeromq-based message specification."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Fernando Perez
Rework messaging to better conform to our spec....
r2926
MinRK
begin testing message spec
r6554 import re
MinRK
kernel_info versions are strings
r16566 from distutils.version import LooseVersion as V
MinRK
begin testing message spec
r6554 from subprocess import PIPE
Thomas Kluyver
Update imports for Python 3...
r13354 try:
from queue import Empty # Py 3
except ImportError:
from Queue import Empty # Py 2
Fernando Perez
Rework messaging to better conform to our spec....
r2926
import nose.tools as nt
MinRK
remove redundant BlockingKernelClient import from test_message_spec
r10385 from IPython.kernel import KernelManager
Fernando Perez
Rework messaging to better conform to our spec....
r2926
MinRK
begin testing message spec
r6554 from IPython.utils.traitlets import (
Takafumi Arakaki
Rewrite VersionReply spec using List and Unicode
r8865 HasTraits, TraitError, Bool, Unicode, Dict, Integer, List, Enum, Any,
MinRK
begin testing message spec
r6554 )
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 from IPython.utils.py3compat import string_types, iteritems
MinRK
begin testing message spec
r6554
MinRK
move kernel-test utilities to kernel.tests.utils...
r12414 from .utils import TIMEOUT, start_global_kernel, flush_channels, execute
MinRK
begin testing message spec
r6554 #-----------------------------------------------------------------------------
MinRK
move kernel-test utilities to kernel.tests.utils...
r12414 # Globals
MinRK
begin testing message spec
r6554 #-----------------------------------------------------------------------------
MinRK
move kernel-test utilities to kernel.tests.utils...
r12414 KC = None
MinRK
apply the same long-timeout logic to kernel tests...
r11802
Fernando Perez
Rework messaging to better conform to our spec....
r2926 def setup():
MinRK
move kernel-test utilities to kernel.tests.utils...
r12414 global KC
KC = start_global_kernel()
MinRK
begin testing message spec
r6554
#-----------------------------------------------------------------------------
MinRK
move kernel-test utilities to kernel.tests.utils...
r12414 # Message Spec References
MinRK
begin testing message spec
r6554 #-----------------------------------------------------------------------------
class Reference(HasTraits):
Takafumi Arakaki
Document how test_message_spec works
r8829
"""
Base class for message spec specification testing.
This class is the core of the message specification test. The
idea is that child classes implement trait attributes for each
message keys, so that message keys can be tested against these
traits using :meth:`check` method.
"""
MinRK
begin testing message spec
r6554 def check(self, d):
"""validate a dict against our traits"""
for key in self.trait_names():
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_in(key, d)
MinRK
begin testing message spec
r6554 # FIXME: always allow None, probably not a good idea
if d[key] is None:
continue
try:
setattr(self, key, d[key])
except TraitError as e:
MinRK
kernel_info versions are strings
r16566 assert False, str(e)
MinRK
begin testing message spec
r6554
MinRK
kernel_info versions are strings
r16566 class Version(Unicode):
def validate(self, obj, value):
min_version = self.default_value
if V(value) < V(min_version):
raise TraitError("bad version: %s < %s" % (value, min_version))
MinRK
begin testing message spec
r6554
class RMessage(Reference):
msg_id = Unicode()
msg_type = Unicode()
header = Dict()
parent_header = Dict()
content = Dict()
MinRK
kernel_info versions are strings
r16566
def check(self, d):
super(RMessage, self).check(d)
RHeader().check(self.header)
MinRK
remove user_variables...
r16570 if self.parent_header:
RHeader().check(self.parent_header)
MinRK
begin testing message spec
r6554
class RHeader(Reference):
msg_id = Unicode()
msg_type = Unicode()
session = Unicode()
username = Unicode()
MinRK
kernel_info versions are strings
r16566 version = Version('5.0')
MinRK
begin testing message spec
r6554
MinRK
update completion_ and objection_info_request...
r16580 mime_pat = re.compile(r'\w+/\w+')
class MimeBundle(Reference):
metadata = Dict()
data = Dict()
def _data_changed(self, name, old, new):
for k,v in iteritems(new):
assert mime_pat.match(k)
nt.assert_is_instance(v, string_types)
# shell replies
MinRK
begin testing message spec
r6554
class ExecuteReply(Reference):
execution_count = Integer()
status = Enum((u'ok', u'error'))
def check(self, d):
Thomas Kluyver
Remove uses of @parametric decorator
r12374 Reference.check(self, d)
MinRK
begin testing message spec
r6554 if d['status'] == 'ok':
Thomas Kluyver
Remove uses of @parametric decorator
r12374 ExecuteReplyOkay().check(d)
MinRK
begin testing message spec
r6554 elif d['status'] == 'error':
Thomas Kluyver
Remove uses of @parametric decorator
r12374 ExecuteReplyError().check(d)
MinRK
begin testing message spec
r6554
Fernando Perez
Rework messaging to better conform to our spec....
r2926
MinRK
begin testing message spec
r6554 class ExecuteReplyOkay(Reference):
payload = List(Dict)
user_expressions = Dict()
Fernando Perez
Rework messaging to better conform to our spec....
r2926
MinRK
begin testing message spec
r6554
class ExecuteReplyError(Reference):
ename = Unicode()
evalue = Unicode()
traceback = List(Unicode)
MinRK
s/object_info_request/inspect_request
r16587 class InspectReply(MimeBundle):
MinRK
begin testing message spec
r6554 found = Bool()
class ArgSpec(Reference):
args = List(Unicode)
varargs = Unicode()
varkw = Unicode()
MinRK
use parametric tests in message_spec...
r6558 defaults = List()
MinRK
begin testing message spec
r6554
class Status(Reference):
MinRK
add missing "starting" status to test_message_spec
r10341 execution_state = Enum((u'busy', u'idle', u'starting'))
MinRK
begin testing message spec
r6554
class CompleteReply(Reference):
matches = List(Unicode)
MinRK
complete_reply has cursor_start and cursor_end, not matched_text
r16588 cursor_start = Integer()
cursor_end = Integer()
status = Unicode()
MinRK
begin testing message spec
r6554
Takafumi Arakaki
Rename version_rep/req to kernel_info_rep/req
r8879 class KernelInfoReply(Reference):
MinRK
kernel_info versions are strings
r16566 protocol_version = Version('5.0')
MinRK
add implementation and implementation_version to kernel_info_reply
r16584 implementation = Unicode('ipython')
implementation_version = Version('2.1')
MinRK
kernel_info versions are strings
r16566 language_version = Version('2.7')
MinRK
add implementation and implementation_version to kernel_info_reply
r16584 language = Unicode('python')
banner = Unicode()
Takafumi Arakaki
Rewrite VersionReply spec using List and Unicode
r8865
Takafumi Arakaki
Add a test for version_request in test_message_spec.py
r8831
MinRK
begin testing message spec
r6554 # IOPub messages
MinRK
pyin -> execute_input
r16567 class ExecuteInput(Reference):
MinRK
begin testing message spec
r6554 code = Unicode()
MinRK
include execution_count in pyin check
r6559 execution_count = Integer()
MinRK
begin testing message spec
r6554
MinRK
pyerr -> error
r16569 Error = ExecuteReplyError
MinRK
begin testing message spec
r6554
class Stream(Reference):
name = Enum((u'stdout', u'stderr'))
data = Unicode()
MinRK
update completion_ and objection_info_request...
r16580 class DisplayData(MimeBundle):
MinRK
remove `source` key from display_data
r16585 pass
MinRK
begin testing message spec
r6554
MinRK
update completion_ and objection_info_request...
r16580 class ExecuteResult(MimeBundle):
MinRK
handle pyout messages in test_message_spec...
r6664 execution_count = Integer()
MinRK
begin testing message spec
r6554 references = {
'execute_reply' : ExecuteReply(),
MinRK
s/object_info_request/inspect_request
r16587 'inspect_reply' : InspectReply(),
MinRK
begin testing message spec
r6554 'status' : Status(),
'complete_reply' : CompleteReply(),
Takafumi Arakaki
Rename version_rep/req to kernel_info_rep/req
r8879 'kernel_info_reply': KernelInfoReply(),
MinRK
pyin -> execute_input
r16567 'execute_input' : ExecuteInput(),
MinRK
pyout -> execute_result...
r16568 'execute_result' : ExecuteResult(),
MinRK
pyerr -> error
r16569 'error' : Error(),
MinRK
begin testing message spec
r6554 'stream' : Stream(),
'display_data' : DisplayData(),
MinRK
kernel_info versions are strings
r16566 'header' : RHeader(),
MinRK
begin testing message spec
r6554 }
Takafumi Arakaki
Document how test_message_spec works
r8829 """
Specifications of `content` part of the reply messages.
"""
MinRK
begin testing message spec
r6554
def validate_message(msg, msg_type=None, parent=None):
MinRK
evaluate a few dangling validate_message generators...
r6573 """validate a message
This is a generator, and must be iterated through to actually
trigger each test.
If msg_type and/or parent are given, the msg_type and/or parent msg_id
are compared with the given values.
"""
MinRK
begin testing message spec
r6554 RMessage().check(msg)
if msg_type:
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(msg['msg_type'], msg_type)
MinRK
begin testing message spec
r6554 if parent:
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(msg['parent_header']['msg_id'], parent)
MinRK
begin testing message spec
r6554 content = msg['content']
ref = references[msg['msg_type']]
Thomas Kluyver
Remove uses of @parametric decorator
r12374 ref.check(content)
MinRK
begin testing message spec
r6554
#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
# Shell channel
Fernando Perez
Rework messaging to better conform to our spec....
r2926
def test_execute():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
expose shell channel methods at the client level
r10294 msg_id = KC.execute(code='x=1')
MinRK
apply the same long-timeout logic to kernel tests...
r11802 reply = KC.get_shell_msg(timeout=TIMEOUT)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 validate_message(reply, 'execute_reply', msg_id)
MinRK
begin testing message spec
r6554
def test_execute_silent():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
begin testing message spec
r6554 msg_id, reply = execute(code='x=1', silent=True)
# flush status=idle
MinRK
apply the same long-timeout logic to kernel tests...
r11802 status = KC.iopub_channel.get_msg(timeout=TIMEOUT)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 validate_message(status, 'status', msg_id)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(status['content']['execution_state'], 'idle')
MinRK
begin testing message spec
r6554
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_raises(Empty, KC.iopub_channel.get_msg, timeout=0.1)
MinRK
begin testing message spec
r6554 count = reply['execution_count']
msg_id, reply = execute(code='x=2', silent=True)
# flush status=idle
MinRK
apply the same long-timeout logic to kernel tests...
r11802 status = KC.iopub_channel.get_msg(timeout=TIMEOUT)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 validate_message(status, 'status', msg_id)
nt.assert_equal(status['content']['execution_state'], 'idle')
MinRK
begin testing message spec
r6554
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_raises(Empty, KC.iopub_channel.get_msg, timeout=0.1)
MinRK
begin testing message spec
r6554 count_2 = reply['execution_count']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(count_2, count)
MinRK
begin testing message spec
r6554
def test_execute_error():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
begin testing message spec
r6554
msg_id, reply = execute(code='1/0')
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(reply['status'], 'error')
nt.assert_equal(reply['ename'], 'ZeroDivisionError')
MinRK
begin testing message spec
r6554
MinRK
pyerr -> error
r16569 error = KC.iopub_channel.get_msg(timeout=TIMEOUT)
validate_message(error, 'error', msg_id)
MinRK
begin testing message spec
r6554
def test_execute_inc():
"""execute request should increment execution_count"""
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
begin testing message spec
r6554 msg_id, reply = execute(code='x=1')
count = reply['execution_count']
flush_channels()
Fernando Perez
Rework messaging to better conform to our spec....
r2926
MinRK
begin testing message spec
r6554 msg_id, reply = execute(code='x=2')
count_2 = reply['execution_count']
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(count_2, count+1)
MinRK
begin testing message spec
r6554
def test_user_expressions():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
begin testing message spec
r6554 msg_id, reply = execute(code='x=1', user_expressions=dict(foo='x+1'))
user_expressions = reply['user_expressions']
MinRK
test user_variables/expressions in message spec
r10637 nt.assert_equal(user_expressions, {u'foo': {
u'status': u'ok',
u'data': {u'text/plain': u'2'},
u'metadata': {},
}})
def test_user_expressions_fail():
flush_channels()
msg_id, reply = execute(code='x=0', user_expressions=dict(foo='nosuchname'))
user_expressions = reply['user_expressions']
foo = user_expressions['foo']
nt.assert_equal(foo['status'], 'error')
nt.assert_equal(foo['ename'], 'NameError')
MinRK
begin testing message spec
r6554
def test_oinfo():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
s/object_info_request/inspect_request
r16587 msg_id = KC.inspect('a')
MinRK
apply the same long-timeout logic to kernel tests...
r11802 reply = KC.get_shell_msg(timeout=TIMEOUT)
MinRK
s/object_info_request/inspect_request
r16587 validate_message(reply, 'inspect_reply', msg_id)
MinRK
begin testing message spec
r6554
def test_oinfo_found():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
begin testing message spec
r6554
msg_id, reply = execute(code='a=5')
MinRK
s/object_info_request/inspect_request
r16587 msg_id = KC.inspect('a')
MinRK
apply the same long-timeout logic to kernel tests...
r11802 reply = KC.get_shell_msg(timeout=TIMEOUT)
MinRK
s/object_info_request/inspect_request
r16587 validate_message(reply, 'inspect_reply', msg_id)
MinRK
begin testing message spec
r6554 content = reply['content']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 assert content['found']
MinRK
update completion_ and objection_info_request...
r16580 text = content['data']['text/plain']
nt.assert_in('Type:', text)
nt.assert_in('Docstring:', text)
MinRK
begin testing message spec
r6554
MinRK
a couple more oinfo tests
r6557 def test_oinfo_detail():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
a couple more oinfo tests
r6557
msg_id, reply = execute(code='ip=get_ipython()')
MinRK
use parametric tests in message_spec...
r6558
MinRK
s/object_info_request/inspect_request
r16587 msg_id = KC.inspect('ip.object_inspect', cursor_pos=10, detail_level=1)
MinRK
apply the same long-timeout logic to kernel tests...
r11802 reply = KC.get_shell_msg(timeout=TIMEOUT)
MinRK
s/object_info_request/inspect_request
r16587 validate_message(reply, 'inspect_reply', msg_id)
MinRK
a couple more oinfo tests
r6557 content = reply['content']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 assert content['found']
MinRK
update completion_ and objection_info_request...
r16580 text = content['data']['text/plain']
nt.assert_in('Definition:', text)
nt.assert_in('Source:', text)
MinRK
a couple more oinfo tests
r6557
def test_oinfo_not_found():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
a couple more oinfo tests
r6557
MinRK
s/object_info_request/inspect_request
r16587 msg_id = KC.inspect('dne')
MinRK
apply the same long-timeout logic to kernel tests...
r11802 reply = KC.get_shell_msg(timeout=TIMEOUT)
MinRK
s/object_info_request/inspect_request
r16587 validate_message(reply, 'inspect_reply', msg_id)
MinRK
a couple more oinfo tests
r6557 content = reply['content']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_false(content['found'])
MinRK
a couple more oinfo tests
r6557
MinRK
begin testing message spec
r6554 def test_complete():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
begin testing message spec
r6554 msg_id, reply = execute(code="alpha = albert = 5")
MinRK
update completion_ and objection_info_request...
r16580 msg_id = KC.complete('al', 2)
MinRK
apply the same long-timeout logic to kernel tests...
r11802 reply = KC.get_shell_msg(timeout=TIMEOUT)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 validate_message(reply, 'complete_reply', msg_id)
MinRK
begin testing message spec
r6554 matches = reply['content']['matches']
for name in ('alpha', 'albert'):
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_in(name, matches)
MinRK
begin testing message spec
r6554
Takafumi Arakaki
Rename version_rep/req to kernel_info_rep/req
r8879 def test_kernel_info_request():
Takafumi Arakaki
Add a test for version_request in test_message_spec.py
r8831 flush_channels()
MinRK
expose shell channel methods at the client level
r10294 msg_id = KC.kernel_info()
MinRK
apply the same long-timeout logic to kernel tests...
r11802 reply = KC.get_shell_msg(timeout=TIMEOUT)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 validate_message(reply, 'kernel_info_reply', msg_id)
Takafumi Arakaki
Add a test for version_request in test_message_spec.py
r8831
Pablo de Oliveira
Add regression test for #1349
r12953 def test_single_payload():
flush_channels()
msg_id, reply = execute(code="for i in range(3):\n"+
" x=range?\n")
payload = reply['payload']
next_input_pls = [pl for pl in payload if pl["source"] == "set_next_input"]
nt.assert_equal(len(next_input_pls), 1)
MinRK
mark IOPub channel tests
r6561 # IOPub channel
MinRK
begin testing message spec
r6554 def test_stream():
MinRK
use parametric tests in message_spec...
r6558 flush_channels()
MinRK
begin testing message spec
r6554 msg_id, reply = execute("print('hi')")
MinRK
apply the same long-timeout logic to kernel tests...
r11802 stdout = KC.iopub_channel.get_msg(timeout=TIMEOUT)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 validate_message(stdout, 'stream', msg_id)
MinRK
begin testing message spec
r6554 content = stdout['content']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(content['data'], u'hi\n')
MinRK
begin testing message spec
r6554
MinRK
use parametric tests in message_spec...
r6558 def test_display_data():
flush_channels()
MinRK
begin testing message spec
r6554 msg_id, reply = execute("from IPython.core.display import display; display(1)")
MinRK
apply the same long-timeout logic to kernel tests...
r11802 display = KC.iopub_channel.get_msg(timeout=TIMEOUT)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 validate_message(display, 'display_data', parent=msg_id)
MinRK
begin testing message spec
r6554 data = display['content']['data']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(data['text/plain'], u'1')
MinRK
begin testing message spec
r6554