def test_export_nodes_nothing_to_write(self, project_path, nodes_path): nodes = json.dumps({ "cells": [ { "cell_type": "code", "source": "print('hello world')", "metadata": {}, }, { "cell_type": "text", "source": "hello world", "metadata": { "tags": ["node"] }, }, ] }) notebook_file = project_path / "notebook.iypnb" notebook_file.write_text(nodes) with pytest.warns(UserWarning, match="Skipping notebook"): output_path = nodes_path / "{}.py".format(notebook_file.stem) _export_nodes(notebook_file, output_path) output_path = nodes_path / "notebook.py" assert not output_path.exists()
def test_export_nodes(self, project_path, nodes_path): nodes = json.dumps( { "cells": [ { "cell_type": "code", "source": "print('hello world')", "metadata": {"tags": ["node"]}, }, { "cell_type": "code", "source": "print(10+5)", "metadata": {"tags": ["node"]}, }, {"cell_type": "code", "source": "a = 10", "metadata": {}}, ] } ) notebook_file = project_path / "notebook.ipynb" notebook_file.write_text(nodes) output_path = nodes_path / f"{notebook_file.stem}.py" _export_nodes(notebook_file, output_path) assert output_path.is_file() assert output_path.read_text() == "print('hello world')\nprint(10+5)\n"
def test_export_nodes_different_notebook_paths(self, project_path, nodes_path): nodes = json.dumps( { "cells": [ { "cell_type": "code", "source": "print('hello world')", "metadata": {"tags": ["node"]}, } ] } ) notebook_file1 = project_path / "notebook1.ipynb" notebook_file1.write_text(nodes) output_path1 = nodes_path / "notebook1.py" notebook_file2 = nodes_path / "notebook2.ipynb" notebook_file2.write_text(nodes) output_path2 = nodes_path / "notebook2.py" _export_nodes(notebook_file1, output_path1) _export_nodes(notebook_file2, output_path2) assert output_path1.read_text() == "print('hello world')\n" assert output_path2.read_text() == "print('hello world')\n"
def test_export_nodes_json_error(self, nodes_path): random_file = nodes_path / "notebook.txt" random_file.touch() random_file.write_text("original") output_path = nodes_path / f"{random_file.stem}.py" pattern = "Provided filepath is not a Jupyter notebook" with pytest.raises(KedroCliError, match=pattern): _export_nodes(random_file, output_path)
def test_export_nodes_overwrite(self, project_path, nodes_path): existing_nodes = nodes_path / "notebook.py" existing_nodes.touch() existing_nodes.write_text("original") nodes = json.dumps({ "cells": [{ "cell_type": "code", "source": "print('hello world')", "metadata": { "tags": ["node"] }, }] }) notebook_file = project_path / "notebook.iypnb" notebook_file.write_text(nodes) output_path = nodes_path / f"{notebook_file.stem}.py" _export_nodes(notebook_file, output_path) assert output_path.is_file() assert output_path.read_text() == "print('hello world')\n"