test_json.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import copy
  2. import json
  3. from base64 import decodestring
  4. from unittest import TestCase
  5. from ipython_genutils.py3compat import unicode_type
  6. from ..nbjson import reads, writes
  7. from ..nbbase import from_dict
  8. from .. import nbjson
  9. from .nbexamples import nb0
  10. from . import formattest
  11. from .nbexamples import nb0
  12. class TestJSON(formattest.NBFormatTest, TestCase):
  13. nb0_ref = None
  14. ext = 'ipynb'
  15. mod = nbjson
  16. def test_roundtrip_nosplit(self):
  17. """Ensure that multiline blobs are still readable"""
  18. # ensures that notebooks written prior to splitlines change
  19. # are still readable.
  20. s = writes(nb0, split_lines=False)
  21. self.assertEqual(nbjson.reads(s),nb0)
  22. def test_roundtrip_split(self):
  23. """Ensure that splitting multiline blocks is safe"""
  24. # This won't differ from test_roundtrip unless the default changes
  25. s = writes(nb0, split_lines=True)
  26. self.assertEqual(nbjson.reads(s),nb0)
  27. def test_strip_transient(self):
  28. """transient values aren't written to files"""
  29. nb = copy.deepcopy(nb0)
  30. nb.orig_nbformat = 2
  31. nb.orig_nbformat_minor = 3
  32. nb.worksheets[0].cells[0].metadata.trusted = False
  33. nbs = nbjson.writes(nb)
  34. nb2 = from_dict(json.loads(nbs))
  35. self.assertNotIn('orig_nbformat', nb2)
  36. self.assertNotIn('orig_nbformat_minor', nb2)
  37. for cell in nb2.worksheets[0].cells:
  38. self.assertNotIn('trusted', cell.metadata)
  39. def test_to_json(self):
  40. """to_notebook_json doesn't strip transient"""
  41. nb = copy.deepcopy(nb0)
  42. nb.orig_nbformat = 2
  43. nb.orig_nbformat_minor = 3
  44. nb.worksheets[0].cells[0].metadata.trusted = False
  45. nbs = json.dumps(nb)
  46. nb2 = nbjson.to_notebook(json.loads(nbs))
  47. nb2 = from_dict(json.loads(nbs))
  48. self.assertIn('orig_nbformat', nb2)
  49. self.assertIn('orig_nbformat_minor', nb2)
  50. cell = nb2.worksheets[0].cells[0]
  51. self.assertIn('trusted', cell.metadata)
  52. def test_read_png(self):
  53. """PNG output data is b64 unicode"""
  54. s = writes(nb0)
  55. nb1 = nbjson.reads(s)
  56. found_png = False
  57. for cell in nb1.worksheets[0].cells:
  58. if not 'outputs' in cell:
  59. continue
  60. for output in cell.outputs:
  61. if 'png' in output:
  62. found_png = True
  63. pngdata = output['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.worksheets[0].cells:
  75. if not 'outputs' in cell:
  76. continue
  77. for output in cell.outputs:
  78. if 'jpeg' in output:
  79. found_jpeg = True
  80. jpegdata = output['jpeg']
  81. self.assertEqual(type(jpegdata), unicode_type)
  82. # test that it is valid b64 data
  83. b64bytes = jpegdata.encode('ascii')
  84. raw_bytes = decodestring(b64bytes)
  85. assert found_jpeg, "never found jpeg output"