##// END OF EJS Templates
Simplify check_handler_with_file
Takafumi Arakaki -
Show More
@@ -1,96 +1,92 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 import os
9 9 import unittest
10 10 import base64
11 11
12 12 from IPython.zmq.kernelmanager import KernelManager
13 13 from IPython.frontend.terminal.console.interactiveshell \
14 14 import ZMQTerminalInteractiveShell
15 15 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
16 16 from IPython.testing.tools import monkeypatch
17 17 from IPython.testing.decorators import skip_without
18 18 from IPython.utils.ipstruct import Struct
19 19 from IPython.utils.process import find_cmd
20 20
21 21
22 22 SCRIPT_PATH = os.path.join(
23 23 os.path.abspath(os.path.dirname(__file__)), 'writetofile.py')
24 24
25 25
26 26 class ZMQTerminalInteractiveShellTestCase(unittest.TestCase):
27 27
28 28 def setUp(self):
29 29 km = KernelManager()
30 30 self.shell = ZMQTerminalInteractiveShell(kernel_manager=km)
31 31 self.raw = b'dummy data'
32 32 self.mime = 'image/png'
33 33 self.data = {self.mime: base64.encodestring(self.raw).decode('ascii')}
34 34
35 35 def test_no_call_by_default(self):
36 36 def raise_if_called(*args, **kwds):
37 37 assert False
38 38
39 39 shell = self.shell
40 40 shell.handle_image_PIL
41 41 shell.handle_image_stream
42 42 shell.handle_image_tempfile
43 43 shell.handle_image_callable
44 44
45 45 shell.handle_image(None, None) # arguments are dummy
46 46
47 47 @skip_without('PIL')
48 48 def test_handle_image_PIL(self):
49 49 import PIL.Image
50 50
51 51 open_called_with = []
52 52 show_called_with = []
53 53
54 54 def fake_open(arg):
55 55 open_called_with.append(arg)
56 56 return Struct(show=lambda: show_called_with.append(None))
57 57
58 58 with monkeypatch(PIL.Image, 'open', fake_open):
59 59 self.shell.handle_image_PIL(self.data, self.mime)
60 60
61 61 assert len(open_called_with) == 1
62 62 assert len(show_called_with) == 1
63 63 assert open_called_with[0].getvalue() == self.raw
64 64
65 @staticmethod
66 def get_handler_command(inpath, outpath):
67 return [find_cmd('python'), SCRIPT_PATH, inpath, outpath]
68
69 65 def check_handler_with_file(self, inpath, handler):
70 66 shell = self.shell
71 67 configname = '{0}_image_handler'.format(handler)
72 68 funcname = 'handle_image_{0}'.format(handler)
73 69
74 70 assert hasattr(shell, configname)
75 71 assert hasattr(shell, funcname)
76 72
77 73 with NamedFileInTemporaryDirectory('data') as file:
78 cmd = self.get_handler_command(inpath, file.name)
74 cmd = [find_cmd('python'), SCRIPT_PATH, inpath, file.name]
79 75 setattr(shell, configname, cmd)
80 76 getattr(shell, funcname)(self.data, self.mime)
81 77 transferred = file.read()
82 78
83 79 assert transferred == self.raw
84 80
85 81 def test_handle_image_stream(self):
86 82 self.check_handler_with_file('-', 'stream')
87 83
88 84 def test_handle_image_tempfile(self):
89 85 self.check_handler_with_file('{file}', 'tempfile')
90 86
91 87 def test_handle_image_callable(self):
92 88 called_with = []
93 89 self.shell.callable_image_handler = called_with.append
94 90 self.shell.handle_image_callable(self.data, self.mime)
95 91 assert len(called_with) == 1
96 92 assert called_with[0] is self.data
General Comments 0
You need to be logged in to leave comments. Login now