-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_documentation_notebook.py
More file actions
186 lines (157 loc) · 7.04 KB
/
test_documentation_notebook.py
File metadata and controls
186 lines (157 loc) · 7.04 KB
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
import unittest
import os
import sys
import importlib
import shutil
import subprocess
import time
import warnings
from nbconvert import PythonExporter
from teachpyx import __file__ as teachpyx_file
from teachpyx.ext_test_case import ExtTestCase
VERBOSE = 0
ROOT = os.path.realpath(os.path.abspath(os.path.join(teachpyx_file, "..", "..")))
def import_source(module_file_path, module_name):
if not os.path.exists(module_file_path):
raise FileNotFoundError(module_file_path)
module_spec = importlib.util.spec_from_file_location(module_name, module_file_path)
if module_spec is None:
raise RuntimeError(
f"Unable to find or execute {module_name!r} in {module_file_path!r}."
)
module = importlib.util.module_from_spec(module_spec)
return module_spec.loader.exec_module(module)
class TestDocumentationNotebook(ExtTestCase):
_tmp = "temp_notebooks"
def post_process(self, content):
lines = []
for line in content.split("\n"):
if "get_ipython()" in line:
line = "# " + line
lines.append(line)
return "\n".join(lines)
def run_test(self, nb_name: str, verbose=0) -> int:
ppath = os.environ.get("PYTHONPATH", "")
if len(ppath) == 0:
os.environ["PYTHONPATH"] = ROOT
elif ROOT not in ppath:
sep = ";" if sys.platform == "win32" else ":"
os.environ["PYTHONPATH"] = ppath + sep + ROOT
perf = time.perf_counter()
exporter = PythonExporter()
content = self.post_process(exporter.from_filename(nb_name)[0])
bcontent = content.encode("utf-8")
tmp = self._tmp
if not os.path.exists(tmp):
os.mkdir(tmp)
# with tempfile.NamedTemporaryFile(suffix=".py") as tmp:
name = os.path.splitext(os.path.split(nb_name)[-1])[0]
if os.path.exists(tmp):
tmp_name = os.path.join(tmp, name + ".py")
self.assertEndsWith(tmp_name, ".py")
with open(tmp_name, "wb") as f:
f.write(bcontent)
fold, name = os.path.split(tmp_name)
try:
mod = import_source(fold, os.path.splitext(name)[0])
assert mod is not None
except (FileNotFoundError, RuntimeError):
# try another way
cmds = [sys.executable, "-u", tmp_name]
p = subprocess.Popen(
cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
res = p.communicate()
out, err = res
st = err.decode("ascii", errors="ignore")
if "No such file or directory" in st:
raise FileNotFoundError(st) # noqa: B904
if len(st) > 0 and "Traceback" in st:
msg = (
f"Example {nb_name!r} (cmd: {cmds} - "
f"exec_prefix={sys.exec_prefix!r}) "
f"failed due to\n{st}"
)
if "CERTIFICATE_VERIFY_FAILED" in st and sys.platform == "win32":
warnings.warn(msg, stacklevel=0)
else:
raise AssertionError(msg) # noqa: B904
dt = time.perf_counter() - perf
if verbose:
print(f"{dt:.3f}: run {name!r}")
return 1
@classmethod
def add_test_methods_path(cls, fold, copy_folder=None):
if copy_folder:
full_path = os.path.join(fold, copy_folder)
assert os.path.exists(full_path), f"Unable to find {full_path!r}"
dest = copy_folder
if not os.path.exists(dest):
os.makedirs(dest)
for name in os.listdir(full_path):
if not os.path.exists(os.path.join(dest, name)):
shutil.copy(os.path.join(full_path, name), dest)
found = os.listdir(fold)
last = os.path.split(fold)[-1]
for name in found:
if name.endswith(".ipynb"):
fullname = os.path.join(fold, name)
if "interro_rapide_" in name or (
sys.platform == "win32"
and (
"protobuf" in name
or "td_note_2021" in name
or "nb_pandas" in name
)
):
@unittest.skip("notebook with questions or issues with windows")
def _test_(self, fullname=fullname):
res = self.run_test(fullname, verbose=VERBOSE)
self.assertIn(res, (-1, 1))
elif "module_file_regex" in name and sys.platform != "win32":
@unittest.skip("issues with linux")
def _test_(self, fullname=fullname):
res = self.run_test(fullname, verbose=VERBOSE)
self.assertIn(res, (-1, 1))
elif (
"ml_a_tree_overfitting" in name
and os.environ.get("CIRCLECI", "undefined") != "undefined"
):
@unittest.skip("issues with circleci")
def _test_(self, fullname=fullname):
res = self.run_test(fullname, verbose=VERBOSE)
self.assertIn(res, (-1, 1))
elif "pretraitement_son" in name:
@unittest.skip("audio not working")
def _test_(self, fullname=fullname):
res = self.run_test(fullname, verbose=VERBOSE)
self.assertIn(res, (-1, 1))
else:
def _test_(self, fullname=fullname):
res = self.run_test(fullname, verbose=VERBOSE)
self.assertIn(res, (-1, 1))
lasts = last.replace("-", "_")
names = os.path.splitext(name)[0].replace("-", "_")
setattr(cls, f"test_{lasts}_{names}", _test_)
@classmethod
def add_test_methods(cls):
this = os.path.abspath(os.path.dirname(__file__))
folds = [
os.path.join(this, "..", "..", "_doc", "c_data"),
os.path.join(this, "..", "..", "_doc", "practice", "algo-base"),
os.path.join(this, "..", "..", "_doc", "practice", "algo-compose"),
os.path.join(this, "..", "..", "_doc", "practice", "exams"),
os.path.join(this, "..", "..", "_doc", "practice", "ml"),
os.path.join(this, "..", "..", "_doc", "practice", "py-base"),
os.path.join(this, "..", "..", "_doc", "practice", "tds-base"),
os.path.join(this, "..", "..", "_doc", "practice", "years", "2023"),
os.path.join(this, "..", "..", "_doc", "practice", "years", "2025"),
]
for fold in folds:
cls.add_test_methods_path(
os.path.normpath(fold),
copy_folder=("images" if fold.endswith(("ml", "py-base")) else None),
)
TestDocumentationNotebook.add_test_methods()
if __name__ == "__main__":
unittest.main(verbosity=2)