forked from openvinotoolkit/openvino_notebooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_notebooks.py
92 lines (79 loc) · 3.31 KB
/
test_notebooks.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
import urllib.error
import urllib.request
from pathlib import Path
from typing import Set
import pytest
import toml
from pip._internal.req import parse_requirements
def get_parsed_requirements(requirements_file: str) -> Set:
"""
Returns a set of requirements that are defined in `requirements_file`,
without versions
"""
requirements_set = set()
parsed_requirements = parse_requirements(requirements_file, session=False)
separators = ("=", "<", ">", "[")
for req in parsed_requirements:
requirement = req.requirement
for separator in separators:
requirement = requirement.replace(separator, "|")
requirements_set.add(requirement.split("|")[0])
return requirements_set
def test_readme():
"""
Test that all notebooks have a README file and exist in the Notebooks README
"""
notebooks_readme = Path("notebooks/README.md").read_text(encoding="utf-8")
for item in Path("notebooks").iterdir():
if item.is_dir():
# item is a notebook directory
notebook_dir = item.relative_to("notebooks")
if str(notebook_dir)[0].isdigit():
assert "README.md" in [
filename.name for filename in item.iterdir()
], f"README not found in {item}"
assert (
str(notebook_dir) in notebooks_readme
), f"{item} not found in notebooks README"
def test_requirements_docker():
"""
Test that requirements.txt is a subset of Docker requirements in Pipfile
This test does not check requirements versions, it only verifies existence
"""
with open(".docker/Pipfile") as f:
pipfile_contents = toml.load(f)
docker_requirements = set(list(pipfile_contents["packages"].keys()))
pip_requirements = get_parsed_requirements("requirements.txt")
assert pip_requirements.issubset(
docker_requirements
), f"Docker Pipfile misses: {pip_requirements.difference(docker_requirements)}"
def test_requirements_binder():
"""
Test that requirements.txt is a subset of Binder requirements
This test does not check requirements versions, it only verifies existence
"""
pip_requirements = get_parsed_requirements("requirements.txt")
binder_requirements = get_parsed_requirements(".binder/requirements.txt")
assert pip_requirements.issubset(
binder_requirements
), f"Binder requirements misses: {pip_requirements.difference(binder_requirements)}"
@pytest.mark.skip(reason="URL existence is tested in docker_nbval")
def test_urls_exist():
"""
Test that urls that may be cached still exist on the server
"""
urls = [
"http://cs231n.stanford.edu/tiny-imagenet-200.zip",
"https://github.com/onnx/models/raw/main/vision/style_transfer/fast_neural_style/model/pointilism-9.onnx",
"https://storage.openvinotoolkit.org/data/test_data/openvino_notebooks/kits19/case_00030.zip",
]
opener = urllib.request.build_opener()
opener.addheaders = [("User-agent", "Mozilla/5.0")]
urllib.request.install_opener(opener)
for url in urls:
try:
urlobject = urllib.request.urlopen(url, timeout=10)
assert urlobject.status == 200
except urllib.error.HTTPError:
print(f"Downloading {url} failed")
raise