|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from mindee import Client, product |
| 7 | +from mindee.input.sources import PathInput |
| 8 | +from mindee.mindee_http.error import ( |
| 9 | + MindeeHTTPClientException, |
| 10 | + MindeeHTTPServerException, |
| 11 | + handle_error, |
| 12 | +) |
| 13 | +from tests.test_inputs import FILE_TYPES_DIR |
| 14 | +from tests.utils import clear_envvars, dummy_envvars |
| 15 | + |
| 16 | +ERROR_DATA_DIR = Path("./tests/data/errors") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def empty_client(monkeypatch) -> Client: |
| 21 | + clear_envvars(monkeypatch) |
| 22 | + return Client() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def dummy_client(monkeypatch) -> Client: |
| 27 | + dummy_envvars(monkeypatch) |
| 28 | + return Client("dummy") |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def dummy_file(monkeypatch) -> PathInput: |
| 33 | + clear_envvars(monkeypatch) |
| 34 | + c = Client(api_key="dummy-client") |
| 35 | + return c.source_from_path(FILE_TYPES_DIR / "pdf" / "blank.pdf") |
| 36 | + |
| 37 | + |
| 38 | +def test_http_client_error(dummy_client: Client, dummy_file: PathInput): |
| 39 | + with pytest.raises(MindeeHTTPClientException): |
| 40 | + dummy_client.parse(product.InvoiceV4, dummy_file) |
| 41 | + |
| 42 | + |
| 43 | +def test_http_enqueue_client_error(dummy_client: Client, dummy_file: PathInput): |
| 44 | + with pytest.raises(MindeeHTTPClientException): |
| 45 | + dummy_client.enqueue(product.InvoiceV4, dummy_file) |
| 46 | + |
| 47 | + |
| 48 | +def test_http_parse_client_error(dummy_client: Client, dummy_file: PathInput): |
| 49 | + with pytest.raises(MindeeHTTPClientException): |
| 50 | + dummy_client.parse_queued(product.InvoiceV4, "dummy-queue-id") |
| 51 | + |
| 52 | + |
| 53 | +def test_http_enqueue_and_parse_client_error( |
| 54 | + dummy_client: Client, dummy_file: PathInput |
| 55 | +): |
| 56 | + with pytest.raises(MindeeHTTPClientException): |
| 57 | + dummy_client.enqueue_and_parse(product.InvoiceV4, dummy_file) |
| 58 | + |
| 59 | + |
| 60 | +def test_http_400_error(): |
| 61 | + error_ref = open(ERROR_DATA_DIR / "error_400_no_details.json") |
| 62 | + error_obj = json.load(error_ref) |
| 63 | + error_400 = handle_error("dummy-url", error_obj, 400) |
| 64 | + with pytest.raises(MindeeHTTPClientException): |
| 65 | + raise error_400 |
| 66 | + assert error_400.status_code == 400 |
| 67 | + assert error_400.api_code == "SomeCode" |
| 68 | + assert error_400.api_message == "Some scary message here" |
| 69 | + assert error_400.api_details is None |
| 70 | + |
| 71 | + |
| 72 | +def test_http_401_error(): |
| 73 | + error_ref = open(ERROR_DATA_DIR / "error_401_invalid_token.json") |
| 74 | + error_obj = json.load(error_ref) |
| 75 | + error_401 = handle_error("dummy-url", error_obj, 401) |
| 76 | + with pytest.raises(MindeeHTTPClientException): |
| 77 | + raise error_401 |
| 78 | + assert error_401.status_code == 401 |
| 79 | + assert error_401.api_code == "Unauthorized" |
| 80 | + assert error_401.api_message == "Authorization required" |
| 81 | + assert error_401.api_details == "Invalid token provided" |
| 82 | + |
| 83 | + |
| 84 | +def test_http_429_error(): |
| 85 | + error_ref = open(ERROR_DATA_DIR / "error_429_too_many_requests.json") |
| 86 | + error_obj = json.load(error_ref) |
| 87 | + error_429 = handle_error("dummy-url", error_obj, 429) |
| 88 | + with pytest.raises(MindeeHTTPClientException): |
| 89 | + raise error_429 |
| 90 | + assert error_429.status_code == 429 |
| 91 | + assert error_429.api_code == "TooManyRequests" |
| 92 | + assert error_429.api_message == "Too many requests" |
| 93 | + assert error_429.api_details == "Too Many Requests." |
| 94 | + |
| 95 | + |
| 96 | +def test_http_500_error(): |
| 97 | + error_ref = open(ERROR_DATA_DIR / "error_500_inference_fail.json") |
| 98 | + error_obj = json.load(error_ref) |
| 99 | + error_500 = handle_error("dummy-url", error_obj, 500) |
| 100 | + with pytest.raises(MindeeHTTPServerException): |
| 101 | + raise error_500 |
| 102 | + assert error_500.status_code == 500 |
| 103 | + assert error_500.api_code == "failure" |
| 104 | + assert error_500.api_message == "Inference failed" |
| 105 | + assert error_500.api_details == "Can not run prediction: " |
| 106 | + |
| 107 | + |
| 108 | +def test_http_500_html_error(): |
| 109 | + error_ref_contents = open(ERROR_DATA_DIR / "error_50x.html").read() |
| 110 | + error_500 = handle_error("dummy-url", error_ref_contents, 500) |
| 111 | + with pytest.raises(MindeeHTTPServerException): |
| 112 | + raise error_500 |
| 113 | + assert error_500.status_code == 500 |
| 114 | + assert error_500.api_code == "UnknownError" |
| 115 | + assert error_500.api_message == "Server sent back an unexpected reply." |
| 116 | + assert error_500.api_details == error_ref_contents |
0 commit comments