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
|
import pytest
from textwrap import dedent
from testbook.testbook import testbook
from testbook.client import TestbookNotebookClient
from testbook.exceptions import TestbookCellTagNotFoundError, TestbookExecuteResultNotFoundError
@pytest.fixture(scope='module')
def notebook():
with testbook('tests/resources/inject.ipynb', execute=True) as tb:
yield tb
@pytest.mark.parametrize("cell_index_args, expected_result", [(2, 2), ('hello', 1)])
def test_cell_index(cell_index_args, expected_result, notebook):
assert notebook._cell_index(cell_index_args) == expected_result
@pytest.mark.parametrize(
"cell_index_args, expected_error",
[([1, 2, 3], TypeError), ('non-existent-tag', TestbookCellTagNotFoundError)],
)
def test_cell_index_raises_error(cell_index_args, expected_error, notebook):
with pytest.raises(expected_error):
notebook._cell_index(cell_index_args)
@pytest.mark.parametrize(
"var_name, expected_result",
[
('sample_dict', {'foo': 'bar'}),
('sample_list', ['foo', 'bar']),
('sample_list + ["hello world"]', ['foo', 'bar', 'hello world']),
('sample_int', 42),
('sample_int * 2', 84),
('sample_str', 'hello world'),
('sample_str + " foo"', 'hello world foo'),
],
)
def test_value(var_name, expected_result, notebook):
assert notebook.value(var_name) == expected_result
@pytest.mark.parametrize("code", [('sample_int *= 2'), ('print(sample_int)'), ('')])
def test_value_raises_error(code, notebook):
with pytest.raises(TestbookExecuteResultNotFoundError):
notebook.value(code)
@pytest.mark.parametrize(
"cell, expected_result",
[
(
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "hello world\n" "foo\n" "bar\n",
}
],
},
"""
hello world
foo
bar
""",
),
({"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": []}, ""),
],
)
def test_output_text(cell, expected_result):
assert TestbookNotebookClient._output_text(cell) == dedent(expected_result).strip()
@pytest.mark.parametrize(
"cell", [{}, {"cell_type": "markdown", "metadata": {}, "source": ["# Hello"]}]
)
def test_output_text_raises_error(cell):
with pytest.raises(ValueError):
assert TestbookNotebookClient._output_text(cell)
def test_cell_execute_result_index(notebook_factory):
nb = notebook_factory()
with testbook(nb, execute="test") as tb:
assert tb.cell_execute_result(1) == [{"text/plain": "22"}]
assert tb.cell_execute_result(2) == [{"text/plain": "text"}]
def test_cell_execute_result_tag(notebook_factory):
nb = notebook_factory()
with testbook(nb, execute="test") as tb:
assert tb.cell_execute_result("test") == [{"text/plain": "22"}]
assert tb.cell_execute_result("dummy-outputs") == [{"text/plain": "text"}]
def test_cell_execute_result_indexerror(notebook_factory):
nb = notebook_factory([])
with testbook(nb) as tb:
with pytest.raises(IndexError):
tb.cell_execute_result(1)
def test_cell_execute_result_tagnotfound(notebook_factory):
nb = notebook_factory([])
with testbook(nb) as tb:
with pytest.raises(TestbookCellTagNotFoundError):
tb.cell_execute_result("test")
|