test_convert.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """Tests for nbformat.convert"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from .base import TestsBase
  5. from ..converter import convert
  6. from ..reader import read, get_version
  7. from ..validator import isvalid, validate
  8. from .. import current_nbformat
  9. class TestConvert(TestsBase):
  10. def test_downgrade_3_2(self):
  11. """Do notebook downgrades work?"""
  12. # Open a version 3 notebook and attempt to downgrade it to version 2.
  13. with self.fopen(u'test3.ipynb', u'r') as f:
  14. nb = read(f)
  15. nb = convert(nb, 2)
  16. # Check if downgrade was successful.
  17. (major, minor) = get_version(nb)
  18. self.assertEqual(major, 2)
  19. def test_upgrade_2_3(self):
  20. """Do notebook upgrades work?"""
  21. # Open a version 2 notebook and attempt to upgrade it to version 3.
  22. with self.fopen(u'test2.ipynb', u'r') as f:
  23. nb = read(f)
  24. nb = convert(nb, 3)
  25. # Check if upgrade was successful.
  26. (major, minor) = get_version(nb)
  27. self.assertEqual(major, 3)
  28. def test_upgrade_downgrade_4_3_4(self):
  29. """Test that a v4 notebook downgraded to v3 and then upgraded to v4
  30. passes validation tests"""
  31. with self.fopen(u'test4.ipynb', u'r') as f:
  32. nb = read(f)
  33. validate(nb)
  34. nb = convert(nb, 3)
  35. validate(nb)
  36. nb = convert(nb, 4)
  37. self.assertEqual(isvalid(nb), True)
  38. def test_open_current(self):
  39. """Can an old notebook be opened and converted to the current version
  40. while remembering the original version of the notebook?"""
  41. # Open a version 2 notebook and attempt to upgrade it to the current version
  42. # while remembering it's version information.
  43. with self.fopen(u'test2.ipynb', u'r') as f:
  44. nb = read(f)
  45. (original_major, original_minor) = get_version(nb)
  46. nb = convert(nb, current_nbformat)
  47. # Check if upgrade was successful.
  48. (major, minor) = get_version(nb)
  49. self.assertEqual(major, current_nbformat)
  50. # Check if the original major revision was remembered.
  51. self.assertEqual(original_major, 2)