test_validate.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """Tests for nbformat validation"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import io
  5. import os
  6. import pytest
  7. from nbformat.validator import validate, ValidationError
  8. from ..nbjson import reads
  9. from ..nbbase import (
  10. nbformat,
  11. new_code_cell, new_markdown_cell, new_notebook,
  12. new_output, new_raw_cell,
  13. )
  14. def validate4(obj, ref=None):
  15. return validate(obj, ref, version=nbformat)
  16. def test_valid_code_cell():
  17. cell = new_code_cell()
  18. validate4(cell, 'code_cell')
  19. def test_invalid_code_cell():
  20. cell = new_code_cell()
  21. cell['source'] = 5
  22. with pytest.raises(ValidationError):
  23. validate4(cell, 'code_cell')
  24. cell = new_code_cell()
  25. del cell['metadata']
  26. with pytest.raises(ValidationError):
  27. validate4(cell, 'code_cell')
  28. cell = new_code_cell()
  29. del cell['source']
  30. with pytest.raises(ValidationError):
  31. validate4(cell, 'code_cell')
  32. cell = new_code_cell()
  33. del cell['cell_type']
  34. with pytest.raises(ValidationError):
  35. validate4(cell, 'code_cell')
  36. def test_invalid_markdown_cell():
  37. cell = new_markdown_cell()
  38. cell['source'] = 5
  39. with pytest.raises(ValidationError):
  40. validate4(cell, 'markdown_cell')
  41. cell = new_markdown_cell()
  42. del cell['metadata']
  43. with pytest.raises(ValidationError):
  44. validate4(cell, 'markdown_cell')
  45. cell = new_markdown_cell()
  46. del cell['source']
  47. with pytest.raises(ValidationError):
  48. validate4(cell, 'markdown_cell')
  49. cell = new_markdown_cell()
  50. del cell['cell_type']
  51. with pytest.raises(ValidationError):
  52. validate4(cell, 'markdown_cell')
  53. def test_invalid_raw_cell():
  54. cell = new_raw_cell()
  55. cell['source'] = 5
  56. with pytest.raises(ValidationError):
  57. validate4(cell, 'raw_cell')
  58. cell = new_raw_cell()
  59. del cell['metadata']
  60. with pytest.raises(ValidationError):
  61. validate4(cell, 'raw_cell')
  62. cell = new_raw_cell()
  63. del cell['source']
  64. with pytest.raises(ValidationError):
  65. validate4(cell, 'raw_cell')
  66. cell = new_raw_cell()
  67. del cell['cell_type']
  68. with pytest.raises(ValidationError):
  69. validate4(cell, 'raw_cell')
  70. def test_sample_notebook():
  71. here = os.path.dirname(__file__)
  72. with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "test4.ipynb"), encoding='utf-8') as f:
  73. nb = reads(f.read())
  74. validate4(nb)