test_nbbase.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # coding: utf-8
  2. """Tests for the Python API for composing notebook elements"""
  3. from nbformat.validator import isvalid, validate, ValidationError
  4. from ..nbbase import (
  5. NotebookNode, nbformat,
  6. new_code_cell, new_markdown_cell, new_notebook,
  7. new_output, new_raw_cell,
  8. )
  9. def test_empty_notebook():
  10. nb = new_notebook()
  11. assert nb.cells == []
  12. assert nb.metadata == NotebookNode()
  13. assert nb.nbformat == nbformat
  14. def test_empty_markdown_cell():
  15. cell = new_markdown_cell()
  16. assert cell.cell_type == 'markdown'
  17. assert cell.source == ''
  18. def test_markdown_cell():
  19. cell = new_markdown_cell(u'* Søme markdown')
  20. assert cell.source == u'* Søme markdown'
  21. def test_empty_raw_cell():
  22. cell = new_raw_cell()
  23. assert cell.cell_type == u'raw'
  24. assert cell.source == ''
  25. def test_raw_cell():
  26. cell = new_raw_cell('hi')
  27. assert cell.source == u'hi'
  28. def test_empty_code_cell():
  29. cell = new_code_cell('hi')
  30. assert cell.cell_type == 'code'
  31. assert cell.source == u'hi'
  32. def test_empty_display_data():
  33. output = new_output('display_data')
  34. assert output.output_type == 'display_data'
  35. def test_empty_stream():
  36. output = new_output('stream')
  37. assert output.output_type == 'stream'
  38. assert output.name == 'stdout'
  39. assert output.text == ''
  40. def test_empty_execute_result():
  41. output = new_output('execute_result', execution_count=1)
  42. assert output.output_type == 'execute_result'
  43. mimebundle = {
  44. 'text/plain': "some text",
  45. "application/json": {
  46. "key": "value"
  47. },
  48. "image/svg+xml": 'ABCDEF',
  49. "application/octet-stream": 'ABC-123',
  50. "application/vnd.foo+bar": "Some other stuff",
  51. }
  52. def test_display_data():
  53. output = new_output('display_data', mimebundle)
  54. for key, expected in mimebundle.items():
  55. assert output.data[key] == expected
  56. def test_execute_result():
  57. output = new_output('execute_result', mimebundle, execution_count=10)
  58. assert output.execution_count == 10
  59. for key, expected in mimebundle.items():
  60. assert output.data[key] == expected
  61. def test_error():
  62. o = new_output(output_type=u'error', ename=u'NameError',
  63. evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
  64. )
  65. assert o.output_type == u'error'
  66. assert o.ename == u'NameError'
  67. assert o.evalue == u'Name not found'
  68. assert o.traceback == [u'frame 0', u'frame 1', u'frame 2']
  69. def test_code_cell_with_outputs():
  70. cell = new_code_cell(execution_count=10, outputs=[
  71. new_output('display_data', mimebundle),
  72. new_output('stream', text='hello'),
  73. new_output('execute_result', mimebundle, execution_count=10),
  74. ])
  75. assert cell.execution_count == 10
  76. assert len(cell.outputs) == 3
  77. er = cell.outputs[-1]
  78. assert er.execution_count == 10
  79. assert er['output_type'] == 'execute_result'
  80. def test_stream():
  81. output = new_output('stream', name='stderr', text='hello there')
  82. assert output.name == 'stderr'
  83. assert output.text == 'hello there'