File: test_clearoutput.py

package info (click to toggle)
nbconvert 7.16.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,044 kB
  • sloc: python: 8,394; makefile: 199; javascript: 2
file content (50 lines) | stat: -rw-r--r-- 1,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Module with tests for the clearoutput preprocessor.
"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from nbconvert.preprocessors.clearoutput import ClearOutputPreprocessor

from .base import PreprocessorTestsBase


class TestClearOutput(PreprocessorTestsBase):
    """Contains test functions for clearoutput.py"""

    def build_notebook(self):
        notebook = super().build_notebook()
        # Add a test field to the first cell
        if "metadata" not in notebook.cells[0]:
            notebook.cells[0].metadata = {}
        notebook.cells[0].metadata["test_field"] = "test_value"
        return notebook

    def build_preprocessor(self):
        """Make an instance of a preprocessor"""
        preprocessor = ClearOutputPreprocessor()
        preprocessor.enabled = True
        return preprocessor

    def test_constructor(self):
        """Can a ClearOutputPreprocessor be constructed?"""
        self.build_preprocessor()

    def test_output(self):
        """Test the output of the ClearOutputPreprocessor"""
        for remove_test_field in [False, True]:
            nb = self.build_notebook()
            res = self.build_resources()
            preprocessor = self.build_preprocessor()
            # Also remove the test field in addition to defaults
            if remove_test_field:
                preprocessor.remove_metadata_fields.add("test_field")
            nb, res = preprocessor(nb, res)
            assert nb.cells[0].outputs == []
            assert nb.cells[0].execution_count is None
            if "metadata" in nb.cells[0]:
                for field in preprocessor.remove_metadata_fields:
                    assert field not in nb.cells[0].metadata
                # Ensure the test field is only removed when added to the traitlet
                assert remove_test_field or "test_field" in nb.cells[0].metadata