base.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """utility functions for preprocessor tests"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from nbformat import v4 as nbformat
  5. from ...tests.base import TestsBase
  6. from ...exporters.exporter import ResourcesDict
  7. class PreprocessorTestsBase(TestsBase):
  8. """Contains test functions preprocessor tests"""
  9. def build_notebook(self, with_json_outputs=False):
  10. """Build a notebook in memory for use with preprocessor tests"""
  11. outputs = [
  12. nbformat.new_output("stream", name="stdout", text="a"),
  13. nbformat.new_output("display_data", data={'text/plain': 'b'}),
  14. nbformat.new_output("stream", name="stdout", text="c"),
  15. nbformat.new_output("stream", name="stdout", text="d"),
  16. nbformat.new_output("stream", name="stderr", text="e"),
  17. nbformat.new_output("stream", name="stderr", text="f"),
  18. nbformat.new_output("display_data", data={'image/png': 'Zw=='}), # g
  19. nbformat.new_output("display_data", data={'application/pdf': 'aA=='}), # h
  20. ]
  21. if with_json_outputs:
  22. outputs.extend([
  23. nbformat.new_output(
  24. "display_data", data={'application/json': [1, 2, 3]}
  25. ), # j
  26. nbformat.new_output(
  27. "display_data", data={'application/json': {'a': 1, 'c': {'b': 2}}}
  28. ), # k
  29. nbformat.new_output(
  30. "display_data", data={'application/json': 'abc'}
  31. ), # l
  32. nbformat.new_output(
  33. "display_data", data={'application/json': 15.03}
  34. ), # m
  35. ])
  36. cells=[nbformat.new_code_cell(source="$ e $", execution_count=1, outputs=outputs),
  37. nbformat.new_markdown_cell(source="$ e $")]
  38. return nbformat.new_notebook(cells=cells)
  39. def build_resources(self):
  40. """Build an empty resources dictionary."""
  41. res = ResourcesDict()
  42. res['metadata'] = ResourcesDict()
  43. return res