Skip to content

Commit af62231

Browse files
committed
Add unit test for convert_notebook_to_script
1 parent 2aa8157 commit af62231

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

test/test_core.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,30 @@ def test_script_creator_init_with_parameters():
128128
"parameter_2": (str, "default value"),
129129
}
130130

131+
131132
def test_script_creator_init_no_parameters():
132133
script_creator = ScriptCreator(
133134
pathlib.Path(__file__).parent / "data" / "noparamtest.ipynb"
134135
)
135136
assert script_creator.nb_params.params == {}
137+
138+
139+
@pytest.mark.parametrize("clear", [True, False])
140+
def test_script_creator_convert_notebook_to_script(tmp_path, clear):
141+
(output_dir := tmp_path / "output").mkdir()
142+
extraneous_filename = "foo"
143+
(output_dir / extraneous_filename).touch()
144+
script_creator = ScriptCreator(
145+
pathlib.Path(__file__).parent / "data" / "paramtest.ipynb"
146+
)
147+
script_creator.convert_notebook_to_script(output_dir, clear)
148+
filenames = {
149+
"user_code.py",
150+
"execute.py",
151+
"parameters.py",
152+
"parameters.yaml",
153+
"util.py",
154+
} | (set() if clear else {extraneous_filename})
155+
expected = {output_dir / f for f in filenames}
156+
assert set(output_dir.iterdir()) == expected
157+
# TODO test execution as well?

xcengine/core.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def convert_notebook_to_script(
5454
(body, resources) = exporter.from_notebook_node(self.notebook)
5555
with open(output_dir / "user_code.py", "w") as fh:
5656
fh.write(
57-
"import unittest.mock\n"
58-
"get_ipython = unittest.mock.MagicMock\n"
57+
"import unittest.mock\n"
58+
"get_ipython = unittest.mock.MagicMock\n"
5959
)
6060
fh.write(body)
6161
parent_dir = pathlib.Path(__file__).parent
@@ -84,8 +84,7 @@ def process_params_cell(self) -> None:
8484
# turns them into no-ops
8585
setup_code = (
8686
"import unittest.mock\n"
87-
"get_ipython = unittest.mock.MagicMock\n"
88-
+ setup_code
87+
"get_ipython = unittest.mock.MagicMock\n" + setup_code
8988
)
9089
params_node = nbformat.from_dict(self.notebook)
9190
params_node.cells = [params_node.cells[params_cell_index]]

0 commit comments

Comments
 (0)