Exemple #1
0
def test_run_notebook_node():
    notebook_node = utils.make_notebook_node(["2 + 2"])

    utils.run_notebook_node(notebook_node)

    output = notebook_node.cells[0].outputs[0]
    assert output.data["text/plain"] == "4"
Exemple #2
0
def test_make_notebook_node():
    code_cell_contents = ['print("hello")', "2 + 2"]

    notebook_node = utils.make_notebook_node(code_cell_contents)

    got_cells = set(cell.source for cell in notebook_node.cells)
    assert set(code_cell_contents) == got_cells
Exemple #3
0
def test_get_returned_output_text():
    notebook_node = utils.make_notebook_node(['2 + 2'])
    utils.run_notebook_node(notebook_node)

    output_text = utils.get_output_text(notebook_node.cells[0])

    assert "4" in output_text
Exemple #4
0
def test_get_output_text_handles_no_outputs():
    notebook_node = utils.make_notebook_node(
        ['print("Some numbers:")\nfor num in range(6):\n\tprint(num)'])
    del notebook_node.cells[0]["outputs"]

    output_text = utils.get_output_text(notebook_node.cells[0])

    assert output_text == ""
Exemple #5
0
def test_get_printed_output_text():
    notebook_node = utils.make_notebook_node(
        ['print("Some numbers:")\nfor num in range(6):\n\tprint(num)'])
    utils.run_notebook_node(notebook_node)

    output_text = utils.get_output_text(notebook_node.cells[0])

    assert "4" in output_text
Exemple #6
0
def test_replace_code_input_strings_handles_no_matching_strings():
    cell_contents = ['print("nothing to replace here")']
    notebook_node = utils.make_notebook_node(cell_contents)
    processor = preprocessors.ReplaceCodeInputStringsPreprocessor(
        string_replacements={"thing_to_replace": "replacer"})

    processor.preprocess(notebook_node, {})

    assert notebook_node.cells[0].source == cell_contents[0]
Exemple #7
0
def test_remove_a_tagged_cell():
    cell_contents = ["# TEST_CELL", "print(2 + 2)"]
    notebook_node = utils.make_notebook_node(cell_contents)
    assert len(notebook_node.cells) == 2
    processor = preprocessors.RemoveTaggedCellsPreprocessor(
        tag_string="# TEST_CELL")

    processor.preprocess(notebook_node, {})

    assert len(notebook_node.cells) == 1
    assert notebook_node.cells[0].source == cell_contents[1]
Exemple #8
0
def test_remove_tagged_cells_handles_no_tagged_cells():
    cell_contents = ["1 + 1", "print(2 + 2)"]
    notebook_node = utils.make_notebook_node(cell_contents)
    assert len(notebook_node.cells) == 2
    processor = preprocessors.RemoveTaggedCellsPreprocessor(
        tag_string="# TEST_CELL")

    processor.preprocess(notebook_node, {})

    assert len(notebook_node.cells) == 2
    processed_cell_sources = set(cell.source for cell in notebook_node.cells)
    assert processed_cell_sources == set(cell_contents)
Exemple #9
0
def test_replace_code_input_strings_preprocessor():
    dataset_for_display = "my_display_dataset"
    notebook_node = utils.make_notebook_node(
        ['dataset_id = "{}"'.format(dataset_for_display)])
    assert dataset_for_display in notebook_node.cells[0].source
    dataset_for_execution = "my_randomized_dataset02923535"
    processor = preprocessors.ReplaceCodeInputStringsPreprocessor(
        string_replacements={dataset_for_display: dataset_for_execution})

    processor.preprocess(notebook_node, {})

    assert dataset_for_display not in notebook_node.cells[0].source
    assert dataset_for_execution in notebook_node.cells[0].source
Exemple #10
0
def test_remove_tagged_cells_skips_markdown_cells():
    notebook_node = utils.make_notebook_node(["1 + 1"])
    notebook_node.cells.append(
        nbformat.v4.new_markdown_cell(source="Sometimes we add a # TEST_CELL"))
    original_cell_sources = set(cell.source for cell in notebook_node.cells)
    assert len(notebook_node.cells) == 2
    processor = preprocessors.RemoveTaggedCellsPreprocessor(
        tag_string="# TEST_CELL")

    processor.preprocess(notebook_node, {})

    assert len(notebook_node.cells) == 2
    processed_cell_sources = set(cell.source for cell in notebook_node.cells)
    assert processed_cell_sources == original_cell_sources
Exemple #11
0
def test_remove_multiple_tagged_cells():
    cell_contents = [
        "print(2 + 2)", "# TEST_CELL\nassert 2 + 2 == 4", "my_var = 40 * 2",
        "# TEST_CELL\nassert my_var == 80"
    ]
    notebook_node = utils.make_notebook_node(cell_contents)
    assert len(notebook_node.cells) == 4
    processor = preprocessors.RemoveTaggedCellsPreprocessor(
        tag_string="# TEST_CELL")

    processor.preprocess(notebook_node, {})

    assert len(notebook_node.cells) == 2
    assert notebook_node.cells[0].source == cell_contents[0]
    assert notebook_node.cells[1].source == cell_contents[2]
Exemple #12
0
def test_run_notebook_node_allows_warnings():
    cell_input = ("import warnings\n"
                  "warnings.warn(\"deprecated\", DeprecationWarning)")
    notebook_node = utils.make_notebook_node([cell_input])

    utils.run_notebook_node(notebook_node)
Exemple #13
0
def test_run_notebook_node_can_allow_errors(cell_input):
    notebook_node = utils.make_notebook_node([cell_input])

    utils.run_notebook_node(notebook_node, allow_errors=True)
Exemple #14
0
def test_run_notebook_node_disallows_errors_by_default(cell_input):
    notebook_node = utils.make_notebook_node([cell_input])

    with pytest.raises(CellExecutionError):
        utils.run_notebook_node(notebook_node)