Show More
@@ -923,51 +923,56 def test_notebook_export_json_with_output(): | |||||
923 | _ip.history_manager.reset() |
|
923 | _ip.history_manager.reset() | |
924 | _ip.colors = "neutral" |
|
924 | _ip.colors = "neutral" | |
925 |
|
925 | |||
926 | commands = [ |
|
926 | try: | |
927 | "display('test')", |
|
927 | commands = [ | |
928 |
"1 |
|
928 | "1/0", | |
929 | "display('a'), display('b')", |
|
929 | "print('test')", | |
930 | "1/0", |
|
930 | "display('test')", | |
931 | "print('test')", |
|
931 | "1+1", | |
932 | "import sys\nprint('test', file=sys.stderr)", |
|
932 | "display('a'), display('b')", | |
933 | ] |
|
933 | "import sys\nprint('test', file=sys.stderr)", | |
934 |
|
934 | ] | ||
935 | clean_nb = nbformat.v4.new_notebook( |
|
935 | ||
936 | cells=[nbformat.v4.new_code_cell(source=cmd) for cmd in commands] |
|
936 | clean_nb = nbformat.v4.new_notebook( | |
937 | ) |
|
937 | cells=[nbformat.v4.new_code_cell(source=cmd) for cmd in commands] | |
938 |
|
||||
939 | with TemporaryDirectory() as td: |
|
|||
940 | outfile = os.path.join(td, "nb.ipynb") |
|
|||
941 | client = NotebookClient( |
|
|||
942 | clean_nb, |
|
|||
943 | timeout=600, |
|
|||
944 | kernel_name="python3", |
|
|||
945 | resources={"metadata": {"path": td}}, |
|
|||
946 | allow_errors=True, |
|
|||
947 | ) |
|
938 | ) | |
948 | client.execute() |
|
|||
949 | nbformat.write(clean_nb, outfile) |
|
|||
950 | expected_nb = nbformat.read(outfile, as_version=4) |
|
|||
951 |
|
||||
952 | for cmd in commands: |
|
|||
953 | _ip.run_cell(cmd, store_history=True, silent=False) |
|
|||
954 |
|
939 | |||
955 | with TemporaryDirectory() as td: |
|
940 | with TemporaryDirectory() as td: | |
956 | outfile = os.path.join(td, "nb.ipynb") |
|
941 | outfile = os.path.join(td, "nb.ipynb") | |
957 | _ip.run_cell(f"%notebook {outfile}", store_history=True) |
|
942 | client = NotebookClient( | |
958 | actual_nb = nbformat.read(outfile, as_version=4) |
|
943 | clean_nb, | |
959 |
|
944 | timeout=600, | ||
960 | assert len(actual_nb["cells"]) == len(commands) |
|
945 | kernel_name="python3", | |
961 | assert len(expected_nb["cells"]) == len(commands) |
|
946 | resources={"metadata": {"path": td}}, | |
962 |
|
947 | allow_errors=True, | ||
963 | for i, command in enumerate(commands): |
|
948 | ) | |
964 | actual = actual_nb["cells"][i] |
|
949 | client.execute() | |
965 | expected = expected_nb["cells"][i] |
|
950 | nbformat.write(clean_nb, outfile) | |
966 | assert expected["source"] == command |
|
951 | expected_nb = nbformat.read(outfile, as_version=4) | |
967 | assert actual["source"] == expected["source"] |
|
952 | ||
968 | assert ( |
|
953 | for cmd in commands: | |
969 | actual["outputs"] == expected["outputs"] |
|
954 | _ip.run_cell(cmd, store_history=True, silent=False) | |
970 | ), f"Outputs do not match for cell {i+1} with source {command!r}" |
|
955 | print(f"\n{_ip.history_manager.outputs}\n") | |
|
956 | ||||
|
957 | with TemporaryDirectory() as td: | |||
|
958 | outfile = os.path.join(td, "nb.ipynb") | |||
|
959 | _ip.run_cell(f"%notebook {outfile}", store_history=True) | |||
|
960 | sleep(2) | |||
|
961 | actual_nb = nbformat.read(outfile, as_version=4) | |||
|
962 | ||||
|
963 | assert len(actual_nb["cells"]) == len(commands) | |||
|
964 | assert len(expected_nb["cells"]) == len(commands) | |||
|
965 | ||||
|
966 | for i, command in enumerate(commands): | |||
|
967 | actual = actual_nb["cells"][i] | |||
|
968 | expected = expected_nb["cells"][i] | |||
|
969 | assert expected["source"] == command | |||
|
970 | assert actual["source"] == expected["source"] | |||
|
971 | assert ( | |||
|
972 | actual["outputs"] == expected["outputs"] | |||
|
973 | ), f"Outputs do not match for cell {i+1} with source {command!r}" | |||
|
974 | finally: | |||
|
975 | _ip.colors = "nocolor" | |||
971 |
|
976 | |||
972 |
|
977 | |||
973 | class TestEnv(TestCase): |
|
978 | class TestEnv(TestCase): |
General Comments 0
You need to be logged in to leave comments.
Login now