##// END OF EJS Templates
remove remaining unused testing.tools.monkeypatch import
Min RK -
Show More
@@ -1,95 +1,94 b''
1 # Copyright (c) IPython Development Team.
1 # Copyright (c) IPython Development Team.
2 # Distributed under the terms of the Modified BSD License.
2 # Distributed under the terms of the Modified BSD License.
3
3
4 import os
4 import os
5 import sys
5 import sys
6 import unittest
6 import unittest
7 import base64
7 import base64
8
8
9 try:
9 try:
10 from unittest.mock import patch
10 from unittest.mock import patch
11 except ImportError:
11 except ImportError:
12 from mock import patch
12 from mock import patch
13
13
14 from IPython.kernel import KernelClient
14 from IPython.kernel import KernelClient
15 from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
15 from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
16 from IPython.utils.tempdir import TemporaryDirectory
16 from IPython.utils.tempdir import TemporaryDirectory
17 from IPython.testing.tools import monkeypatch
18 from IPython.testing.decorators import skip_without
17 from IPython.testing.decorators import skip_without
19 from IPython.utils.ipstruct import Struct
18 from IPython.utils.ipstruct import Struct
20
19
21
20
22 SCRIPT_PATH = os.path.join(
21 SCRIPT_PATH = os.path.join(
23 os.path.abspath(os.path.dirname(__file__)), 'writetofile.py')
22 os.path.abspath(os.path.dirname(__file__)), 'writetofile.py')
24
23
25
24
26 class ZMQTerminalInteractiveShellTestCase(unittest.TestCase):
25 class ZMQTerminalInteractiveShellTestCase(unittest.TestCase):
27
26
28 def setUp(self):
27 def setUp(self):
29 client = KernelClient()
28 client = KernelClient()
30 self.shell = ZMQTerminalInteractiveShell(kernel_client=client)
29 self.shell = ZMQTerminalInteractiveShell(kernel_client=client)
31 self.raw = b'dummy data'
30 self.raw = b'dummy data'
32 self.mime = 'image/png'
31 self.mime = 'image/png'
33 self.data = {self.mime: base64.encodestring(self.raw).decode('ascii')}
32 self.data = {self.mime: base64.encodestring(self.raw).decode('ascii')}
34
33
35 def test_no_call_by_default(self):
34 def test_no_call_by_default(self):
36 def raise_if_called(*args, **kwds):
35 def raise_if_called(*args, **kwds):
37 assert False
36 assert False
38
37
39 shell = self.shell
38 shell = self.shell
40 shell.handle_image_PIL = raise_if_called
39 shell.handle_image_PIL = raise_if_called
41 shell.handle_image_stream = raise_if_called
40 shell.handle_image_stream = raise_if_called
42 shell.handle_image_tempfile = raise_if_called
41 shell.handle_image_tempfile = raise_if_called
43 shell.handle_image_callable = raise_if_called
42 shell.handle_image_callable = raise_if_called
44
43
45 shell.handle_image(None, None) # arguments are dummy
44 shell.handle_image(None, None) # arguments are dummy
46
45
47 @skip_without('PIL')
46 @skip_without('PIL')
48 def test_handle_image_PIL(self):
47 def test_handle_image_PIL(self):
49 import PIL.Image
48 import PIL.Image
50
49
51 open_called_with = []
50 open_called_with = []
52 show_called_with = []
51 show_called_with = []
53
52
54 def fake_open(arg):
53 def fake_open(arg):
55 open_called_with.append(arg)
54 open_called_with.append(arg)
56 return Struct(show=lambda: show_called_with.append(None))
55 return Struct(show=lambda: show_called_with.append(None))
57
56
58 with patch.object(PIL.Image, 'open', fake_open):
57 with patch.object(PIL.Image, 'open', fake_open):
59 self.shell.handle_image_PIL(self.data, self.mime)
58 self.shell.handle_image_PIL(self.data, self.mime)
60
59
61 self.assertEqual(len(open_called_with), 1)
60 self.assertEqual(len(open_called_with), 1)
62 self.assertEqual(len(show_called_with), 1)
61 self.assertEqual(len(show_called_with), 1)
63 self.assertEqual(open_called_with[0].getvalue(), self.raw)
62 self.assertEqual(open_called_with[0].getvalue(), self.raw)
64
63
65 def check_handler_with_file(self, inpath, handler):
64 def check_handler_with_file(self, inpath, handler):
66 shell = self.shell
65 shell = self.shell
67 configname = '{0}_image_handler'.format(handler)
66 configname = '{0}_image_handler'.format(handler)
68 funcname = 'handle_image_{0}'.format(handler)
67 funcname = 'handle_image_{0}'.format(handler)
69
68
70 assert hasattr(shell, configname)
69 assert hasattr(shell, configname)
71 assert hasattr(shell, funcname)
70 assert hasattr(shell, funcname)
72
71
73 with TemporaryDirectory() as tmpdir:
72 with TemporaryDirectory() as tmpdir:
74 outpath = os.path.join(tmpdir, 'data')
73 outpath = os.path.join(tmpdir, 'data')
75 cmd = [sys.executable, SCRIPT_PATH, inpath, outpath]
74 cmd = [sys.executable, SCRIPT_PATH, inpath, outpath]
76 setattr(shell, configname, cmd)
75 setattr(shell, configname, cmd)
77 getattr(shell, funcname)(self.data, self.mime)
76 getattr(shell, funcname)(self.data, self.mime)
78 # cmd is called and file is closed. So it's safe to open now.
77 # cmd is called and file is closed. So it's safe to open now.
79 with open(outpath, 'rb') as file:
78 with open(outpath, 'rb') as file:
80 transferred = file.read()
79 transferred = file.read()
81
80
82 self.assertEqual(transferred, self.raw)
81 self.assertEqual(transferred, self.raw)
83
82
84 def test_handle_image_stream(self):
83 def test_handle_image_stream(self):
85 self.check_handler_with_file('-', 'stream')
84 self.check_handler_with_file('-', 'stream')
86
85
87 def test_handle_image_tempfile(self):
86 def test_handle_image_tempfile(self):
88 self.check_handler_with_file('{file}', 'tempfile')
87 self.check_handler_with_file('{file}', 'tempfile')
89
88
90 def test_handle_image_callable(self):
89 def test_handle_image_callable(self):
91 called_with = []
90 called_with = []
92 self.shell.callable_image_handler = called_with.append
91 self.shell.callable_image_handler = called_with.append
93 self.shell.handle_image_callable(self.data, self.mime)
92 self.shell.handle_image_callable(self.data, self.mime)
94 self.assertEqual(len(called_with), 1)
93 self.assertEqual(len(called_with), 1)
95 assert called_with[0] is self.data
94 assert called_with[0] is self.data
General Comments 0
You need to be logged in to leave comments. Login now