test_json.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from base64 import decodestring
  2. import json
  3. from unittest import TestCase
  4. from ipython_genutils.py3compat import unicode_type
  5. from ..nbjson import reads, writes
  6. from .. import nbjson
  7. from .nbexamples import nb0
  8. from . import formattest
  9. class TestJSON(formattest.NBFormatTest, TestCase):
  10. nb0_ref = None
  11. ext = 'ipynb'
  12. mod = nbjson
  13. def test_roundtrip_nosplit(self):
  14. """Ensure that multiline blobs are still readable"""
  15. # ensures that notebooks written prior to splitlines change
  16. # are still readable.
  17. s = writes(nb0, split_lines=False)
  18. self.assertEqual(nbjson.reads(s),nb0)
  19. def test_roundtrip_split(self):
  20. """Ensure that splitting multiline blocks is safe"""
  21. # This won't differ from test_roundtrip unless the default changes
  22. s = writes(nb0, split_lines=True)
  23. self.assertEqual(nbjson.reads(s),nb0)
  24. def test_splitlines(self):
  25. """Test splitlines in mime-bundles"""
  26. s = writes(nb0, split_lines=True)
  27. raw_nb = json.loads(s)
  28. for i, ref_cell in enumerate(nb0.cells):
  29. if ref_cell.source.strip() == 'Cell with attachments':
  30. attach_ref = ref_cell['attachments']['attachment1']
  31. attach_json = raw_nb['cells'][i]['attachments']['attachment1']
  32. if ref_cell.source.strip() == 'json_outputs()':
  33. output_ref = ref_cell['outputs'][0]['data']
  34. output_json = raw_nb['cells'][i]['outputs'][0]['data']
  35. for key, json_value in attach_json.items():
  36. if key == 'text/plain':
  37. # text should be split
  38. assert json_value == attach_ref['text/plain'].splitlines(True)
  39. else:
  40. # JSON attachments
  41. assert json_value == attach_ref[key]
  42. # check that JSON outputs are left alone:
  43. for key, json_value in output_json.items():
  44. if key == 'text/plain':
  45. # text should be split
  46. assert json_value == output_ref['text/plain'].splitlines(True)
  47. else:
  48. # JSON outputs should be left alone
  49. assert json_value == output_ref[key]
  50. def test_read_png(self):
  51. """PNG output data is b64 unicode"""
  52. s = writes(nb0)
  53. nb1 = nbjson.reads(s)
  54. found_png = False
  55. for cell in nb1.cells:
  56. if not 'outputs' in cell:
  57. continue
  58. for output in cell.outputs:
  59. if not 'data' in output:
  60. continue
  61. if 'image/png' in output.data:
  62. found_png = True
  63. pngdata = output.data['image/png']
  64. self.assertEqual(type(pngdata), unicode_type)
  65. # test that it is valid b64 data
  66. b64bytes = pngdata.encode('ascii')
  67. raw_bytes = decodestring(b64bytes)
  68. assert found_png, "never found png output"
  69. def test_read_jpeg(self):
  70. """JPEG output data is b64 unicode"""
  71. s = writes(nb0)
  72. nb1 = nbjson.reads(s)
  73. found_jpeg = False
  74. for cell in nb1.cells:
  75. if not 'outputs' in cell:
  76. continue
  77. for output in cell.outputs:
  78. if not 'data' in output:
  79. continue
  80. if 'image/jpeg' in output.data:
  81. found_jpeg = True
  82. jpegdata = output.data['image/jpeg']
  83. self.assertEqual(type(jpegdata), unicode_type)
  84. # test that it is valid b64 data
  85. b64bytes = jpegdata.encode('ascii')
  86. raw_bytes = decodestring(b64bytes)
  87. assert found_jpeg, "never found jpeg output"