-
Notifications
You must be signed in to change notification settings - Fork 78
/
test_execute.py
302 lines (251 loc) · 12 KB
/
test_execute.py
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import pytest
@pytest.mark.sphinx_params(
"basic_unrun.ipynb", conf={"jupyter_execute_notebooks": "auto"}
)
def test_basic_unrun_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
assert "test_name" in sphinx_run.app.env.metadata["basic_unrun"]
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
# Test execution statistics, should look like:
# {'basic_unrun': {'mtime': '2020-08-20T03:32:27.061454', 'runtime': 0.964572671,
# 'method': 'auto', 'succeeded': True}}
assert sphinx_run.env.nb_execution_data_changed is True
assert "basic_unrun" in sphinx_run.env.nb_execution_data
assert sphinx_run.env.nb_execution_data["basic_unrun"]["method"] == "auto"
assert sphinx_run.env.nb_execution_data["basic_unrun"]["succeeded"] is True
@pytest.mark.sphinx_params(
"basic_unrun.ipynb", conf={"jupyter_execute_notebooks": "cache"}
)
def test_basic_unrun_cache(sphinx_run, file_regression, check_nbs):
"""The outputs should be populated."""
sphinx_run.build()
assert sphinx_run.warnings() == ""
assert "test_name" in sphinx_run.app.env.metadata["basic_unrun"]
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
# Test execution statistics, should look like:
# {'basic_unrun': {'mtime': '2020-08-20T03:32:27.061454', 'runtime': 0.964572671,
# 'method': 'cache', 'succeeded': True}}
assert sphinx_run.env.nb_execution_data_changed is True
assert "basic_unrun" in sphinx_run.env.nb_execution_data
assert sphinx_run.env.nb_execution_data["basic_unrun"]["method"] == "cache"
assert sphinx_run.env.nb_execution_data["basic_unrun"]["succeeded"] is True
@pytest.mark.sphinx_params(
"basic_unrun.ipynb", conf={"jupyter_execute_notebooks": "cache"}
)
def test_rebuild_cache(sphinx_run):
"""The notebook should only be executed once."""
sphinx_run.build()
assert "Executing" in sphinx_run.status(), sphinx_run.status()
sphinx_run.invalidate_files()
sphinx_run.build()
assert "Executing" not in sphinx_run.status(), sphinx_run.status()
@pytest.mark.sphinx_params(
"basic_unrun.ipynb", conf={"jupyter_execute_notebooks": "force"}
)
def test_rebuild_force(sphinx_run):
"""The notebook should be executed twice."""
sphinx_run.build()
assert "Executing" in sphinx_run.status(), sphinx_run.status()
sphinx_run.invalidate_files()
sphinx_run.build()
assert "Executing" in sphinx_run.status(), sphinx_run.status()
@pytest.mark.sphinx_params(
"basic_unrun.ipynb",
conf={
"jupyter_execute_notebooks": "cache",
"execution_excludepatterns": ["basic_*"],
},
)
def test_exclude_path(sphinx_run, file_regression):
"""The notebook should not be executed."""
sphinx_run.build()
assert len(sphinx_run.app.env.nb_excluded_exec_paths) == 1
assert "Executing" not in sphinx_run.status(), sphinx_run.status()
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
@pytest.mark.sphinx_params(
"basic_failing.ipynb", conf={"jupyter_execute_notebooks": "cache"}
)
def test_basic_failing_cache(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert "Execution Failed" in sphinx_run.warnings()
assert (
"Couldn't find cache key for notebook file source/basic_failing.ipynb"
in sphinx_run.warnings()
)
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
sphinx_run.get_report_file()
assert "basic_failing" in sphinx_run.env.nb_execution_data
assert sphinx_run.env.nb_execution_data["basic_failing"]["method"] == "cache"
assert sphinx_run.env.nb_execution_data["basic_failing"]["succeeded"] is False
assert "error_log" in sphinx_run.env.nb_execution_data["basic_failing"]
@pytest.mark.sphinx_params(
"basic_failing.ipynb", conf={"jupyter_execute_notebooks": "auto"}
)
def test_basic_failing_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert "Execution Failed" in sphinx_run.warnings()
assert "Execution Failed with traceback saved in" in sphinx_run.warnings()
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
sphinx_run.get_report_file()
assert "basic_failing" in sphinx_run.env.nb_execution_data
assert sphinx_run.env.nb_execution_data["basic_failing"]["method"] == "auto"
assert sphinx_run.env.nb_execution_data["basic_failing"]["succeeded"] is False
assert "error_log" in sphinx_run.env.nb_execution_data["basic_failing"]
@pytest.mark.sphinx_params(
"basic_failing.ipynb",
conf={"jupyter_execute_notebooks": "cache", "execution_allow_errors": True},
)
def test_allow_errors_cache(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert not sphinx_run.warnings()
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
@pytest.mark.sphinx_params(
"basic_failing.ipynb",
conf={"jupyter_execute_notebooks": "auto", "execution_allow_errors": True},
)
def test_allow_errors_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert not sphinx_run.warnings()
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
@pytest.mark.sphinx_params(
"basic_unrun.ipynb", conf={"jupyter_execute_notebooks": "force"}
)
def test_outputs_present(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
assert "test_name" in sphinx_run.app.env.metadata["basic_unrun"]
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
@pytest.mark.sphinx_params(
"complex_outputs_unrun.ipynb", conf={"jupyter_execute_notebooks": "cache"}
)
def test_complex_outputs_unrun_cache(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
# Widget view and widget state should make it into the HTML
html = sphinx_run.get_html()
assert '<script type="application/vnd.jupyter.widget-view+json">' in html
assert '<script type="application/vnd.jupyter.widget-state+json">' in html
@pytest.mark.sphinx_params(
"complex_outputs_unrun.ipynb", conf={"jupyter_execute_notebooks": "auto"}
)
def test_complex_outputs_unrun_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
# Widget view and widget state should make it into the HTML
html = sphinx_run.get_html()
assert '<script type="application/vnd.jupyter.widget-view+json">' in html
assert '<script type="application/vnd.jupyter.widget-state+json">' in html
@pytest.mark.sphinx_params(
"basic_unrun.ipynb", conf={"jupyter_execute_notebooks": "off"}
)
def test_no_execute(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
@pytest.mark.sphinx_params(
"basic_unrun.ipynb", conf={"jupyter_execute_notebooks": "cache"}
)
def test_jupyter_cache_path(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
assert "Execution Succeeded" in sphinx_run.status()
assert sphinx_run.warnings() == ""
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
# Testing relative paths within the notebook
@pytest.mark.sphinx_params(
"basic_relative.ipynb", conf={"jupyter_execute_notebooks": "cache"}
)
def test_relative_path_cache(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
assert "Executing" in sphinx_run.status(), sphinx_run.status()
@pytest.mark.sphinx_params(
"basic_relative.ipynb", conf={"jupyter_execute_notebooks": "force"}
)
def test_relative_path_force(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
assert "Executing" in sphinx_run.status(), sphinx_run.status()
# Execution timeout configuration
@pytest.mark.sphinx_params(
"complex_outputs_unrun.ipynb",
conf={"jupyter_execute_notebooks": "cache", "execution_timeout": 1},
)
def test_execution_timeout(sphinx_run, file_regression, check_nbs):
""" execution should fail given the low timeout value"""
sphinx_run.build()
# print(sphinx_run.status())
assert "execution failed" in sphinx_run.warnings()
@pytest.mark.sphinx_params(
"complex_outputs_unrun_timeout.ipynb",
conf={"jupyter_execute_notebooks": "cache", "execution_timeout": 60},
)
def test_execution_metadata_timeout(sphinx_run, file_regression, check_nbs):
""" notebook timeout metadata has higher preference then execution_timeout config"""
sphinx_run.build()
assert "execution failed" in sphinx_run.warnings()
@pytest.mark.sphinx_params(
"nb_exec_table.md", conf={"jupyter_execute_notebooks": "auto"},
)
def test_nb_exec_table(sphinx_run, file_regression, check_nbs):
"""Test that the table gets output into the HTML,
including a row for the executed notebook.
"""
sphinx_run.build()
assert not sphinx_run.warnings()
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
assert '<tr class="row-even"><td><p>nb_exec_table</p></td>' in sphinx_run.get_html()
@pytest.mark.sphinx_params(
"custom-formats.Rmd",
conf={
"jupyter_execute_notebooks": "auto",
"nb_custom_formats": {".Rmd": ["jupytext.reads", {"fmt": "Rmd"}]},
},
)
def test_custom_convert_auto(sphinx_run, file_regression, check_nbs):
sphinx_run.build()
# print(sphinx_run.status())
assert sphinx_run.warnings() == ""
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
assert sphinx_run.env.nb_execution_data_changed is True
assert "custom-formats" in sphinx_run.env.nb_execution_data
assert sphinx_run.env.nb_execution_data["custom-formats"]["method"] == "auto"
assert sphinx_run.env.nb_execution_data["custom-formats"]["succeeded"] is True
@pytest.mark.sphinx_params(
"custom-formats.Rmd",
conf={
"jupyter_execute_notebooks": "cache",
"nb_custom_formats": {".Rmd": ["jupytext.reads", {"fmt": "Rmd"}]},
},
)
def test_custom_convert_cache(sphinx_run, file_regression, check_nbs):
"""The outputs should be populated."""
sphinx_run.build()
assert sphinx_run.warnings() == ""
file_regression.check(sphinx_run.get_nb(), check_fn=check_nbs, extension=".ipynb")
file_regression.check(sphinx_run.get_doctree().pformat(), extension=".xml")
assert sphinx_run.env.nb_execution_data_changed is True
assert "custom-formats" in sphinx_run.env.nb_execution_data
assert sphinx_run.env.nb_execution_data["custom-formats"]["method"] == "cache"
assert sphinx_run.env.nb_execution_data["custom-formats"]["succeeded"] is True