Skip to content

Commit bbdab24

Browse files
committed
Start of consolidating tests for exceptions
1 parent 13196bc commit bbdab24

2 files changed

Lines changed: 74 additions & 96 deletions

File tree

tests/test_add_target.py

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@
99
from requests import codes
1010

1111
from vws import VWS
12-
from vws.exceptions import (
13-
BadImage,
14-
Fail,
15-
MetadataTooLarge,
16-
ProjectInactive,
17-
TargetNameExist,
18-
)
12+
from vws.exceptions import MetadataTooLarge, ProjectInactive
1913

2014

2115
class TestSuccess:
@@ -57,70 +51,6 @@ def test_add_two_targets(
5751
client.add_target(name='a', width=1, image=high_quality_image)
5852

5953

60-
class TestName:
61-
"""
62-
Tests for the ``name`` parameter to ``add_target``.
63-
"""
64-
65-
def test_add_two_targets_same_name(
66-
self,
67-
client: VWS,
68-
high_quality_image: io.BytesIO,
69-
) -> None:
70-
"""
71-
A ``TargetNameExist`` exception is raised after adding two targets with
72-
the same name.
73-
"""
74-
client.add_target(name='x', width=1, image=high_quality_image)
75-
with pytest.raises(TargetNameExist) as exc:
76-
client.add_target(name='x', width=1, image=high_quality_image)
77-
78-
assert exc.value.response.status_code == codes.FORBIDDEN
79-
80-
81-
class TestAuthentication:
82-
"""
83-
Tests for authentication issues.
84-
"""
85-
86-
def test_authentication_error(
87-
self,
88-
high_quality_image: io.BytesIO,
89-
) -> None:
90-
"""
91-
A ``Fail`` exception is raised when there are authentication issues.
92-
"""
93-
with MockVWS() as mock:
94-
client = VWS(
95-
server_access_key='a',
96-
server_secret_key=mock.server_secret_key,
97-
)
98-
99-
with pytest.raises(Fail) as exc:
100-
client.add_target(
101-
name='x',
102-
width=1,
103-
image=high_quality_image,
104-
)
105-
106-
exception = exc.value
107-
assert exception.response.status_code == codes.BAD_REQUEST
108-
109-
110-
class TestImage:
111-
"""
112-
Tests for the ``image`` parameter to ``add_target``.
113-
"""
114-
115-
def test_not_an_image(self, client: VWS) -> None:
116-
"""
117-
A ``BadImage`` exception is raised when a non-image is given.
118-
"""
119-
not_an_image = io.BytesIO(b'Not an image')
120-
with pytest.raises(BadImage):
121-
client.add_target(name='x', width=1, image=not_an_image)
122-
123-
12454
class TestCustomBaseURL:
12555
"""
12656
Tests for adding images to databases under custom VWS URLs.
@@ -146,30 +76,6 @@ def test_custom_base_url(self, high_quality_image: io.BytesIO) -> None:
14676
)
14777

14878

149-
class TestInactiveProject:
150-
"""
151-
Tests for using an inactive project.
152-
"""
153-
154-
def test_inactive_project(self, high_quality_image: io.BytesIO) -> None:
155-
"""
156-
A ``ProjectInactive`` exception is raised if adding a target to an
157-
inactive database.
158-
"""
159-
with MockVWS(state=States.PROJECT_INACTIVE) as mock:
160-
client = VWS(
161-
server_access_key=mock.server_access_key,
162-
server_secret_key=mock.server_secret_key,
163-
)
164-
165-
with pytest.raises(ProjectInactive):
166-
client.add_target(
167-
name='x',
168-
width=1,
169-
image=high_quality_image,
170-
)
171-
172-
17379
class TestApplicationMetadata:
17480
"""
17581
Tests for the ``application_metadata`` parameter to ``add_target``.

tests/test_exceptions.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
import random
77

88
import pytest
9+
from mock_vws import MockVWS, States
910
from PIL import Image
1011
from requests import codes
1112

1213
from vws import VWS
13-
from vws.exceptions import ImageTooLarge, UnknownTarget
14+
from vws.exceptions import (
15+
BadImage,
16+
Fail,
17+
ImageTooLarge,
18+
TargetNameExist,
19+
ProjectInactive,
20+
UnknownTarget,
21+
)
1422

1523

1624
def _make_image_file(
@@ -65,6 +73,7 @@ def test_image_too_large(client: VWS) -> None:
6573

6674
assert exc.value.response.status_code == codes.UNPROCESSABLE_ENTITY
6775

76+
6877
def test_invalid_given_id(client: VWS) -> None:
6978
"""
7079
Giving an invalid ID to a helper which requires a target ID to be given
@@ -80,3 +89,66 @@ def test_request_quota_reached() -> None:
8089
See https://github.com/adamtheturtle/vws-python/issues/822 for writing
8190
this test.
8291
"""
92+
93+
94+
def test_fail(high_quality_image: io.BytesIO) -> None:
95+
"""
96+
A ``Fail`` exception is raised when there are authentication issues.
97+
"""
98+
with MockVWS() as mock:
99+
client = VWS(
100+
server_access_key='a',
101+
server_secret_key=mock.server_secret_key,
102+
)
103+
104+
with pytest.raises(Fail) as exc:
105+
client.add_target(
106+
name='x',
107+
width=1,
108+
image=high_quality_image,
109+
)
110+
111+
exception = exc.value
112+
assert exception.response.status_code == codes.BAD_REQUEST
113+
114+
115+
def test_bad_image(client: VWS) -> None:
116+
"""
117+
A ``BadImage`` exception is raised when a non-image is given.
118+
"""
119+
not_an_image = io.BytesIO(b'Not an image')
120+
with pytest.raises(BadImage):
121+
client.add_target(name='x', width=1, image=not_an_image)
122+
123+
124+
def test_target_name_exist(
125+
client: VWS,
126+
high_quality_image: io.BytesIO,
127+
) -> None:
128+
"""
129+
A ``TargetNameExist`` exception is raised after adding two targets with
130+
the same name.
131+
"""
132+
client.add_target(name='x', width=1, image=high_quality_image)
133+
with pytest.raises(TargetNameExist) as exc:
134+
client.add_target(name='x', width=1, image=high_quality_image)
135+
136+
assert exc.value.response.status_code == codes.FORBIDDEN
137+
138+
def test_project_inactive(client: VWS, high_quality_image: io.BytesIO) -> None:
139+
"""
140+
A ``ProjectInactive`` exception is raised if adding a target to an
141+
inactive database.
142+
"""
143+
with MockVWS(state=States.PROJECT_INACTIVE) as mock:
144+
client = VWS(
145+
server_access_key=mock.server_access_key,
146+
server_secret_key=mock.server_secret_key,
147+
)
148+
149+
with pytest.raises(ProjectInactive):
150+
client.add_target(
151+
name='x',
152+
width=1,
153+
image=high_quality_image,
154+
)

0 commit comments

Comments
 (0)