Skip to content

Commit 43e2ac6

Browse files
committed
Add VuMark target setup to mock backends
1 parent bf6ed8c commit 43e2ac6

2 files changed

Lines changed: 102 additions & 84 deletions

File tree

tests/mock_vws/fixtures/vuforia_backends.py

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Choose which backends to use for the tests."""
22

33
import contextlib
4+
import io
45
import logging
56
from collections.abc import Generator
67
from enum import Enum
@@ -9,6 +10,7 @@
910
import requests
1011
import responses
1112
from beartype import beartype
13+
from PIL import Image
1214
from requests_mock_flask import add_flask_app_to_mock
1315
from vws import VWS
1416
from vws.exceptions.vws_exceptions import (
@@ -21,6 +23,9 @@
2123
from mock_vws._flask_server.vws import VWS_FLASK_APP
2224
from mock_vws.database import VuforiaDatabase
2325
from mock_vws.states import States
26+
from mock_vws.target import Target
27+
from mock_vws.target_raters import HardcodedTargetTrackingRater
28+
from tests.mock_vws.fixtures.credentials import VuMarkVuforiaDatabase
2429
from tests.mock_vws.utils.retries import RETRY_ON_TOO_MANY_REQUESTS
2530

2631
LOGGER = logging.getLogger(name=__name__)
@@ -57,16 +62,51 @@ def _delete_all_targets(*, database_keys: VuforiaDatabase) -> None:
5762
vws_client.delete_target(target_id=target)
5863

5964

65+
@beartype
66+
def _rgb_png_bytes() -> bytes:
67+
"""Return a small RGB PNG image."""
68+
image = Image.new(mode="RGB", size=(8, 8), color=(255, 0, 0))
69+
image_file = io.BytesIO()
70+
image.save(fp=image_file, format="PNG")
71+
return image_file.getvalue()
72+
73+
74+
@beartype
75+
def _vumark_database(
76+
*,
77+
vumark_vuforia_database: VuMarkVuforiaDatabase,
78+
) -> VuforiaDatabase:
79+
"""Return a database with a target for VuMark instance generation."""
80+
vumark_target = Target(
81+
active_flag=True,
82+
application_metadata=None,
83+
image_value=_rgb_png_bytes(),
84+
name="mock-vumark-target",
85+
processing_time_seconds=0,
86+
width=1,
87+
target_tracking_rater=HardcodedTargetTrackingRater(rating=5),
88+
target_id=vumark_vuforia_database.target_id,
89+
)
90+
return VuforiaDatabase(
91+
database_name=vumark_vuforia_database.target_manager_database_name,
92+
server_access_key=vumark_vuforia_database.server_access_key,
93+
server_secret_key=vumark_vuforia_database.server_secret_key,
94+
targets={vumark_target},
95+
)
96+
97+
6098
@beartype
6199
def _enable_use_real_vuforia(
62100
*,
63101
working_database: VuforiaDatabase,
64102
inactive_database: VuforiaDatabase,
103+
vumark_vuforia_database: VuMarkVuforiaDatabase,
65104
monkeypatch: pytest.MonkeyPatch,
66105
) -> Generator[None]:
67106
"""Test against the real Vuforia."""
68107
assert monkeypatch
69108
assert inactive_database
109+
assert vumark_vuforia_database
70110
_delete_all_targets(database_keys=working_database)
71111
yield
72112

@@ -76,6 +116,7 @@ def _enable_use_mock_vuforia(
76116
*,
77117
working_database: VuforiaDatabase,
78118
inactive_database: VuforiaDatabase,
119+
vumark_vuforia_database: VuMarkVuforiaDatabase,
79120
monkeypatch: pytest.MonkeyPatch,
80121
) -> Generator[None]:
81122
"""Test against the in-memory mock Vuforia."""
@@ -96,10 +137,14 @@ def _enable_use_mock_vuforia(
96137
client_access_key=inactive_database.client_access_key,
97138
client_secret_key=inactive_database.client_secret_key,
98139
)
140+
vumark_database = _vumark_database(
141+
vumark_vuforia_database=vumark_vuforia_database,
142+
)
99143

100144
with MockVWS() as mock:
101145
mock.add_database(database=working_database)
102146
mock.add_database(database=inactive_database)
147+
mock.add_database(database=vumark_database)
103148
yield
104149

105150

@@ -108,6 +153,7 @@ def _enable_use_docker_in_memory(
108153
*,
109154
working_database: VuforiaDatabase,
110155
inactive_database: VuforiaDatabase,
156+
vumark_vuforia_database: VuMarkVuforiaDatabase,
111157
monkeypatch: pytest.MonkeyPatch,
112158
) -> Generator[None]:
113159
"""Test against mock Vuforia created to be run in a container."""
@@ -170,6 +216,13 @@ def _enable_use_docker_in_memory(
170216
json=inactive_database.to_dict(),
171217
timeout=30,
172218
)
219+
requests.post(
220+
url=databases_url,
221+
json=_vumark_database(
222+
vumark_vuforia_database=vumark_vuforia_database,
223+
).to_dict(),
224+
timeout=30,
225+
)
173226

174227
yield
175228

@@ -233,8 +286,9 @@ def fixture_verify_mock_vuforia(
233286
request: pytest.FixtureRequest,
234287
vuforia_database: VuforiaDatabase,
235288
inactive_database: VuforiaDatabase,
289+
vumark_vuforia_database: VuMarkVuforiaDatabase,
236290
monkeypatch: pytest.MonkeyPatch,
237-
) -> Generator[VuforiaBackend]:
291+
) -> Generator[None]:
238292
"""Test functions which use this fixture are run multiple times. Once
239293
with
240294
the real Vuforia, and once with each mock.
@@ -257,12 +311,12 @@ def fixture_verify_mock_vuforia(
257311
VuforiaBackend.DOCKER_IN_MEMORY: _enable_use_docker_in_memory,
258312
}[backend]
259313

260-
for _ in enable_function(
314+
yield from enable_function(
261315
working_database=vuforia_database,
262316
inactive_database=inactive_database,
317+
vumark_vuforia_database=vumark_vuforia_database,
263318
monkeypatch=monkeypatch,
264-
):
265-
yield backend
319+
)
266320

267321

268322
@pytest.fixture(
@@ -278,6 +332,7 @@ def mock_only_vuforia(
278332
request: pytest.FixtureRequest,
279333
vuforia_database: VuforiaDatabase,
280334
inactive_database: VuforiaDatabase,
335+
vumark_vuforia_database: VuMarkVuforiaDatabase,
281336
monkeypatch: pytest.MonkeyPatch,
282337
) -> Generator[None]:
283338
"""Test functions which use this fixture are run multiple times. Once
@@ -305,5 +360,6 @@ def mock_only_vuforia(
305360
yield from enable_function(
306361
working_database=vuforia_database,
307362
inactive_database=inactive_database,
363+
vumark_vuforia_database=vumark_vuforia_database,
308364
monkeypatch=monkeypatch,
309365
)
Lines changed: 42 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,63 @@
11
"""Tests for the VuMark generation web API."""
22

3-
import base64
4-
import io
53
import json
64
from http import HTTPMethod, HTTPStatus
75
from uuid import uuid4
86

97
import pytest
108
import requests
11-
from vws import VWS
129
from vws_auth_tools import authorization_header, rfc_1123_date
1310

14-
from mock_vws.database import VuforiaDatabase
1511
from tests.mock_vws.fixtures.credentials import VuMarkVuforiaDatabase
16-
from tests.mock_vws.fixtures.vuforia_backends import VuforiaBackend
1712

1813
_VWS_HOST = "https://vws.vuforia.com"
1914
_PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
2015

2116

2217
@pytest.mark.usefixtures("verify_mock_vuforia")
23-
class TestGenerateInstance:
24-
"""Tests for VuMark instance generation."""
25-
26-
_TINY_PNG = base64.b64decode(
27-
s=(
28-
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4"
29-
"2mP8/x8AAwMCAO7Zl6kAAAAASUVORK5CYII="
30-
),
31-
)
32-
33-
@staticmethod
34-
def _create_mock_target_id(vuforia_database: VuforiaDatabase) -> str:
35-
"""Create and return a target ID for mock backends."""
36-
vws_client = VWS(
37-
server_access_key=vuforia_database.server_access_key,
38-
server_secret_key=vuforia_database.server_secret_key,
18+
def test_generate_instance_success(
19+
vumark_vuforia_database: VuMarkVuforiaDatabase,
20+
) -> None:
21+
"""A VuMark instance can be generated with valid template settings."""
22+
if vumark_vuforia_database.target_id.startswith("<"):
23+
pytest.skip(
24+
reason=(
25+
"VuMark target ID is a placeholder. "
26+
"Set VUMARK_VUFORIA_TARGET_ID."
27+
),
3928
)
40-
return vws_client.add_target(
41-
name=uuid4().hex,
42-
width=1,
43-
image=io.BytesIO(initial_bytes=TestGenerateInstance._TINY_PNG),
44-
active_flag=True,
45-
application_metadata=None,
46-
)
47-
48-
def test_generate_instance_success(
49-
self,
50-
verify_mock_vuforia: VuforiaBackend,
51-
vuforia_database: VuforiaDatabase,
52-
vumark_vuforia_database: VuMarkVuforiaDatabase,
53-
) -> None:
54-
"""A VuMark instance can be generated with valid template settings."""
55-
if verify_mock_vuforia == VuforiaBackend.REAL:
56-
server_access_key = vumark_vuforia_database.server_access_key
57-
server_secret_key = vumark_vuforia_database.server_secret_key
58-
target_id = vumark_vuforia_database.target_id
59-
else:
60-
server_access_key = vuforia_database.server_access_key
61-
server_secret_key = vuforia_database.server_secret_key
62-
target_id = self._create_mock_target_id(
63-
vuforia_database=vuforia_database
64-
)
6529

66-
request_path = f"/targets/{target_id}/instances"
67-
content_type = "application/json"
68-
generated_instance_id = uuid4().hex
69-
content = json.dumps(
70-
obj={"instance_id": generated_instance_id}
71-
).encode(encoding="utf-8")
72-
date = rfc_1123_date()
73-
authorization_string = authorization_header(
74-
access_key=server_access_key,
75-
secret_key=server_secret_key,
76-
method=HTTPMethod.POST,
77-
content=content,
78-
content_type=content_type,
79-
date=date,
80-
request_path=request_path,
81-
)
30+
request_path = f"/targets/{vumark_vuforia_database.target_id}/instances"
31+
content_type = "application/json"
32+
generated_instance_id = uuid4().hex
33+
content = json.dumps(obj={"instance_id": generated_instance_id}).encode(
34+
encoding="utf-8"
35+
)
36+
date = rfc_1123_date()
37+
authorization_string = authorization_header(
38+
access_key=vumark_vuforia_database.server_access_key,
39+
secret_key=vumark_vuforia_database.server_secret_key,
40+
method=HTTPMethod.POST,
41+
content=content,
42+
content_type=content_type,
43+
date=date,
44+
request_path=request_path,
45+
)
8246

83-
response = requests.post(
84-
url=_VWS_HOST + request_path,
85-
headers={
86-
"Accept": "image/png",
87-
"Authorization": authorization_string,
88-
"Content-Length": str(object=len(content)),
89-
"Content-Type": content_type,
90-
"Date": date,
91-
},
92-
data=content,
93-
timeout=30,
94-
)
47+
response = requests.post(
48+
url=_VWS_HOST + request_path,
49+
headers={
50+
"Accept": "image/png",
51+
"Authorization": authorization_string,
52+
"Content-Length": str(object=len(content)),
53+
"Content-Type": content_type,
54+
"Date": date,
55+
},
56+
data=content,
57+
timeout=30,
58+
)
9559

96-
assert response.status_code == HTTPStatus.OK
97-
assert (
98-
response.headers["Content-Type"].split(sep=";")[0] == "image/png"
99-
)
100-
assert response.content.startswith(_PNG_SIGNATURE)
101-
assert len(response.content) > len(_PNG_SIGNATURE)
60+
assert response.status_code == HTTPStatus.OK
61+
assert response.headers["Content-Type"].split(sep=";")[0] == "image/png"
62+
assert response.content.startswith(_PNG_SIGNATURE)
63+
assert len(response.content) > len(_PNG_SIGNATURE)

0 commit comments

Comments
 (0)