Debian Codegraph @

tests/test_cli.py

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