1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
import os
from pathlib import Path
from click.testing import CliRunner
import pytest
from jupyter_cache.cache.main import JupyterCacheBase
from jupyter_cache.cli import CacheContext
from jupyter_cache.cli.commands import cmd_cache, cmd_main, cmd_notebook, cmd_project
NB_PATH = os.path.join(os.path.realpath(os.path.dirname(__file__)), "notebooks")
class Runner(CliRunner):
def __init__(self, path) -> None:
super().__init__()
self._cache_path = path
def create_cache(self) -> JupyterCacheBase:
return JupyterCacheBase(str(self._cache_path))
def invoke(self, *args, **kwargs):
return super().invoke(*args, **kwargs, obj=CacheContext(self._cache_path))
@pytest.fixture()
def runner(tmp_path):
return Runner(tmp_path)
def test_base(runner: Runner):
result = runner.invoke(cmd_main.jcache, "-v")
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "jupyter-cache version" in result.output.strip(), result.output
def test_clear_cache(runner: Runner):
result = runner.invoke(cmd_project.clear_cache, input="y")
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "Cache cleared!" in result.output.strip(), result.output
def test_list_caches(runner: Runner):
db = runner.create_cache()
db.cache_notebook_file(
path=os.path.join(NB_PATH, "basic.ipynb"),
uri="basic.ipynb",
check_validity=False,
)
result = runner.invoke(cmd_cache.list_caches, [])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "basic.ipynb" in result.output.strip(), result.output
def test_list_caches_latest_only(runner: Runner):
db = runner.create_cache()
db.cache_notebook_file(
path=os.path.join(NB_PATH, "basic.ipynb"),
uri="basic.ipynb",
check_validity=False,
)
db.cache_notebook_file(
path=os.path.join(NB_PATH, "complex_outputs.ipynb"),
uri="basic.ipynb",
check_validity=False,
)
result = runner.invoke(cmd_cache.list_caches, [])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert len(result.output.strip().splitlines()) == 4, result.output
result = runner.invoke(cmd_cache.list_caches, ["--latest-only"])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert len(result.output.strip().splitlines()) == 3, result.output
def test_cache_with_artifact(runner: Runner):
nb_path = os.path.join(NB_PATH, "basic.ipynb")
a_path = os.path.join(NB_PATH, "artifact_folder", "artifact.txt")
result = runner.invoke(
cmd_cache.cache_nb, ["--no-validate", "-nb", nb_path, a_path]
)
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "basic.ipynb" in result.output.strip(), result.output
result = runner.invoke(cmd_cache.cached_info, ["1"])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "- artifact_folder/artifact.txt" in result.output.strip(), result.output
result = runner.invoke(
cmd_cache.cat_artifact, ["1", "artifact_folder/artifact.txt"]
)
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "An artifact" in result.output.strip(), result.output
def test_cache_nbs(runner: Runner):
db = runner.create_cache()
path = os.path.join(NB_PATH, "basic.ipynb")
result = runner.invoke(cmd_cache.cache_nbs, ["--no-validate", path])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "basic.ipynb" in result.output.strip(), result.output
assert db.list_cache_records()[0].uri == path
def test_remove_caches(runner: Runner):
db = runner.create_cache()
db.cache_notebook_file(
path=os.path.join(NB_PATH, "basic.ipynb"),
uri="basic.ipynb",
check_validity=False,
)
result = runner.invoke(cmd_cache.remove_caches, ["1"])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "Success" in result.output.strip(), result.output
assert db.list_cache_records() == []
def test_diff_nbs(runner: Runner):
db = runner.create_cache()
path = os.path.join(NB_PATH, "basic.ipynb")
path2 = os.path.join(NB_PATH, "basic_failing.ipynb")
db.cache_notebook_file(path, check_validity=False)
# nb_bundle = db.get_cache_bundle(1)
# nb_bundle.nb.cells[0].source = "# New Title"
# db.stage_notebook_bundle(nb_bundle)
result = runner.invoke(cmd_cache.diff_nb, ["1", path2])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
print(result.output.splitlines()[2:])
assert result.output.splitlines()[1:] == [
"--- cached pk=1",
f"+++ other: {path2}",
"## 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",
"",
"",
"Success!",
]
def test_add_nbs_to_project(runner: Runner):
db = runner.create_cache()
path = os.path.join(NB_PATH, "basic.ipynb")
result = runner.invoke(cmd_notebook.add_notebooks, [path])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "basic.ipynb" in result.output.strip(), result.output
assert db.list_project_records()[0].uri == path
def test_remove_nbs_from_project(runner: Runner):
db = runner.create_cache()
path = os.path.join(NB_PATH, "basic.ipynb")
result = runner.invoke(cmd_notebook.add_notebooks, [path])
result = runner.invoke(cmd_notebook.remove_nbs, [path])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "basic.ipynb" in result.output.strip(), result.output
assert db.list_project_records() == []
def test_clear_project(runner: Runner):
db = runner.create_cache()
path = os.path.join(NB_PATH, "basic.ipynb")
result = runner.invoke(cmd_notebook.add_notebooks, [path])
result = runner.invoke(cmd_notebook.clear_nbs, [], input="y")
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert db.list_project_records() == []
def test_list_nbs_in_project(runner: Runner):
db = runner.create_cache()
db.cache_notebook_file(
path=os.path.join(NB_PATH, "basic.ipynb"), check_validity=False
)
db.add_nb_to_project(path=os.path.join(NB_PATH, "basic.ipynb"))
db.add_nb_to_project(path=os.path.join(NB_PATH, "basic_failing.ipynb"))
result = runner.invoke(cmd_notebook.list_nbs_in_project, [])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "basic.ipynb" in result.output.strip(), result.output
def test_show_project_record(runner: Runner):
db = runner.create_cache()
db.cache_notebook_file(
path=os.path.join(NB_PATH, "basic.ipynb"), check_validity=False
)
db.add_nb_to_project(path=os.path.join(NB_PATH, "basic.ipynb"))
result = runner.invoke(cmd_notebook.show_project_record, ["1"])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert "basic.ipynb" in result.output.strip(), result.output
def test_project_execute(runner: Runner):
db = runner.create_cache()
db.add_nb_to_project(path=os.path.join(NB_PATH, "basic.ipynb"))
result = runner.invoke(cmd_project.execute_nbs, [])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert len(db.list_cache_records()) == 1
def test_project_merge(runner: Runner, tmp_path: Path):
db = runner.create_cache()
record = db.add_nb_to_project(path=os.path.join(NB_PATH, "basic_unrun.ipynb"))
db.cache_notebook_file(
path=os.path.join(NB_PATH, "basic.ipynb"),
uri="basic.ipynb",
check_validity=False,
)
result = runner.invoke(
cmd_notebook.merge_executed,
[str(record.pk), str(tmp_path / "output.ipynb")],
)
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert (tmp_path / "output.ipynb").exists()
def test_project_invalidate(runner: Runner):
db = runner.create_cache()
db.cache_notebook_file(
path=os.path.join(NB_PATH, "basic.ipynb"), check_validity=False
)
db.add_nb_to_project(path=os.path.join(NB_PATH, "basic.ipynb"))
result = runner.invoke(cmd_notebook.invalidate_nbs, ["1"])
assert result.exception is None, result.output
assert result.exit_code == 0, result.output
assert db.list_project_records()
assert not db.list_cache_records()
|