Skip to content

Commit 9a8e8cc

Browse files
committed
Separate tests for VWS and VWQ exceptions
1 parent ae8fd9c commit 9a8e8cc

File tree

4 files changed

+169
-128
lines changed

4 files changed

+169
-128
lines changed

src/vws/vws.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ def _make_request(
148148
149149
Returns:
150150
The response to the request made by `requests`.
151+
152+
Raises:
153+
~vws.exceptions.UnknownVWSErrorPossiblyBadName: Vuforia returns an
154+
HTML page with the text "Oops, an error occurred". This has
155+
been seen to happen when the given name includes a bad
156+
character.
151157
"""
152158
response = _target_api_request(
153159
server_access_key=self._server_access_key,

tests/test_cloud_reco_exceptions.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
"""
2+
Tests for exceptions raised when using the CloudRecoService.
3+
"""
4+
5+
import io
6+
from http import HTTPStatus
7+
8+
import pytest
9+
import requests
10+
from mock_vws import MockVWS
11+
from mock_vws.database import VuforiaDatabase
12+
from mock_vws.states import States
13+
14+
from vws import VWS, CloudRecoService
15+
from vws.exceptions.base_exceptions import CloudRecoException
16+
from vws.exceptions.cloud_reco_exceptions import (
17+
AuthenticationFailure,
18+
MatchProcessing,
19+
MaxNumResultsOutOfRange,
20+
InactiveProject,
21+
)
22+
from vws.exceptions.custom_exceptions import (
23+
ConnectionErrorPossiblyImageTooLarge,
24+
)
25+
26+
27+
def test_too_many_max_results(
28+
cloud_reco_client: CloudRecoService,
29+
high_quality_image: io.BytesIO,
30+
) -> None:
31+
"""
32+
A ``MaxNumResultsOutOfRange`` error is raised if the given
33+
``max_num_results`` is out of range.
34+
"""
35+
with pytest.raises(MaxNumResultsOutOfRange) as exc:
36+
cloud_reco_client.query(
37+
image=high_quality_image,
38+
max_num_results=51,
39+
)
40+
41+
expected_value = (
42+
"Integer out of range (51) in form data part 'max_result'. "
43+
'Accepted range is from 1 to 50 (inclusive).'
44+
)
45+
assert str(exc.value) == exc.value.response.text == expected_value
46+
47+
48+
def test_image_too_large(
49+
cloud_reco_client: CloudRecoService,
50+
png_too_large: io.BytesIO,
51+
) -> None:
52+
"""
53+
A ``ConnectionErrorPossiblyImageTooLarge`` exception is raised if an
54+
image which is too large is given.
55+
"""
56+
with pytest.raises(ConnectionErrorPossiblyImageTooLarge) as exc:
57+
cloud_reco_client.query(image=png_too_large)
58+
59+
assert isinstance(exc.value, requests.ConnectionError)
60+
61+
62+
def test_cloudrecoexception_inheritance() -> None:
63+
"""
64+
CloudRecoService-specific exceptions inherit from CloudRecoException.
65+
"""
66+
subclasses = [
67+
MatchProcessing,
68+
MaxNumResultsOutOfRange,
69+
]
70+
for subclass in subclasses:
71+
assert issubclass(subclass, CloudRecoException)
72+
73+
74+
def test_base_exception(
75+
vws_client: VWS,
76+
cloud_reco_client: CloudRecoService,
77+
high_quality_image: io.BytesIO,
78+
) -> None:
79+
"""
80+
``CloudRecoException``s has a response property.
81+
"""
82+
vws_client.add_target(
83+
name='x',
84+
width=1,
85+
image=high_quality_image,
86+
active_flag=True,
87+
application_metadata=None,
88+
)
89+
90+
with pytest.raises(CloudRecoException) as exc:
91+
cloud_reco_client.query(image=high_quality_image)
92+
93+
assert exc.value.response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
94+
95+
96+
def test_match_processing(
97+
vws_client: VWS,
98+
cloud_reco_client: CloudRecoService,
99+
high_quality_image: io.BytesIO,
100+
) -> None:
101+
"""
102+
A ``MatchProcessing`` exception is raised when a target in processing is
103+
matched.
104+
"""
105+
vws_client.add_target(
106+
name='x',
107+
width=1,
108+
image=high_quality_image,
109+
active_flag=True,
110+
application_metadata=None,
111+
)
112+
with pytest.raises(MatchProcessing) as exc:
113+
cloud_reco_client.query(image=high_quality_image)
114+
assert exc.value.response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
115+
116+
117+
def test_authentication_failure(high_quality_image: io.BytesIO) -> None:
118+
"""
119+
An ``AuthenticationFailure`` exception is raised when the client access key
120+
exists but the client secret key is incorrect.
121+
"""
122+
database = VuforiaDatabase()
123+
cloud_reco_client = CloudRecoService(
124+
client_access_key=database.client_access_key,
125+
client_secret_key='a',
126+
)
127+
with MockVWS() as mock:
128+
mock.add_database(database=database)
129+
130+
with pytest.raises(AuthenticationFailure) as exc:
131+
cloud_reco_client.query(image=high_quality_image)
132+
133+
assert exc.value.response.status_code == HTTPStatus.UNAUTHORIZED
134+
135+
136+
def test_inactive_project(high_quality_image: io.BytesIO) -> None:
137+
"""
138+
An ``InactiveProject`` exception is raised when querying an inactive
139+
database.
140+
"""
141+
database = VuforiaDatabase(state=States.PROJECT_INACTIVE)
142+
with MockVWS() as mock:
143+
mock.add_database(database=database)
144+
cloud_reco_client = CloudRecoService(
145+
client_access_key=database.client_access_key,
146+
client_secret_key=database.client_secret_key,
147+
)
148+
149+
with pytest.raises(InactiveProject) as exc:
150+
cloud_reco_client.query(image=high_quality_image)
151+
152+
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN

tests/test_query.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@
55
import io
66
import uuid
77

8-
import pytest
9-
import requests
108
from mock_vws import MockVWS
119
from mock_vws.database import VuforiaDatabase
1210

1311
from vws import VWS, CloudRecoService
14-
from vws.exceptions.cloud_reco_exceptions import MaxNumResultsOutOfRange
15-
from vws.exceptions.custom_exceptions import (
16-
ConnectionErrorPossiblyImageTooLarge,
17-
)
1812
from vws.include_target_data import CloudRecoIncludeTargetData
1913

2014

@@ -54,20 +48,6 @@ def test_match(
5448
[matching_target] = cloud_reco_client.query(image=high_quality_image)
5549
assert matching_target.target_id == target_id
5650

57-
def test_too_large(
58-
self,
59-
cloud_reco_client: CloudRecoService,
60-
png_too_large: io.BytesIO,
61-
) -> None:
62-
"""
63-
A ``ConnectionErrorPossiblyImageTooLarge`` exception is raised if an
64-
image which is too large is given.
65-
"""
66-
with pytest.raises(ConnectionErrorPossiblyImageTooLarge) as exc:
67-
cloud_reco_client.query(image=png_too_large)
68-
69-
assert isinstance(exc.value, requests.ConnectionError)
70-
7151

7252
class TestCustomBaseVWQURL:
7353
"""
@@ -182,27 +162,6 @@ def test_custom(
182162
)
183163
assert len(matches) == 2
184164

185-
def test_too_many(
186-
self,
187-
cloud_reco_client: CloudRecoService,
188-
high_quality_image: io.BytesIO,
189-
) -> None:
190-
"""
191-
A ``MaxNumResultsOutOfRange`` error is raised if the given
192-
``max_num_results`` is out of range.
193-
"""
194-
with pytest.raises(MaxNumResultsOutOfRange) as exc:
195-
cloud_reco_client.query(
196-
image=high_quality_image,
197-
max_num_results=51,
198-
)
199-
200-
expected_value = (
201-
"Integer out of range (51) in form data part 'max_result'. "
202-
'Accepted range is from 1 to 50 (inclusive).'
203-
)
204-
assert str(exc.value) == exc.value.response.text == expected_value
205-
206165

207166
class TestIncludeTargetData:
208167
"""

tests/test_exceptions.py renamed to tests/test_vws_exceptions.py

Lines changed: 11 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Tests for various exceptions.
2+
Tests for VWS exceptions.
33
"""
44

55
import io
@@ -11,13 +11,8 @@
1111
from mock_vws.database import VuforiaDatabase
1212
from mock_vws.states import States
1313

14-
from vws import VWS, CloudRecoService
15-
from vws.exceptions.base_exceptions import CloudRecoException, VWSException
16-
from vws.exceptions.cloud_reco_exceptions import (
17-
InactiveProject,
18-
MatchProcessing,
19-
MaxNumResultsOutOfRange,
20-
)
14+
from vws import VWS
15+
from vws.exceptions.base_exceptions import VWSException
2116
from vws.exceptions.custom_exceptions import UnknownVWSErrorPossiblyBadName
2217
from vws.exceptions.vws_exceptions import (
2318
AuthenticationFailure,
@@ -187,25 +182,6 @@ def test_project_inactive(high_quality_image: io.BytesIO) -> None:
187182
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
188183

189184

190-
def test_inactive_project(high_quality_image: io.BytesIO) -> None:
191-
"""
192-
An ``InactiveProject`` exception is raised when querying an inactive
193-
database.
194-
"""
195-
database = VuforiaDatabase(state=States.PROJECT_INACTIVE)
196-
with MockVWS() as mock:
197-
mock.add_database(database=database)
198-
cloud_reco_client = CloudRecoService(
199-
client_access_key=database.client_access_key,
200-
client_secret_key=database.client_secret_key,
201-
)
202-
203-
with pytest.raises(InactiveProject) as exc:
204-
cloud_reco_client.query(image=high_quality_image)
205-
206-
assert exc.value.response.status_code == HTTPStatus.FORBIDDEN
207-
208-
209185
def test_target_status_processing(
210186
vws_client: VWS,
211187
high_quality_image: io.BytesIO,
@@ -278,12 +254,14 @@ def test_authentication_failure(high_quality_image: io.BytesIO) -> None:
278254
incorrect.
279255
"""
280256
database = VuforiaDatabase()
257+
258+
vws_client = VWS(
259+
server_access_key=database.server_access_key,
260+
server_secret_key='a',
261+
)
262+
281263
with MockVWS() as mock:
282264
mock.add_database(database=database)
283-
vws_client = VWS(
284-
server_access_key=database.server_access_key,
285-
server_secret_key='a',
286-
)
287265

288266
with pytest.raises(AuthenticationFailure) as exc:
289267
vws_client.add_target(
@@ -296,16 +274,6 @@ def test_authentication_failure(high_quality_image: io.BytesIO) -> None:
296274

297275
assert exc.value.response.status_code == HTTPStatus.UNAUTHORIZED
298276

299-
cloud_reco_client = CloudRecoService(
300-
client_access_key=database.client_access_key,
301-
client_secret_key='a',
302-
)
303-
304-
with pytest.raises(AuthenticationFailure) as exc:
305-
cloud_reco_client.query(image=high_quality_image)
306-
307-
assert exc.value.response.status_code == HTTPStatus.UNAUTHORIZED
308-
309277

310278
def test_target_status_not_success(
311279
vws_client: VWS,
@@ -330,27 +298,6 @@ def test_target_status_not_success(
330298
assert exc.value.target_id == target_id
331299

332300

333-
def test_match_processing(
334-
vws_client: VWS,
335-
cloud_reco_client: CloudRecoService,
336-
high_quality_image: io.BytesIO,
337-
) -> None:
338-
"""
339-
A ``MatchProcessing`` exception is raised when a target in processing is
340-
matched.
341-
"""
342-
vws_client.add_target(
343-
name='x',
344-
width=1,
345-
image=high_quality_image,
346-
active_flag=True,
347-
application_metadata=None,
348-
)
349-
with pytest.raises(MatchProcessing) as exc:
350-
cloud_reco_client.query(image=high_quality_image)
351-
assert exc.value.response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
352-
353-
354301
def test_vwsexception_inheritance() -> None:
355302
"""
356303
VWS-related exceptions should inherit from VWSException.
@@ -377,27 +324,12 @@ def test_vwsexception_inheritance() -> None:
377324
assert issubclass(subclass, VWSException)
378325

379326

380-
def test_cloudrecoexception_inheritance() -> None:
381-
"""
382-
CloudRecoService-specific exceptions should inherit from
383-
CloudRecoException.
384-
"""
385-
subclasses = [
386-
MatchProcessing,
387-
MaxNumResultsOutOfRange,
388-
]
389-
for subclass in subclasses:
390-
assert issubclass(subclass, CloudRecoException)
391-
392-
393-
def test_base_exceptions_properties(
327+
def test_base_exception(
394328
vws_client: VWS,
395-
cloud_reco_client: CloudRecoService,
396329
high_quality_image: io.BytesIO,
397330
) -> None:
398331
"""
399-
``VWSException``s and ``CloudRecoException```s each have a response
400-
property.
332+
``VWSException``s has a response property.
401333
"""
402334
with pytest.raises(VWSException) as exc:
403335
vws_client.get_target_record(target_id='a')
@@ -411,11 +343,3 @@ def test_base_exceptions_properties(
411343
active_flag=True,
412344
application_metadata=None,
413345
)
414-
415-
with pytest.raises(CloudRecoException) as cloud_reco_exc:
416-
cloud_reco_client.query(image=high_quality_image)
417-
418-
assert (
419-
cloud_reco_exc.value.response.status_code
420-
== HTTPStatus.INTERNAL_SERVER_ERROR
421-
)

0 commit comments

Comments
 (0)