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