def test_execution_timeout_config(tmp_path): """ tests the timeout value passed to the executor""" from jupyter_cache.executors import load_executor db = JupyterCacheBase(str(tmp_path)) db.stage_notebook_file( path=os.path.join(NB_PATH, "complex_outputs_unrun.ipynb")) executor = load_executor("basic", db) result = executor.run_and_cache(timeout=60) assert result == { "succeeded": [os.path.join(NB_PATH, "complex_outputs_unrun.ipynb")], "excepted": [], "errored": [], } db.clear_cache() db.stage_notebook_file( path=os.path.join(NB_PATH, "complex_outputs_unrun.ipynb")) executor = load_executor("basic", db) result = executor.run_and_cache(timeout=1) assert result == { "succeeded": [], "excepted": [os.path.join(NB_PATH, "complex_outputs_unrun.ipynb")], "errored": [], }
def clear_cache(cache_path): """Clear the cache completely.""" from jupyter_cache.cache.main import JupyterCacheBase db = JupyterCacheBase(cache_path) click.confirm("Are you sure you want to permanently clear the cache!?", abort=True) db.clear_cache() click.secho("Cache cleared!", fg="green")
def test_basic_workflow(tmp_path): cache = JupyterCacheBase(str(tmp_path)) with pytest.raises(NbValidityError): cache.cache_notebook_file(path=os.path.join(NB_PATH, "basic.ipynb")) cache.cache_notebook_file( path=os.path.join(NB_PATH, "basic.ipynb"), uri="basic.ipynb", check_validity=False, ) assert cache.list_cache_records()[0].uri == "basic.ipynb" pk = cache.match_cache_file(path=os.path.join(NB_PATH, "basic.ipynb")).pk nb_bundle = cache.get_cache_bundle(pk) assert nb_bundle.nb.metadata["kernelspec"] == { "display_name": "Python 3", "language": "python", "name": "python3", } assert set(nb_bundle.record.to_dict().keys()) == { "pk", "hashkey", "uri", "data", "created", "accessed", "description", } # assert cache.get_cache_codecell(pk, 0).source == "a=1\nprint(a)" path = os.path.join(NB_PATH, "basic_failing.ipynb") diff = cache.diff_nbfile_with_cache(pk, path, as_str=True, use_color=False) assert diff == dedent( f"""\ nbdiff --- cached pk=1 +++ other: {path} ## inserted before nb/cells/0: + code cell: + source: + raise Exception('oopsie!') ## deleted nb/cells/0: - code cell: - execution_count: 2 - source: - a=1 - print(a) - outputs: - output 0: - output_type: stream - name: stdout - text: - 1 """ ) cache.remove_cache(pk) assert cache.list_cache_records() == [] cache.cache_notebook_file( path=os.path.join(NB_PATH, "basic.ipynb"), uri="basic.ipynb", check_validity=False, ) with pytest.raises(ValueError): cache.stage_notebook_file(os.path.join(NB_PATH, "basic.ipynb"), assets=[""]) cache.stage_notebook_file( os.path.join(NB_PATH, "basic.ipynb"), assets=[os.path.join(NB_PATH, "basic.ipynb")], ) assert [r.pk for r in cache.list_staged_records()] == [1] assert [r.pk for r in cache.list_staged_unexecuted()] == [] cache.stage_notebook_file(os.path.join(NB_PATH, "basic_failing.ipynb")) assert [r.pk for r in cache.list_staged_records()] == [1, 2] assert [r.pk for r in cache.list_staged_unexecuted()] == [2] bundle = cache.get_staged_notebook(os.path.join(NB_PATH, "basic_failing.ipynb")) assert bundle.nb.metadata cache.clear_cache() assert cache.list_cache_records() == []