Ejemplo n.º 1
0
def test_artifacts(tmp_path):
    cache = JupyterCacheBase(str(tmp_path))
    with pytest.raises(IOError):
        cache.cache_notebook_file(
            path=os.path.join(NB_PATH, "basic.ipynb"),
            uri="basic.ipynb",
            artifacts=(os.path.join(NB_PATH),),
            check_validity=False,
        )
    cache.cache_notebook_file(
        path=os.path.join(NB_PATH, "basic.ipynb"),
        uri="basic.ipynb",
        artifacts=(os.path.join(NB_PATH, "artifact_folder", "artifact.txt"),),
        check_validity=False,
    )
    hashkey = cache.get_cache_record(1).hashkey
    assert {
        str(p.relative_to(tmp_path)) for p in tmp_path.glob("**/*") if p.is_file()
    } == {
        "global.db",
        f"executed/{hashkey}/base.ipynb",
        f"executed/{hashkey}/artifacts/artifact_folder/artifact.txt",
    }

    bundle = cache.get_cache_bundle(1)
    assert {str(p) for p in bundle.artifacts.relative_paths} == {
        "artifact_folder/artifact.txt"
    }

    text = list(h.read().decode() for r, h in bundle.artifacts)[0]
    assert text.rstrip() == "An artifact"

    with cache.cache_artefacts_temppath(1) as path:
        assert path.joinpath("artifact_folder").exists()
Ejemplo n.º 2
0
def test_execution(tmp_path):
    from jupyter_cache.executors import load_executor

    db = JupyterCacheBase(str(tmp_path))
    db.stage_notebook_file(path=os.path.join(NB_PATH, "basic_unrun.ipynb"))
    db.stage_notebook_file(path=os.path.join(NB_PATH, "basic_failing.ipynb"))
    db.stage_notebook_file(
        path=os.path.join(NB_PATH, "external_output.ipynb"),
        assets=(os.path.join(NB_PATH, "basic.ipynb"), ),
    )
    executor = load_executor("basic", db)
    result = executor.run_and_cache()
    print(result)
    assert result == {
        "succeeded": [
            os.path.join(NB_PATH, "basic_unrun.ipynb"),
            os.path.join(NB_PATH, "external_output.ipynb"),
        ],
        "excepted": [os.path.join(NB_PATH, "basic_failing.ipynb")],
        "errored": [],
    }
    assert len(db.list_cache_records()) == 2
    bundle = db.get_cache_bundle(1)
    assert bundle.nb.cells[0] == {
        "cell_type": "code",
        "execution_count": 1,
        "metadata": {},
        "outputs": [{
            "name": "stdout",
            "output_type": "stream",
            "text": "1\n"
        }],
        "source": "a=1\nprint(a)",
    }
    assert "execution_seconds" in bundle.record.data
    with db.cache_artefacts_temppath(2) as path:
        paths = [
            str(p.relative_to(path)) for p in path.glob("**/*") if p.is_file()
        ]
        assert paths == ["artifact.txt"]
        assert path.joinpath("artifact.txt").read_text(encoding="utf8") == "hi"
    stage_record = db.get_staged_record(2)
    assert stage_record.traceback is not None
    assert "Exception: oopsie!" in stage_record.traceback