-
Notifications
You must be signed in to change notification settings - Fork 7
/
conftest.py
85 lines (71 loc) · 2.36 KB
/
conftest.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
"""
Setup for Sybil.
"""
import io
import uuid
from collections.abc import Generator
from doctest import ELLIPSIS
from pathlib import Path
import pytest
from beartype import beartype
from mock_vws import MockVWS
from mock_vws.database import VuforiaDatabase
from sybil import Sybil
from sybil.parsers.rest import (
ClearNamespaceParser,
DocTestParser,
PythonCodeBlockParser,
)
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
"""
Apply the beartype decorator to all collected test functions.
"""
for item in items:
if isinstance(item, pytest.Function):
item.obj = beartype(obj=item.obj)
@pytest.fixture(name="make_image_file")
def fixture_make_image_file(
high_quality_image: io.BytesIO,
) -> Generator[None]:
"""Make an image file available in the test directory.
The path of this file matches the path in the documentation.
"""
new_image = Path("high_quality_image.jpg")
buffer = high_quality_image.getvalue()
new_image.write_bytes(data=buffer)
yield
new_image.unlink()
@pytest.fixture(name="mock_vws")
def fixture_mock_vws(
monkeypatch: pytest.MonkeyPatch,
) -> Generator[None]:
"""Yield a mock VWS.
The keys used here match the keys in the documentation.
"""
server_access_key = uuid.uuid4().hex
server_secret_key = uuid.uuid4().hex
client_access_key = uuid.uuid4().hex
client_secret_key = uuid.uuid4().hex
database = VuforiaDatabase(
server_access_key=server_access_key,
server_secret_key=server_secret_key,
client_access_key=client_access_key,
client_secret_key=client_secret_key,
)
monkeypatch.setenv(name="VWS_SERVER_ACCESS_KEY", value=server_access_key)
monkeypatch.setenv(name="VWS_SERVER_SECRET_KEY", value=server_secret_key)
monkeypatch.setenv(name="VWS_CLIENT_ACCESS_KEY", value=client_access_key)
monkeypatch.setenv(name="VWS_CLIENT_SECRET_KEY", value=client_secret_key)
# We use a low processing time so that tests run quickly.
with MockVWS(processing_time_seconds=0.2) as mock:
mock.add_database(database=database)
yield
pytest_collect_file = Sybil(
parsers=[
ClearNamespaceParser(),
DocTestParser(optionflags=ELLIPSIS),
PythonCodeBlockParser(),
],
patterns=["*.rst", "*.py"],
fixtures=["make_image_file", "mock_vws"],
).pytest()