##// END OF EJS Templates
Merge pull request #8297 from minrk/rm-parallel...
Merge pull request #8297 from minrk/rm-parallel remove ipython_parallel

File last commit:

r21149:4a4a94e1
r21226:879a45e2 merge
Show More
test_image_handler.py
94 lines | 3.1 KiB | text/x-python | PythonLexer
Min RK
remove testing.tools.monkeypatch...
r21116 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Takafumi Arakaki
Add tests for image handlers
r8250
import os
Takafumi Arakaki
Use sys.executable instead of find_cmd('python')
r8269 import sys
Takafumi Arakaki
Add tests for image handlers
r8250 import unittest
import base64
Min RK
remove testing.tools.monkeypatch...
r21116 try:
from unittest.mock import patch
except ImportError:
from mock import patch
MinRK
zmq frontend needs a Client, Manager is optional
r10292 from IPython.kernel import KernelClient
Fernando Perez
Fix all remaining imports that used `IPython.frontend`.
r11024 from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
Takafumi Arakaki
Open file after it is written
r8270 from IPython.utils.tempdir import TemporaryDirectory
Takafumi Arakaki
Skip test for PIL handler when it is not available
r8251 from IPython.testing.decorators import skip_without
Takafumi Arakaki
Add tests for image handlers
r8250 from IPython.utils.ipstruct import Struct
SCRIPT_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'writetofile.py')
class ZMQTerminalInteractiveShellTestCase(unittest.TestCase):
def setUp(self):
MinRK
zmq frontend needs a Client, Manager is optional
r10292 client = KernelClient()
self.shell = ZMQTerminalInteractiveShell(kernel_client=client)
Takafumi Arakaki
Add tests for image handlers
r8250 self.raw = b'dummy data'
self.mime = 'image/png'
Takafumi Arakaki
Encode to byte before base64.decodestring
r8252 self.data = {self.mime: base64.encodestring(self.raw).decode('ascii')}
Takafumi Arakaki
Add tests for image handlers
r8250
def test_no_call_by_default(self):
def raise_if_called(*args, **kwds):
assert False
shell = self.shell
Takafumi Arakaki
Fix test_no_call_by_default...
r8267 shell.handle_image_PIL = raise_if_called
shell.handle_image_stream = raise_if_called
shell.handle_image_tempfile = raise_if_called
shell.handle_image_callable = raise_if_called
Takafumi Arakaki
Add tests for image handlers
r8250
shell.handle_image(None, None) # arguments are dummy
Takafumi Arakaki
Skip test for PIL handler when it is not available
r8251 @skip_without('PIL')
Takafumi Arakaki
Add tests for image handlers
r8250 def test_handle_image_PIL(self):
import PIL.Image
open_called_with = []
show_called_with = []
def fake_open(arg):
open_called_with.append(arg)
return Struct(show=lambda: show_called_with.append(None))
Min RK
remove testing.tools.monkeypatch...
r21116 with patch.object(PIL.Image, 'open', fake_open):
Takafumi Arakaki
Add tests for image handlers
r8250 self.shell.handle_image_PIL(self.data, self.mime)
Takafumi Arakaki
Use assertEqual when possible
r8268 self.assertEqual(len(open_called_with), 1)
self.assertEqual(len(show_called_with), 1)
self.assertEqual(open_called_with[0].getvalue(), self.raw)
Takafumi Arakaki
Add tests for image handlers
r8250
def check_handler_with_file(self, inpath, handler):
shell = self.shell
configname = '{0}_image_handler'.format(handler)
funcname = 'handle_image_{0}'.format(handler)
assert hasattr(shell, configname)
assert hasattr(shell, funcname)
Takafumi Arakaki
Open file after it is written
r8270 with TemporaryDirectory() as tmpdir:
outpath = os.path.join(tmpdir, 'data')
cmd = [sys.executable, SCRIPT_PATH, inpath, outpath]
Takafumi Arakaki
Add tests for image handlers
r8250 setattr(shell, configname, cmd)
getattr(shell, funcname)(self.data, self.mime)
Takafumi Arakaki
Open file after it is written
r8270 # cmd is called and file is closed. So it's safe to open now.
with open(outpath, 'rb') as file:
transferred = file.read()
Takafumi Arakaki
Add tests for image handlers
r8250
Takafumi Arakaki
Use assertEqual when possible
r8268 self.assertEqual(transferred, self.raw)
Takafumi Arakaki
Add tests for image handlers
r8250
def test_handle_image_stream(self):
self.check_handler_with_file('-', 'stream')
def test_handle_image_tempfile(self):
self.check_handler_with_file('{file}', 'tempfile')
def test_handle_image_callable(self):
called_with = []
self.shell.callable_image_handler = called_with.append
self.shell.handle_image_callable(self.data, self.mime)
Takafumi Arakaki
Use assertEqual when possible
r8268 self.assertEqual(len(called_with), 1)
Takafumi Arakaki
Add tests for image handlers
r8250 assert called_with[0] is self.data