test_api.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Test the APIs at the top-level of nbformat"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import json
  5. import os
  6. from .base import TestsBase
  7. from ipython_genutils.tempdir import TemporaryDirectory
  8. from ..reader import get_version
  9. from nbformat import read, current_nbformat, writes, write
  10. class TestAPI(TestsBase):
  11. def test_read(self):
  12. """Can older notebooks be opened and automatically converted to the current
  13. nbformat?"""
  14. # Open a version 2 notebook.
  15. with self.fopen(u'test2.ipynb', 'r') as f:
  16. nb = read(f, as_version=current_nbformat)
  17. # Check that the notebook was upgraded to the latest version automatically.
  18. (major, minor) = get_version(nb)
  19. self.assertEqual(major, current_nbformat)
  20. def test_write_downgrade_2(self):
  21. """dowgrade a v3 notebook to v2"""
  22. # Open a version 3 notebook.
  23. with self.fopen(u'test3.ipynb', 'r') as f:
  24. nb = read(f, as_version=3)
  25. jsons = writes(nb, version=2)
  26. nb2 = json.loads(jsons)
  27. (major, minor) = get_version(nb2)
  28. self.assertEqual(major, 2)
  29. def test_read_write_path(self):
  30. """read() and write() take filesystem paths"""
  31. path = os.path.join(self._get_files_path(), u'test4.ipynb')
  32. nb = read(path, as_version=4)
  33. with TemporaryDirectory() as td:
  34. dest = os.path.join(td, 'echidna.ipynb')
  35. write(nb, dest)
  36. assert os.path.isfile(dest)