|
4 | 4 |
|
5 | 5 | import io
|
6 | 6 | from http import HTTPStatus
|
| 7 | +from typing import List, Type, Union |
7 | 8 |
|
8 | 9 | import pytest
|
| 10 | +import requests |
9 | 11 | from freezegun import freeze_time
|
10 | 12 | from mock_vws import MockVWS
|
11 | 13 | from mock_vws.database import VuforiaDatabase
|
|
15 | 17 | from vws.exceptions import (
|
16 | 18 | AuthenticationFailure,
|
17 | 19 | BadImage,
|
| 20 | + CloudRecoException, |
| 21 | + ConnectionErrorPossiblyImageTooLarge, |
| 22 | + DateRangeError, |
18 | 23 | Fail,
|
19 | 24 | ImageTooLarge,
|
20 | 25 | MatchProcessing,
|
| 26 | + MaxNumResultsOutOfRange, |
21 | 27 | MetadataTooLarge,
|
| 28 | + ProjectHasNoAPIAccess, |
22 | 29 | ProjectInactive,
|
| 30 | + ProjectSuspended, |
| 31 | + RequestQuotaReached, |
23 | 32 | RequestTimeTooSkewed,
|
24 | 33 | TargetNameExist,
|
| 34 | + TargetProcessingTimeout, |
| 35 | + TargetQuotaReached, |
25 | 36 | TargetStatusNotSuccess,
|
26 | 37 | TargetStatusProcessing,
|
27 | 38 | UnknownTarget,
|
28 | 39 | UnknownVWSErrorPossiblyBadName,
|
| 40 | + VWSException, |
29 | 41 | )
|
30 | 42 |
|
31 | 43 |
|
@@ -333,3 +345,71 @@ def test_match_processing(
|
333 | 345 | with pytest.raises(MatchProcessing) as exc:
|
334 | 346 | cloud_reco_client.query(image=high_quality_image)
|
335 | 347 | assert exc.value.response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
|
| 348 | + |
| 349 | + |
| 350 | +def test_vwsexception_inheritance() -> None: |
| 351 | + """ |
| 352 | + VWS-related exceptions should inherit from VWSException. |
| 353 | + """ |
| 354 | + subclasses = [ |
| 355 | + AuthenticationFailure, |
| 356 | + BadImage, |
| 357 | + DateRangeError, |
| 358 | + Fail, |
| 359 | + ImageTooLarge, |
| 360 | + MetadataTooLarge, |
| 361 | + ProjectInactive, |
| 362 | + ProjectHasNoAPIAccess, |
| 363 | + ProjectSuspended, |
| 364 | + RequestQuotaReached, |
| 365 | + RequestTimeTooSkewed, |
| 366 | + TargetNameExist, |
| 367 | + TargetQuotaReached, |
| 368 | + TargetStatusNotSuccess, |
| 369 | + TargetStatusProcessing, |
| 370 | + UnknownTarget, |
| 371 | + UnknownVWSErrorPossiblyBadName, |
| 372 | + ] |
| 373 | + for subclass in subclasses: |
| 374 | + assert issubclass(subclass, VWSException) |
| 375 | + |
| 376 | + |
| 377 | +def test_cloudrecoexception_inheritance() -> None: |
| 378 | + """ |
| 379 | + CloudRecoService-specific exceptions should inherit from |
| 380 | + CloudRecoException. |
| 381 | + """ |
| 382 | + subclasses = [ |
| 383 | + MatchProcessing, |
| 384 | + MaxNumResultsOutOfRange, |
| 385 | + ] |
| 386 | + for subclass in subclasses: |
| 387 | + assert issubclass(subclass, CloudRecoException) |
| 388 | + |
| 389 | + |
| 390 | +def test_others_inheritance() -> None: |
| 391 | + """ |
| 392 | + Make sure other exceptions are inherited from their expected super-classes. |
| 393 | + """ |
| 394 | + assert issubclass( |
| 395 | + ConnectionErrorPossiblyImageTooLarge, |
| 396 | + requests.ConnectionError, |
| 397 | + ) |
| 398 | + assert issubclass(TargetProcessingTimeout, Exception) |
| 399 | + |
| 400 | + |
| 401 | +def test_base_exceptions_have_response_property_and_text_str() -> None: |
| 402 | + """ |
| 403 | + A VWSException or CloudRecoException should have a response property |
| 404 | + and string representation with the text property of the response. |
| 405 | + """ |
| 406 | + base_classes: List[Union[Type[CloudRecoException], Type[VWSException]]] = [ |
| 407 | + CloudRecoException, |
| 408 | + VWSException, |
| 409 | + ] |
| 410 | + for base in base_classes: |
| 411 | + response = requests.Response() |
| 412 | + setattr(response, '._content', bytes(f'Test{base.__name__}', 'ascii')) |
| 413 | + exception = base(response=response) |
| 414 | + assert exception.response == response |
| 415 | + assert str(exception) == response.text |
0 commit comments