##// END OF EJS Templates
TST: Add more unit tests for in-process kernel manager.
epatters -
Show More
@@ -1,77 +1,102 b''
1 1 #-------------------------------------------------------------------------------
2 2 # Copyright (C) 2012 The IPython Development Team
3 3 #
4 4 # Distributed under the terms of the BSD License. The full license is in
5 5 # the file COPYING, distributed as part of this software.
6 6 #-------------------------------------------------------------------------------
7 7
8 8 #-----------------------------------------------------------------------------
9 9 # Imports
10 10 #-----------------------------------------------------------------------------
11 11 from __future__ import print_function
12 12
13 13 # Standard library imports
14 14 import unittest
15 15
16 16 # Local imports
17 17 from IPython.inprocess.blockingkernelmanager import \
18 18 BlockingInProcessKernelManager
19 19 from IPython.inprocess.ipkernel import InProcessKernel
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Test case
23 23 #-----------------------------------------------------------------------------
24 24
25 25 class InProcessKernelManagerTestCase(unittest.TestCase):
26 26
27 27 def test_inteface(self):
28 28 """ Does the in-process kernel manager implement the basic KM interface?
29 29 """
30 30 km = BlockingInProcessKernelManager()
31 31 self.assert_(not km.channels_running)
32 32 self.assert_(not km.has_kernel)
33 33
34 34 km.start_channels()
35 35 self.assert_(km.channels_running)
36 36
37 37 km.start_kernel()
38 38 self.assert_(km.has_kernel)
39 39 self.assert_(km.kernel is not None)
40 40
41 41 old_kernel = km.kernel
42 42 km.restart_kernel()
43 43 self.assert_(km.kernel is not None)
44 44 self.assertNotEquals(km.kernel, old_kernel)
45 45
46 46 km.shutdown_kernel()
47 47 self.assert_(not km.has_kernel)
48 48
49 49 self.assertRaises(NotImplementedError, km.interrupt_kernel)
50 50 self.assertRaises(NotImplementedError, km.signal_kernel, 9)
51 51
52 52 km.stop_channels()
53 53 self.assert_(not km.channels_running)
54 54
55 55 def test_execute(self):
56 56 """ Does executing code in an in-process kernel work?
57 57 """
58 58 km = BlockingInProcessKernelManager()
59 59 km.start_kernel()
60 60 km.shell_channel.execute('foo = 1')
61 61 self.assertEquals(km.kernel.shell.user_ns['foo'], 1)
62 62
63 def test_complete(self):
64 """ Does requesting completion from an in-process kernel work?
65 """
66 km = BlockingInProcessKernelManager()
67 km.start_kernel()
68 km.kernel.shell.push({'my_bar': 0, 'my_baz': 1})
69 km.shell_channel.complete('my_ba', 'my_ba', 5)
70 msg = km.shell_channel.get_msg()
71 self.assertEquals(msg['header']['msg_type'], 'complete_reply')
72 self.assertEquals(sorted(msg['content']['matches']),
73 ['my_bar', 'my_baz'])
74
63 75 def test_object_info(self):
64 76 """ Does requesting object information from an in-process kernel work?
65 77 """
66 78 km = BlockingInProcessKernelManager()
67 79 km.start_kernel()
68 80 km.kernel.shell.user_ns['foo'] = 1
69 81 km.shell_channel.object_info('foo')
70 82 msg = km.shell_channel.get_msg()
71 83 self.assertEquals(msg['header']['msg_type'], 'object_info_reply')
72 84 self.assertEquals(msg['content']['name'], 'foo')
73 85 self.assertEquals(msg['content']['type_name'], 'int')
74 86
87 def test_history(self):
88 """ Does requesting history from an in-process kernel work?
89 """
90 km = BlockingInProcessKernelManager()
91 km.start_kernel()
92 km.shell_channel.execute('%who')
93 km.shell_channel.history(hist_access_type='tail', n=1)
94 msg = km.shell_channel.get_msgs()[-1]
95 self.assertEquals(msg['header']['msg_type'], 'history_reply')
96 history = msg['content']['history']
97 self.assertEquals(len(history), 1)
98 self.assertEquals(history[0][2], '%who')
99
75 100
76 101 if __name__ == '__main__':
77 102 unittest.main()
General Comments 0
You need to be logged in to leave comments. Login now