Skip to content

Commit c158aa7

Browse files
committed
Add TargetNameExist error
1 parent b7dec85 commit c158aa7

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/vws/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,18 @@ def __init__(self, response: Response) -> None:
4848
"""
4949
super().__init__()
5050
self.response = response
51+
52+
53+
class TargetNameExist(Exception):
54+
"""
55+
Exception raised when Vuforia returns a response with a result code
56+
'TargetNameExist'.
57+
"""
58+
59+
def __init__(self, response: Response) -> None:
60+
"""
61+
Args:
62+
response: The response to a request to Vuforia.
63+
"""
64+
super().__init__()
65+
self.response = response

src/vws/vws.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from vws._authorization import authorization_header, rfc_1123_date
1818
from vws.exceptions import (
1919
MetadataTooLarge,
20+
TargetNameExist,
2021
TargetStatusProcessing,
2122
UnknownTarget,
2223
)
@@ -109,9 +110,10 @@ class _ResultCodes(Enum):
109110

110111

111112
_EXCEPTIONS = {
112-
_ResultCodes.UNKNOWN_TARGET: UnknownTarget,
113-
_ResultCodes.TARGET_STATUS_PROCESSING: TargetStatusProcessing,
114113
_ResultCodes.METADATA_TOO_LARGE: MetadataTooLarge,
114+
_ResultCodes.TARGET_NAME_EXIST: TargetNameExist,
115+
_ResultCodes.TARGET_STATUS_PROCESSING: TargetStatusProcessing,
116+
_ResultCodes.UNKNOWN_TARGET: UnknownTarget,
115117
}
116118

117119

tests/test_add_target.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from requests import codes
1010

1111
from vws import VWS
12-
from vws.exceptions import MetadataTooLarge
12+
from vws.exceptions import MetadataTooLarge, TargetNameExist
1313

1414

1515
class TestSuccess:
@@ -51,6 +51,28 @@ def test_add_two_targets(
5151
client.add_target(name='a', width=1, image=high_quality_image)
5252

5353

54+
class TestName:
55+
"""
56+
Tests for the ``name`` parameter to ``add_target``.
57+
"""
58+
59+
def test_add_two_targets_same_name(
60+
self,
61+
client: VWS,
62+
high_quality_image: io.BytesIO,
63+
) -> None:
64+
"""
65+
A ``TargetNameExist`` exception is raised after adding two targets with
66+
the same name.
67+
"""
68+
client.add_target(name='x', width=1, image=high_quality_image)
69+
70+
with pytest.raises(TargetNameExist) as exc:
71+
client.add_target(name='x', width=1, image=high_quality_image)
72+
73+
assert exc.value.response.status_code == codes.FORBIDDEN
74+
75+
5476
class TestCustomBaseURL:
5577
"""
5678
Tests for adding images to databases under custom VWS URLs.

0 commit comments

Comments
 (0)