File: test_tagremove.py

package info (click to toggle)
nbconvert 7.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,056 kB
  • sloc: python: 8,449; makefile: 199; javascript: 2
file content (88 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Module with tests for the TagRemovePreprocessor.
"""

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

from nbformat import v4 as nbformat

from nbconvert.preprocessors.tagremove import TagRemovePreprocessor
from tests.preprocessors.base import PreprocessorTestsBase


class TestTagRemove(PreprocessorTestsBase):
    """Contains test functions for tagremove.py"""

    def build_notebook(self):
        """
        Build a notebook to have metadata tags for cells, output_areas, and
        individual outputs.
        """
        notebook = super().build_notebook()
        # Add a few empty cells
        notebook.cells[0].outputs.extend(
            [
                nbformat.new_output(
                    "display_data", data={"text/plain": "i"}, metadata={"tags": ["hide_one_output"]}
                ),
            ]
        )
        outputs_to_be_removed = [
            nbformat.new_output("display_data", data={"text/plain": "remove_my_output"}),
        ]
        outputs_to_be_kept = [
            nbformat.new_output(
                "stream",
                name="stdout",
                text="remove_my_output",
            ),
        ]
        notebook.cells.extend(
            [
                nbformat.new_code_cell(
                    source="display('remove_my_output')",
                    execution_count=2,
                    outputs=outputs_to_be_removed,
                    metadata={"tags": ["hide_all_outputs"]},
                ),
                nbformat.new_code_cell(
                    source="print('remove this cell')",
                    execution_count=3,
                    outputs=outputs_to_be_kept,
                    metadata={"tags": ["hide_this_cell"]},
                ),
            ]
        )

        return notebook

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

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

    def test_output(self):
        """Test the output of the TagRemovePreprocessor"""
        nb = self.build_notebook()
        res = self.build_resources()
        preprocessor = self.build_preprocessor()
        preprocessor.remove_cell_tags.add("hide_this_cell")
        preprocessor.remove_all_outputs_tags.add("hide_all_outputs")
        preprocessor.remove_single_output_tags.add("hide_one_output")

        nb, res = preprocessor(nb, res)

        # checks that we can remove entire cells
        self.assertEqual(len(nb.cells), 3)

        # checks that we can remove output areas
        self.assertEqual(len(nb.cells[-1].outputs), 0)

        # checks that we can remove individual outputs
        self.assertEqual(len(nb.cells[0].outputs), 8)