Skip to content

Commit 7c6a9e8

Browse files
Merge pull request #830 from adamtheturtle/more-exception-tests
Add a few more exception tests
2 parents de59188 + 914b219 commit 7c6a9e8

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

src/vws/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ def __init__(self, response: Response) -> None:
2020
self.response = response
2121

2222

23+
class BadImage(Exception):
24+
"""
25+
Exception raised when Vuforia returns a response with a result code
26+
'BadImage'.
27+
"""
28+
29+
def __init__(self, response: Response) -> None:
30+
"""
31+
Args:
32+
response: The response to a request to Vuforia.
33+
"""
34+
super().__init__()
35+
self.response = response
36+
37+
2338
class TargetStatusProcessing(Exception):
2439
"""
2540
Exception raised when Vuforia returns a response with a result code

src/vws/vws.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from vws._authorization import authorization_header, rfc_1123_date
1717
from vws.exceptions import (
18+
BadImage,
1819
ImageTooLarge,
1920
MetadataTooLarge,
2021
TargetNameExist,
@@ -99,6 +100,7 @@ def _raise_for_result_code(
99100
return
100101

101102
exception = {
103+
'BadImage': BadImage,
102104
'ImageTooLarge': ImageTooLarge,
103105
'MetadataTooLarge': MetadataTooLarge,
104106
'TargetNameExist': TargetNameExist,

tests/test_add_target.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
import pytest
88
from mock_vws import MockVWS
9-
from requests import codes
109

1110
from vws import VWS
12-
from vws.exceptions import TargetNameExist
1311

1412

1513
class TestSuccess:
@@ -51,28 +49,6 @@ def test_add_two_targets(
5149
client.add_target(name='a', width=1, image=high_quality_image)
5250

5351

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-
7652
class TestCustomBaseURL:
7753
"""
7854
Tests for adding images to databases under custom VWS URLs.

tests/test_exceptions.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
from vws import VWS
1313
from vws.exceptions import (
14+
BadImage,
1415
ImageTooLarge,
1516
MetadataTooLarge,
17+
TargetNameExist,
1618
TargetStatusProcessing,
1719
UnknownTarget,
1820
)
@@ -88,6 +90,32 @@ def test_request_quota_reached() -> None:
8890
"""
8991

9092

93+
def test_bad_image(client: VWS) -> None:
94+
"""
95+
A ``BadImage`` exception is raised when a non-image is given.
96+
"""
97+
not_an_image = io.BytesIO(b'Not an image')
98+
with pytest.raises(BadImage) as exc:
99+
client.add_target(name='x', width=1, image=not_an_image)
100+
101+
assert exc.value.response.status_code == codes.UNPROCESSABLE_ENTITY
102+
103+
104+
def test_target_name_exist(
105+
client: VWS,
106+
high_quality_image: io.BytesIO,
107+
) -> None:
108+
"""
109+
A ``TargetNameExist`` exception is raised after adding two targets with
110+
the same name.
111+
"""
112+
client.add_target(name='x', width=1, image=high_quality_image)
113+
with pytest.raises(TargetNameExist) as exc:
114+
client.add_target(name='x', width=1, image=high_quality_image)
115+
116+
assert exc.value.response.status_code == codes.FORBIDDEN
117+
118+
91119
def test_target_status_processing(
92120
client: VWS,
93121
high_quality_image: io.BytesIO,

0 commit comments

Comments
 (0)