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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
"""
Module with tests for the clearmetadata preprocessor.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from nbconvert.preprocessors.clearmetadata import ClearMetadataPreprocessor
from .base import PreprocessorTestsBase
class TestClearMetadata(PreprocessorTestsBase):
"""Contains test functions for clearmetadata.py"""
def build_notebook(self):
notebook = super().build_notebook()
notebook.metadata = {
"language_info": {"name": "python", "version": "3.6.7"},
"kernelspec": {"language": "python", "name": "python3"},
}
# 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"
notebook.cells[0].metadata["test_nested"] = {"test_keep": "keep", "test_filtered": "filter"}
notebook.cells[0].metadata["executeTime"] = {
"end_time": "09:31:50",
"start_time": "09:31:49",
}
return notebook
def build_preprocessor(self, **kwargs):
"""Make an instance of a preprocessor"""
preprocessor = ClearMetadataPreprocessor(**kwargs)
preprocessor.enabled = True
return preprocessor
def test_constructor(self):
"""Can a ClearMetadataPreprocessor be constructed?"""
self.build_preprocessor()
def test_default_output(self):
"""Test the output of the ClearMetadataPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
preprocessor = self.build_preprocessor()
nb, res = preprocessor(nb, res)
assert not nb.cells[0].metadata
# By default we only preserve the language name
assert nb.metadata == {"language_info": {"name": "python"}}
def test_cell_only(self):
"""Test the output of the ClearMetadataPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
preprocessor = self.build_preprocessor(clear_notebook_metadata=False)
nb, res = preprocessor(nb, res)
assert not nb.cells[0].metadata
assert nb.metadata
def test_notebook_only(self):
"""Test the output of the ClearMetadataPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
preprocessor = self.build_preprocessor(
clear_cell_metadata=False, preserve_nb_metadata_mask=set()
)
nb, res = preprocessor(nb, res)
assert nb.cells[0].metadata
assert not nb.metadata
def test_selective_cell_metadata(self):
"""Test the output of the ClearMetadataPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
preprocessor = self.build_preprocessor(
preserve_cell_metadata_mask=["test_field"], preserve_nb_metadata_mask=set()
)
nb, res = preprocessor(nb, res)
assert nb.cells[0].metadata == {"test_field": "test_value"}
assert not nb.metadata
def test_selective_cell_tuple_metadata(self):
"""Test the output of the ClearMetadataPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
# Ensure that a tuple length 1 works as well as a string key
preprocessor = self.build_preprocessor(
preserve_cell_metadata_mask=[("test_field",)], preserve_nb_metadata_mask=set()
)
nb, res = preprocessor(nb, res)
assert nb.cells[0].metadata == {"test_field": "test_value"}
assert not nb.metadata
def test_nested_cell_metadata(self):
"""Test the output of the ClearMetadataPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
preprocessor = self.build_preprocessor(
preserve_cell_metadata_mask=[("test_nested", "test_keep")],
preserve_nb_metadata_mask=set(),
)
nb, res = preprocessor(nb, res)
assert nb.cells[0].metadata == {"test_nested": {"test_keep": "keep"}}
assert not nb.metadata
def test_nested_cell_tuple_metadata(self):
"""Test the output of the ClearMetadataPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
# Ensure that a tuple length 1 works as well as a string key
preprocessor = self.build_preprocessor(
preserve_cell_metadata_mask=[("test_nested", ("test_keep",))],
preserve_nb_metadata_mask=set(),
)
nb, res = preprocessor(nb, res)
assert nb.cells[0].metadata == {"test_nested": {"test_keep": "keep"}}
assert not nb.metadata
def test_selective_notebook_metadata(self):
"""Test the output of the ClearMetadataPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
preprocessor = self.build_preprocessor(preserve_nb_metadata_mask=["kernelspec"])
nb, res = preprocessor(nb, res)
assert not nb.cells[0].metadata
assert nb.metadata == {"kernelspec": {"language": "python", "name": "python3"}}
|