test_image_handler.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) IPython Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. import os
  4. import sys
  5. import unittest
  6. import base64
  7. try:
  8. from unittest.mock import patch
  9. except ImportError:
  10. from mock import patch
  11. from jupyter_console.ptshell import ZMQTerminalInteractiveShell
  12. from ipython_genutils.tempdir import TemporaryDirectory
  13. from ipython_genutils.testing.decorators import skip_without
  14. from ipython_genutils.ipstruct import Struct
  15. SCRIPT_PATH = os.path.join(
  16. os.path.abspath(os.path.dirname(__file__)), 'writetofile.py')
  17. class NonCommunicatingShell(ZMQTerminalInteractiveShell):
  18. """A testing shell class that doesn't attempt to communicate with the kernel"""
  19. def init_kernel_info(self):
  20. pass
  21. class ZMQTerminalInteractiveShellTestCase(unittest.TestCase):
  22. def setUp(self):
  23. self.shell = NonCommunicatingShell()
  24. self.raw = b'dummy data'
  25. self.mime = 'image/png'
  26. self.data = {self.mime: base64.encodestring(self.raw).decode('ascii')}
  27. def test_call_pil_by_default(self):
  28. pil_called_with = []
  29. def pil_called(data, mime):
  30. pil_called_with.append(data)
  31. def raise_if_called(*args, **kwds):
  32. assert False
  33. shell = self.shell
  34. shell.handle_image_PIL = pil_called
  35. shell.handle_image_stream = raise_if_called
  36. shell.handle_image_tempfile = raise_if_called
  37. shell.handle_image_callable = raise_if_called
  38. shell.handle_image(None, None) # arguments are dummy
  39. assert len(pil_called_with) == 1
  40. @skip_without('PIL')
  41. def test_handle_image_PIL(self):
  42. from PIL import Image, ImageShow
  43. open_called_with = []
  44. show_called_with = []
  45. def fake_open(arg):
  46. open_called_with.append(arg)
  47. def fake_show(img):
  48. show_called_with.append(img)
  49. with patch.object(Image, 'open', fake_open), \
  50. patch.object(ImageShow, 'show', fake_show):
  51. self.shell.handle_image_PIL(self.data, self.mime)
  52. self.assertEqual(len(open_called_with), 1)
  53. self.assertEqual(len(show_called_with), 1)
  54. self.assertEqual(open_called_with[0].getvalue(), self.raw)
  55. def check_handler_with_file(self, inpath, handler):
  56. shell = self.shell
  57. configname = '{0}_image_handler'.format(handler)
  58. funcname = 'handle_image_{0}'.format(handler)
  59. assert hasattr(shell, configname)
  60. assert hasattr(shell, funcname)
  61. with TemporaryDirectory() as tmpdir:
  62. outpath = os.path.join(tmpdir, 'data')
  63. cmd = [sys.executable, SCRIPT_PATH, inpath, outpath]
  64. setattr(shell, configname, cmd)
  65. getattr(shell, funcname)(self.data, self.mime)
  66. # cmd is called and file is closed. So it's safe to open now.
  67. with open(outpath, 'rb') as file:
  68. transferred = file.read()
  69. self.assertEqual(transferred, self.raw)
  70. def test_handle_image_stream(self):
  71. self.check_handler_with_file('-', 'stream')
  72. def test_handle_image_tempfile(self):
  73. self.check_handler_with_file('{file}', 'tempfile')
  74. def test_handle_image_callable(self):
  75. called_with = []
  76. self.shell.callable_image_handler = called_with.append
  77. self.shell.handle_image_callable(self.data, self.mime)
  78. self.assertEqual(len(called_with), 1)
  79. assert called_with[0] is self.data