|
1 | 1 | """Tests for VWS exceptions.""" |
2 | 2 |
|
3 | 3 | import io |
| 4 | +import json |
4 | 5 | import uuid |
5 | 6 | from http import HTTPStatus |
6 | 7 |
|
|
41 | 42 | from vws.vumark_accept import VuMarkAccept |
42 | 43 |
|
43 | 44 |
|
| 45 | +class _RequestQuotaReachedTransport: |
| 46 | + """Transport that always returns ``RequestQuotaReached``. |
| 47 | +
|
| 48 | + ``vws-python-mock`` does not yet simulate this result code (see |
| 49 | + https://github.com/VWS-Python/vws-python-mock/issues/53), so tests |
| 50 | + use a custom transport instead of ``MockVWS``. |
| 51 | + """ |
| 52 | + |
| 53 | + def close(self) -> None: |
| 54 | + """Close the transport.""" |
| 55 | + |
| 56 | + def __call__( |
| 57 | + self, |
| 58 | + *, |
| 59 | + method: str, |
| 60 | + url: str, |
| 61 | + headers: dict[str, str], |
| 62 | + data: bytes, |
| 63 | + request_timeout: float | tuple[float, float], |
| 64 | + ) -> Response: |
| 65 | + """Return a ``RequestQuotaReached`` response.""" |
| 66 | + del method, headers, request_timeout |
| 67 | + body = { |
| 68 | + "transaction_id": uuid.uuid4().hex, |
| 69 | + "result_code": "RequestQuotaReached", |
| 70 | + } |
| 71 | + text = json.dumps(body) |
| 72 | + return Response( |
| 73 | + text=text, |
| 74 | + url=url, |
| 75 | + status_code=HTTPStatus.FORBIDDEN, |
| 76 | + headers={"Content-Type": "application/json"}, |
| 77 | + request_body=data, |
| 78 | + tell_position=0, |
| 79 | + content=text.encode(), |
| 80 | + ) |
| 81 | + |
| 82 | + |
44 | 83 | def test_image_too_large( |
45 | 84 | *, |
46 | 85 | vws_client: VWS, |
@@ -106,9 +145,19 @@ def test_add_bad_name( |
106 | 145 |
|
107 | 146 | def test_request_quota_reached() -> None: |
108 | 147 | """ |
109 | | - See https://github.com/VWS-Python/vws-python/issues/822 for writing |
110 | | - this test. |
| 148 | + A ``RequestQuotaReached`` exception is raised when Vuforia reports |
| 149 | + that the request quota has been reached. |
111 | 150 | """ |
| 151 | + vws_client = VWS( |
| 152 | + server_access_key=uuid.uuid4().hex, |
| 153 | + server_secret_key=uuid.uuid4().hex, |
| 154 | + transport=_RequestQuotaReachedTransport(), |
| 155 | + ) |
| 156 | + |
| 157 | + with pytest.raises(expected_exception=RequestQuotaReachedError) as exc: |
| 158 | + vws_client.list_targets() |
| 159 | + |
| 160 | + assert exc.value.response.status_code == HTTPStatus.FORBIDDEN |
112 | 161 |
|
113 | 162 |
|
114 | 163 | def test_fail(high_quality_image: io.BytesIO) -> None: |
|
0 commit comments