test_validator.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Test nbformat.validator"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import os
  5. from .base import TestsBase
  6. from jsonschema import ValidationError
  7. from nbformat import read
  8. from ..validator import isvalid, validate
  9. class TestValidator(TestsBase):
  10. def test_nb2(self):
  11. """Test that a v2 notebook converted to current passes validation"""
  12. with self.fopen(u'test2.ipynb', u'r') as f:
  13. nb = read(f, as_version=4)
  14. validate(nb)
  15. self.assertEqual(isvalid(nb), True)
  16. def test_nb3(self):
  17. """Test that a v3 notebook passes validation"""
  18. with self.fopen(u'test3.ipynb', u'r') as f:
  19. nb = read(f, as_version=4)
  20. validate(nb)
  21. self.assertEqual(isvalid(nb), True)
  22. def test_nb4(self):
  23. """Test that a v4 notebook passes validation"""
  24. with self.fopen(u'test4.ipynb', u'r') as f:
  25. nb = read(f, as_version=4)
  26. validate(nb)
  27. self.assertEqual(isvalid(nb), True)
  28. def test_nb4_document_info(self):
  29. """Test that a notebook with document_info passes validation"""
  30. with self.fopen(u'test4docinfo.ipynb', u'r') as f:
  31. nb = read(f, as_version=4)
  32. validate(nb)
  33. self.assertEqual(isvalid(nb), True)
  34. def test_nb4custom(self):
  35. """Test that a notebook with a custom JSON mimetype passes validation"""
  36. with self.fopen(u'test4custom.ipynb', u'r') as f:
  37. nb = read(f, as_version=4)
  38. validate(nb)
  39. self.assertEqual(isvalid(nb), True)
  40. def test_nb4jupyter_metadata(self):
  41. """Test that a notebook with a jupyter metadata passes validation"""
  42. with self.fopen(u'test4jupyter_metadata.ipynb', u'r') as f:
  43. nb = read(f, as_version=4)
  44. validate(nb)
  45. self.assertEqual(isvalid(nb), True)
  46. def test_invalid(self):
  47. """Test than an invalid notebook does not pass validation"""
  48. # this notebook has a few different errors:
  49. # - one cell is missing its source
  50. # - invalid cell type
  51. # - invalid output_type
  52. with self.fopen(u'invalid.ipynb', u'r') as f:
  53. nb = read(f, as_version=4)
  54. with self.assertRaises(ValidationError):
  55. validate(nb)
  56. self.assertEqual(isvalid(nb), False)
  57. def test_validate_empty(self):
  58. """Test that an empty dict can be validated without error"""
  59. validate({})
  60. def test_future(self):
  61. """Test than a notebook from the future with extra keys passes validation"""
  62. with self.fopen(u'test4plus.ipynb', u'r') as f:
  63. nb = read(f, as_version=4)
  64. with self.assertRaises(ValidationError):
  65. validate(nb, version=4)
  66. self.assertEqual(isvalid(nb, version=4), False)
  67. self.assertEqual(isvalid(nb), True)
  68. def test_validation_error(self):
  69. with self.fopen(u'invalid.ipynb', u'r') as f:
  70. nb = read(f, as_version=4)
  71. with self.assertRaises(ValidationError) as e:
  72. validate(nb)
  73. s = str(e.exception)
  74. self.assertRegexpMatches(s, "validating.*required.* in markdown_cell")
  75. self.assertRegexpMatches(s, "source.* is a required property")
  76. self.assertRegexpMatches(s, r"On instance\[u?['\"].*cells['\"]\]\[0\]")
  77. self.assertLess(len(s.splitlines()), 10)