test_sessionmanager.py
154 lines
| 6.3 KiB
| text/x-python
|
PythonLexer
Zachary Sailer
|
r13035 | """Tests for the session manager.""" | ||
from unittest import TestCase | ||||
Thomas Kluyver
|
r13097 | from tornado import web | ||
Zachary Sailer
|
r13035 | from ..sessionmanager import SessionManager | ||
Thomas Kluyver
|
r17222 | from IPython.html.services.kernels.kernelmanager import MappingKernelManager | ||
class DummyKernel(object): | ||||
def __init__(self, kernel_name='python'): | ||||
self.kernel_name = kernel_name | ||||
class DummyMKM(MappingKernelManager): | ||||
"""MappingKernelManager interface that doesn't start kernels, for testing""" | ||||
def __init__(self, *args, **kwargs): | ||||
super(DummyMKM, self).__init__(*args, **kwargs) | ||||
self.id_letters = iter(u'ABCDEFGHIJK') | ||||
def _new_id(self): | ||||
return next(self.id_letters) | ||||
def start_kernel(self, kernel_id=None, path=None, kernel_name='python', **kwargs): | ||||
kernel_id = kernel_id or self._new_id() | ||||
self._kernels[kernel_id] = DummyKernel(kernel_name=kernel_name) | ||||
return kernel_id | ||||
def shutdown_kernel(self, kernel_id, now=False): | ||||
del self._kernels[kernel_id] | ||||
Zachary Sailer
|
r13035 | |||
class TestSessionManager(TestCase): | ||||
def test_get_session(self): | ||||
Thomas Kluyver
|
r17222 | sm = SessionManager(kernel_manager=DummyMKM()) | ||
Min RK
|
r18751 | session_id = sm.create_session(path='/path/to/test.ipynb', | ||
Thomas Kluyver
|
r17222 | kernel_name='bar')['id'] | ||
MinRK
|
r13101 | model = sm.get_session(session_id=session_id) | ||
Thomas Kluyver
|
r17222 | expected = {'id':session_id, | ||
Min RK
|
r18751 | 'notebook':{'path': u'/path/to/test.ipynb'}, | ||
Thomas Kluyver
|
r17222 | 'kernel': {'id':u'A', 'name': 'bar'}} | ||
Zachary Sailer
|
r13035 | self.assertEqual(model, expected) | ||
def test_bad_get_session(self): | ||||
# Should raise error if a bad key is passed to the database. | ||||
Thomas Kluyver
|
r17222 | sm = SessionManager(kernel_manager=DummyMKM()) | ||
Min RK
|
r18751 | session_id = sm.create_session(path='/path/to/test.ipynb', | ||
Thomas Kluyver
|
r17222 | kernel_name='foo')['id'] | ||
MinRK
|
r13101 | self.assertRaises(TypeError, sm.get_session, bad_id=session_id) # Bad keyword | ||
Zachary Sailer
|
r13035 | |||
Thomas Kluyver
|
r17782 | def test_get_session_dead_kernel(self): | ||
sm = SessionManager(kernel_manager=DummyMKM()) | ||||
Min RK
|
r18751 | session = sm.create_session(path='/path/to/1/test1.ipynb', kernel_name='python') | ||
Thomas Kluyver
|
r17782 | # kill the kernel | ||
sm.kernel_manager.shutdown_kernel(session['kernel']['id']) | ||||
with self.assertRaises(KeyError): | ||||
sm.get_session(session_id=session['id']) | ||||
# no sessions left | ||||
listed = sm.list_sessions() | ||||
self.assertEqual(listed, []) | ||||
Zachary Sailer
|
r13035 | def test_list_sessions(self): | ||
Thomas Kluyver
|
r17222 | sm = SessionManager(kernel_manager=DummyMKM()) | ||
sessions = [ | ||||
Min RK
|
r18751 | sm.create_session(path='/path/to/1/test1.ipynb', kernel_name='python'), | ||
sm.create_session(path='/path/to/2/test2.ipynb', kernel_name='python'), | ||||
sm.create_session(path='/path/to/3/test3.ipynb', kernel_name='python'), | ||||
Thomas Kluyver
|
r17222 | ] | ||
Zachary Sailer
|
r13035 | sessions = sm.list_sessions() | ||
Min RK
|
r18751 | expected = [ | ||
{ | ||||
'id':sessions[0]['id'], | ||||
'notebook':{'path': u'/path/to/1/test1.ipynb'}, | ||||
'kernel':{'id':u'A', 'name':'python'} | ||||
}, { | ||||
'id':sessions[1]['id'], | ||||
'notebook': {'path': u'/path/to/2/test2.ipynb'}, | ||||
'kernel':{'id':u'B', 'name':'python'} | ||||
}, { | ||||
'id':sessions[2]['id'], | ||||
'notebook':{'path': u'/path/to/3/test3.ipynb'}, | ||||
'kernel':{'id':u'C', 'name':'python'} | ||||
} | ||||
] | ||||
Zachary Sailer
|
r13035 | self.assertEqual(sessions, expected) | ||
Thomas Kluyver
|
r17782 | def test_list_sessions_dead_kernel(self): | ||
sm = SessionManager(kernel_manager=DummyMKM()) | ||||
sessions = [ | ||||
Min RK
|
r18751 | sm.create_session(path='/path/to/1/test1.ipynb', kernel_name='python'), | ||
sm.create_session(path='/path/to/2/test2.ipynb', kernel_name='python'), | ||||
Thomas Kluyver
|
r17782 | ] | ||
# kill one of the kernels | ||||
sm.kernel_manager.shutdown_kernel(sessions[0]['kernel']['id']) | ||||
listed = sm.list_sessions() | ||||
expected = [ | ||||
{ | ||||
'id': sessions[1]['id'], | ||||
'notebook': { | ||||
Min RK
|
r18751 | 'path': u'/path/to/2/test2.ipynb', | ||
Thomas Kluyver
|
r17782 | }, | ||
'kernel': { | ||||
'id': u'B', | ||||
'name':'python', | ||||
} | ||||
} | ||||
] | ||||
self.assertEqual(listed, expected) | ||||
Zachary Sailer
|
r13035 | def test_update_session(self): | ||
Thomas Kluyver
|
r17222 | sm = SessionManager(kernel_manager=DummyMKM()) | ||
Min RK
|
r18751 | session_id = sm.create_session(path='/path/to/test.ipynb', | ||
Thomas Kluyver
|
r17222 | kernel_name='julia')['id'] | ||
Min RK
|
r18751 | sm.update_session(session_id, path='/path/to/new_name.ipynb') | ||
MinRK
|
r13101 | model = sm.get_session(session_id=session_id) | ||
Thomas Kluyver
|
r17222 | expected = {'id':session_id, | ||
Min RK
|
r18751 | 'notebook':{'path': u'/path/to/new_name.ipynb'}, | ||
Thomas Kluyver
|
r17222 | 'kernel':{'id':u'A', 'name':'julia'}} | ||
Zachary Sailer
|
r13035 | self.assertEqual(model, expected) | ||
def test_bad_update_session(self): | ||||
# try to update a session with a bad keyword ~ raise error | ||||
Thomas Kluyver
|
r17222 | sm = SessionManager(kernel_manager=DummyMKM()) | ||
Min RK
|
r18751 | session_id = sm.create_session(path='/path/to/test.ipynb', | ||
Thomas Kluyver
|
r17222 | kernel_name='ir')['id'] | ||
MinRK
|
r13101 | self.assertRaises(TypeError, sm.update_session, session_id=session_id, bad_kw='test.ipynb') # Bad keyword | ||
Zachary Sailer
|
r13035 | |||
def test_delete_session(self): | ||||
Thomas Kluyver
|
r17222 | sm = SessionManager(kernel_manager=DummyMKM()) | ||
sessions = [ | ||||
Min RK
|
r18751 | sm.create_session(path='/path/to/1/test1.ipynb', kernel_name='python'), | ||
sm.create_session(path='/path/to/2/test2.ipynb', kernel_name='python'), | ||||
sm.create_session(path='/path/to/3/test3.ipynb', kernel_name='python'), | ||||
Thomas Kluyver
|
r17222 | ] | ||
sm.delete_session(sessions[1]['id']) | ||||
new_sessions = sm.list_sessions() | ||||
Min RK
|
r18751 | expected = [{ | ||
'id': sessions[0]['id'], | ||||
'notebook': {'path': u'/path/to/1/test1.ipynb'}, | ||||
'kernel': {'id':u'A', 'name':'python'} | ||||
}, { | ||||
'id': sessions[2]['id'], | ||||
'notebook': {'path': u'/path/to/3/test3.ipynb'}, | ||||
'kernel': {'id':u'C', 'name':'python'} | ||||
} | ||||
] | ||||
Thomas Kluyver
|
r17222 | self.assertEqual(new_sessions, expected) | ||
Zachary Sailer
|
r13035 | |||
def test_bad_delete_session(self): | ||||
# try to delete a session that doesn't exist ~ raise error | ||||
Thomas Kluyver
|
r17222 | sm = SessionManager(kernel_manager=DummyMKM()) | ||
Min RK
|
r18751 | sm.create_session(path='/path/to/test.ipynb', kernel_name='python') | ||
MinRK
|
r13101 | self.assertRaises(TypeError, sm.delete_session, bad_kwarg='23424') # Bad keyword | ||
self.assertRaises(web.HTTPError, sm.delete_session, session_id='23424') # nonexistant | ||||
Zachary Sailer
|
r13035 | |||