-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_async_cloud_reco_exceptions.py
More file actions
194 lines (170 loc) · 5.83 KB
/
Copy pathtest_async_cloud_reco_exceptions.py
File metadata and controls
194 lines (170 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""Tests for exceptions raised when using the
AsyncCloudRecoService.
"""
import io # noqa: TC003
import json
import uuid
from http import HTTPStatus
import pytest
from mock_vws import CloudQueryFailureResponse, MockVWS
from mock_vws.database import CloudDatabase
from mock_vws.states import States
from vws import AsyncCloudRecoService
from vws.exceptions.base_exceptions import CloudRecoError
from vws.exceptions.cloud_reco_exceptions import (
AuthenticationFailureError,
InactiveProjectError,
MaxNumResultsOutOfRangeError,
)
from vws.exceptions.custom_exceptions import (
RequestEntityTooLargeError,
)
@pytest.mark.asyncio
async def test_too_many_max_results(
*,
async_cloud_reco_client: AsyncCloudRecoService,
high_quality_image: io.BytesIO,
) -> None:
"""A ``MaxNumResultsOutOfRange`` error is raised if the given
``max_num_results`` is out of range.
"""
with pytest.raises(
expected_exception=MaxNumResultsOutOfRangeError,
) as exc:
await async_cloud_reco_client.query(
image=high_quality_image,
max_num_results=51,
)
expected_value = (
"Integer out of range (51) in form data part "
"'max_result'. "
"Accepted range is from 1 to 50 (inclusive)."
)
assert str(object=exc.value) == exc.value.response.text == expected_value
@pytest.mark.asyncio
async def test_image_too_large(
*,
async_cloud_reco_client: AsyncCloudRecoService,
png_too_large: io.BytesIO | io.BufferedRandom,
) -> None:
"""A ``RequestEntityTooLarge`` exception is raised if an
image which is too large is given.
"""
with pytest.raises(
expected_exception=RequestEntityTooLargeError,
) as exc:
await async_cloud_reco_client.query(
image=png_too_large,
)
assert (
exc.value.response.status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE
)
@pytest.mark.asyncio
async def test_authentication_failure(
high_quality_image: io.BytesIO,
) -> None:
"""An ``AuthenticationFailure`` exception is raised when the
client secret key is incorrect.
"""
database = CloudDatabase()
async_cloud_reco_client = AsyncCloudRecoService(
client_access_key=database.client_access_key,
client_secret_key=uuid.uuid4().hex,
)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
with pytest.raises(
expected_exception=AuthenticationFailureError,
) as exc:
await async_cloud_reco_client.query(
image=high_quality_image,
)
assert exc.value.response.status_code == HTTPStatus.UNAUTHORIZED
@pytest.mark.asyncio
async def test_inactive_project(
high_quality_image: io.BytesIO,
) -> None:
"""An ``InactiveProject`` exception is raised when querying
an inactive database.
"""
database = CloudDatabase(state=States.PROJECT_INACTIVE)
with MockVWS() as mock:
mock.add_cloud_database(cloud_database=database)
async_cloud_reco_client = AsyncCloudRecoService(
client_access_key=database.client_access_key,
client_secret_key=database.client_secret_key,
)
with pytest.raises(
expected_exception=InactiveProjectError,
) as exc:
await async_cloud_reco_client.query(
image=high_quality_image,
)
response = exc.value.response
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.tell_position != 0
@pytest.mark.parametrize(
argnames=("body", "headers"),
argvalues=[
("", {"X-Query-Failure": "empty"}),
(
"Arbitrary upstream failure",
{
"Content-Type": "application/json",
"X-Query-Failure": "text",
},
),
],
ids=["empty", "arbitrary-text"],
)
@pytest.mark.asyncio
async def test_non_json_client_error(
*,
high_quality_image: io.BytesIO,
body: str,
headers: dict[str, str],
) -> None:
"""Non-JSON 4xx responses raise a response-carrying error."""
database = CloudDatabase()
failure_response = CloudQueryFailureResponse(
status_code=HTTPStatus.BAD_REQUEST,
headers=headers,
body=body,
)
cloud_reco_client = AsyncCloudRecoService(
client_access_key=database.client_access_key,
client_secret_key=database.client_secret_key,
)
with MockVWS(cloud_query_failure_response=failure_response) as mock:
mock.add_cloud_database(cloud_database=database)
with pytest.raises(expected_exception=CloudRecoError) as exc:
await cloud_reco_client.query(image=high_quality_image)
response = exc.value.response
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.text == body
assert response.content == body.encode()
response_headers = {
key.lower(): value for key, value in response.headers.items()
}
assert response_headers["x-query-failure"] == headers["X-Query-Failure"]
assert response.request_body
@pytest.mark.asyncio
async def test_non_json_success_response(
*,
high_quality_image: io.BytesIO,
) -> None:
"""Malformed successful responses retain the JSON parsing error."""
database = CloudDatabase()
failure_response = CloudQueryFailureResponse(
status_code=HTTPStatus.OK,
headers={"Content-Type": "application/json"},
body="Not JSON",
)
cloud_reco_client = AsyncCloudRecoService(
client_access_key=database.client_access_key,
client_secret_key=database.client_secret_key,
)
with MockVWS(cloud_query_failure_response=failure_response) as mock:
mock.add_cloud_database(cloud_database=database)
with pytest.raises(expected_exception=json.JSONDecodeError):
await cloud_reco_client.query(image=high_quality_image)