Skip to content

Commit b4cf4f7

Browse files
Merge pull request #1821 from VWS-Python/pytest-readme
Fix code example and add doctest for it
2 parents 8d31375 + ff871d8 commit b4cf4f7

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ lint: \
1010
black \
1111
check-manifest \
1212
doc8 \
13+
doctest \
1314
linkcheck \
1415
mypy \
1516
ruff \

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
extensions = [
1515
"sphinx.ext.autodoc",
16+
"sphinx.ext.doctest",
1617
"sphinx.ext.intersphinx",
1718
"sphinx.ext.napoleon",
1819
"sphinx_autodoc_typehints",

docs/source/index.rst

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,58 @@ To write unit tests for code which uses this library, without using your Vuforia
6161

6262
pip3 install vws-python-mock
6363

64-
.. code:: python
64+
.. testsetup::
65+
66+
import pathlib
67+
import shutil
68+
69+
import vws_test_fixtures
6570

66-
from mock_vws import MockVWS, VuforiaDatabase
71+
# We rely on implementation details of the fixtures package.
72+
image = pathlib.Path(vws_test_fixtures.__path__[0]) / 'high_quality_image.jpg'
73+
assert image.exists(), image.resolve()
74+
new_image = pathlib.Path('high_quality_image.jpg')
75+
shutil.copy(image, new_image)
76+
77+
.. testcode::
78+
79+
import io
80+
import pathlib
81+
82+
from mock_vws.database import VuforiaDatabase
83+
from mock_vws import MockVWS
84+
from vws import CloudRecoService, VWS
6785

6886
with MockVWS() as mock:
6987
database = VuforiaDatabase()
7088
mock.add_database(database=database)
7189
vws_client = VWS(
72-
server_access_key=server_access_key,
73-
server_secret_key=server_secret_key,
90+
server_access_key=database.server_access_key,
91+
server_secret_key=database.server_secret_key,
7492
)
7593
cloud_reco_client = CloudRecoService(
76-
client_access_key=client_access_key,
77-
client_secret_key=client_secret_key,
94+
client_access_key=database.client_access_key,
95+
client_secret_key=database.client_secret_key,
7896
)
7997

80-
name = 'my_image_name'
8198

82-
with open('/path/to/image.png', 'rb') as my_image_file:
99+
image = pathlib.Path('high_quality_image.jpg')
100+
with image.open(mode='rb') as my_image_file:
83101
my_image = io.BytesIO(my_image_file.read())
84102

85103
target_id = vws_client.add_target(
86-
name=name,
104+
name="example_image_name",
87105
width=1,
88106
image=my_image,
107+
application_metadata=None,
108+
active_flag=True,
89109
)
90110

111+
.. testcleanup::
112+
113+
new_image = pathlib.Path('high_quality_image.jpg')
114+
new_image.unlink()
115+
91116
There are some differences between the mock and the real Vuforia.
92117
See https://vws-python-mock.readthedocs.io/en/latest/differences-to-vws.html for details.
93118

lint.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ linkcheck:
5757
.PHONY: spelling
5858
spelling:
5959
$(MAKE) -C docs/ spelling SPHINXOPTS=$(SPHINXOPTS)
60+
61+
.PHONY: doctest
62+
doctest:
63+
$(MAKE) -C docs/ doctest SPHINXOPTS=$(SPHINXOPTS)

0 commit comments

Comments
 (0)