-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ASIDataScience/factor-out-execution
Factor out execution
- Loading branch information
Showing
4 changed files
with
90 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ dist/ | |
.pytest_cache/ | ||
__pycache__/ | ||
*.pyc | ||
.coverage | ||
htmlcov/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import shutil | ||
from pathlib import Path | ||
from io import TextIOWrapper | ||
|
||
import click | ||
from papermill.cli import papermill | ||
|
||
|
||
def run( | ||
notebook: TextIOWrapper, | ||
directory: Path, | ||
execute: bool, | ||
click_context: click.Context, | ||
) -> Path: | ||
input_path = directory / "input.ipynb" | ||
output_path = directory / "output.ipynb" | ||
with input_path.open("w") as input_file: | ||
shutil.copyfileobj(notebook, input_file) | ||
|
||
if execute: | ||
papermill_click_context = papermill.make_context( | ||
"The papermill execution command.", | ||
[str(input_path), str(output_path)] + click_context.args, | ||
parent=click_context, | ||
) | ||
|
||
papermill.invoke(papermill_click_context) | ||
else: | ||
shutil.copy(str(input_path), str(output_path)) | ||
|
||
return output_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from io import StringIO | ||
from pathlib import Path | ||
from unittest.mock import Mock, patch | ||
import pytest | ||
|
||
from faculty_mill.execute import run | ||
|
||
|
||
@pytest.fixture | ||
def tempdir(tmpdir): | ||
"""Fixture that wraps pytest fixture to return Path object""" | ||
return Path(str(tmpdir)) | ||
|
||
|
||
@pytest.fixture | ||
def tmpnotebook(): | ||
test_notebook = StringIO() | ||
test_notebook.write("test notebook content") | ||
test_notebook.seek(0) | ||
return test_notebook | ||
|
||
|
||
@pytest.fixture | ||
def mock_click_context(): | ||
click_context = Mock() | ||
click_context.args = ["arg 1", "arg 2"] | ||
return click_context | ||
|
||
|
||
def test_that_run_copies_content(tempdir, tmpnotebook, mock_click_context): | ||
output_notebook = run( | ||
tmpnotebook, tempdir, execute=False, click_context=mock_click_context | ||
) | ||
assert output_notebook.parent == tempdir | ||
assert output_notebook.read_text() == "test notebook content" | ||
|
||
|
||
@patch("faculty_mill.execute.papermill") | ||
def test_that_run_calls_papermill( | ||
mock_papermill, tempdir, tmpnotebook, mock_click_context | ||
): | ||
output_notebook = run( | ||
tmpnotebook, tempdir, execute=True, click_context=mock_click_context | ||
) | ||
assert output_notebook.parent == tempdir | ||
mock_papermill.make_context.assert_called_once_with( | ||
"The papermill execution command.", | ||
[str(tempdir / "input.ipynb"), str(tempdir / "output.ipynb")] | ||
+ mock_click_context.args, | ||
parent=mock_click_context, | ||
) | ||
mock_papermill.invoke.assert_called_once_with( | ||
mock_papermill.make_context.return_value | ||
) |