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
|
import pytest
from testbook.testbook import testbook
@pytest.fixture(scope='module')
def notebook():
with testbook('tests/resources/inject.ipynb', execute=True) as tb:
yield tb
def inject_helper(*args, **kwargs):
pass
@pytest.mark.parametrize(
"args, kwargs",
[
(None, None),
([1, 2], None),
((1, 2), None),
((True, False), None),
(['a', 'b'], None),
([1.1, float('nan'), float('inf'), float('-inf')], None),
([{'key1': 'value1'}, {'key2': 'value2'}], None),
((1, 2, False), {'key2': 'value2'}),
((None, None, False), {'key2': 'value2'}),
],
)
def test_inject(args, kwargs, notebook):
assert notebook.inject(inject_helper, args=args, kwargs=kwargs, pop=True)
@pytest.mark.parametrize(
"code_block, expected_text",
[
(
'''
def foo():
print('I ran in the code block')
foo()
''',
"I ran in the code block",
),
(
'''
def foo(arg):
print(f'You passed {arg}')
foo('bar')
''',
"You passed bar",
),
],
)
def test_inject_code_block(code_block, expected_text, notebook):
assert notebook.inject(code_block, pop=True).output_text == expected_text
def test_inject_raises_exception(notebook):
values = [3, {'key': 'value'}, ['a', 'b', 'c'], (1, 2, 3), {1, 2, 3}]
for value in values:
with pytest.raises(TypeError):
notebook.inject(value)
def test_inject_before_after(notebook):
notebook.inject("say_hello()", run=False, after="hello")
assert notebook.cells[notebook._cell_index("hello") + 1].source == "say_hello()"
notebook.inject("say_bye()", before="hello")
assert notebook.cells[notebook._cell_index("hello") - 1].source == "say_bye()"
with pytest.raises(ValueError):
notebook.inject("say_hello()", before="hello", after="bye")
def test_inject_pop(notebook):
assert notebook.inject("1+1", pop=True).execute_result == [{'text/plain': '2'}]
assert notebook.cells[-1].source != "1+1"
|