Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix faulty prerun logic in notebook_loader #28

Merged
merged 2 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion testbook/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def cell_output_text(self, cell):
if 'text' in output:
text += output['text']

return text
return text.strip()

def inject(self, code, args=None, prerun=None):
"""Injects given function and executes with arguments passed
Expand Down
12 changes: 5 additions & 7 deletions testbook/execute.py → testbook/notebook_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ def __init__(self, nb_path, prerun=None):
with open(self.nb_path) as f:
nb = nbformat.read(f, as_version=4)

client = TestbookNotebookClient(nb)

if self.prerun is not None:
with client.setup_kernel():
client.execute_cell(self.prerun)

self.client = client
self.client = TestbookNotebookClient(nb)

def _start_kernel(self):
if self.client.km is None:
Expand All @@ -30,6 +24,8 @@ def _start_kernel(self):

def __enter__(self):
self._start_kernel()
if self.prerun is not None:
self.client.execute_cell(self.prerun)
return self.client

def __exit__(self, *args):
Expand All @@ -38,6 +34,8 @@ def __exit__(self, *args):
def __call__(self, func):
def wrapper(*args, **kwargs):
with self.client.setup_kernel():
if self.prerun is not None:
self.client.execute_cell(self.prerun)
func(self.client, *args, **kwargs)

wrapper.__name__ = func.__name__
Expand Down
27 changes: 17 additions & 10 deletions testbook/tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,37 @@
def test_execute_cell():
with notebook_loader('testbook/tests/resources/foo.ipynb') as notebook:
notebook.execute_cell(1)
assert notebook.cell_output_text(1) == 'hello world\n[1, 2, 3]\n'
assert notebook.cell_output_text(1) == 'hello world\n[1, 2, 3]'

notebook.execute_cell([2, 3])
assert notebook.cell_output_text(3) == 'foo\n'
assert notebook.cell_output_text(3) == 'foo'


def test_execute_cell_tags():
with notebook_loader('testbook/tests/resources/foo.ipynb') as notebook:
notebook.execute_cell('test1')
assert notebook.cell_output_text('test1') == 'hello world\n[1, 2, 3]\n'
assert notebook.cell_output_text('test1') == 'hello world\n[1, 2, 3]'

notebook.execute_cell(['prepare_foo', 'execute_foo'])
assert notebook.cell_output_text('execute_foo') == 'foo\n'
assert notebook.cell_output_text('execute_foo') == 'foo'


@notebook_loader("testbook/tests/resources/foo.ipynb")
def test_notebook(notebook):
def test_notebook_loader(notebook):
notebook.execute_cell('test1')
assert notebook.cell_output_text('test1') == 'hello world\n[1, 2, 3]\n'
assert notebook.cell_output_text('test1') == 'hello world\n[1, 2, 3]'

notebook.execute_cell(['prepare_foo', 'execute_foo'])
assert notebook.cell_output_text('execute_foo') == 'foo\n'
assert notebook.cell_output_text('execute_foo') == 'foo'


@notebook_loader("testbook/tests/resources/foo.ipynb", prerun='test1')
def test_notebook_with_prerun(notebook):
assert notebook.cell_output_text(1) == 'hello world\n[1, 2, 3]\n'
@notebook_loader("testbook/tests/resources/foo.ipynb", prerun='prepare_foo')
def test_notebook_loader_with_prerun(notebook):
notebook.execute_cell('execute_foo')
assert notebook.cell_output_text('execute_foo') == 'foo'


def test_notebook_loader_with_prerun_context_manager():
with notebook_loader("testbook/tests/resources/foo.ipynb", prerun='prepare_foo') as notebook:
notebook.execute_cell('execute_foo')
assert notebook.cell_output_text('execute_foo') == 'foo'