|
| 1 | +import pytest |
| 2 | + |
| 3 | +from unstructured_client import UnstructuredClient |
| 4 | +from unstructured_client.models import shared |
| 5 | +from unstructured_client.models.errors import SDKError |
| 6 | + |
| 7 | + |
| 8 | +FAKE_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "server_url", |
| 13 | + [ |
| 14 | + # -- well-formed url -- |
| 15 | + "https://unstructured-000mock.api.unstructuredapp.io", |
| 16 | + # -- common malformed urls -- |
| 17 | + "unstructured-000mock.api.unstructuredapp.io", |
| 18 | + "http://unstructured-000mock.api.unstructuredapp.io/general/v0/general", |
| 19 | + "https://unstructured-000mock.api.unstructuredapp.io/general/v0/general", |
| 20 | + "unstructured-000mock.api.unstructuredapp.io/general/v0/general", |
| 21 | + ], |
| 22 | +) |
| 23 | +def test_clean_server_url_fixes_malformed_paid_api_url(server_url: str): |
| 24 | + client = UnstructuredClient( |
| 25 | + server_url=server_url, |
| 26 | + api_key_auth=FAKE_KEY, |
| 27 | + ) |
| 28 | + assert ( |
| 29 | + client.general.sdk_configuration.server_url |
| 30 | + == "https://unstructured-000mock.api.unstructuredapp.io" |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + "server_url", |
| 36 | + [ |
| 37 | + # -- well-formed url -- |
| 38 | + "http://localhost:8000", |
| 39 | + # -- common malformed urls -- |
| 40 | + "localhost:8000", |
| 41 | + "localhost:8000/general/v0/general", |
| 42 | + "http://localhost:8000/general/v0/general", |
| 43 | + ], |
| 44 | +) |
| 45 | +def test_clean_server_url_fixes_malformed_localhost_url(server_url: str): |
| 46 | + client = UnstructuredClient( |
| 47 | + server_url=server_url, |
| 48 | + api_key_auth=FAKE_KEY, |
| 49 | + ) |
| 50 | + assert client.general.sdk_configuration.server_url == "http://localhost:8000" |
| 51 | + |
| 52 | + |
| 53 | +def test_clean_server_url_returns_empty_string_given_empty_string(): |
| 54 | + client = UnstructuredClient( server_url="", api_key_auth=FAKE_KEY) |
| 55 | + assert client.general.sdk_configuration.server_url == "" |
| 56 | + |
| 57 | + |
| 58 | +def test_clean_server_url_returns_None_given_no_server_url(): |
| 59 | + client = UnstructuredClient( |
| 60 | + api_key_auth=FAKE_KEY, |
| 61 | + ) |
| 62 | + assert client.general.sdk_configuration.server_url == None |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "server_url", |
| 67 | + [ |
| 68 | + # -- well-formed url -- |
| 69 | + "https://unstructured-000mock.api.unstructuredapp.io", |
| 70 | + # -- malformed url -- |
| 71 | + "unstructured-000mock.api.unstructuredapp.io/general/v0/general", |
| 72 | + ], |
| 73 | +) |
| 74 | +def test_clean_server_url_fixes_malformed_urls_with_positional_arguments( |
| 75 | + server_url: str, |
| 76 | +): |
| 77 | + client = UnstructuredClient( |
| 78 | + FAKE_KEY, |
| 79 | + "", |
| 80 | + server_url, |
| 81 | + ) |
| 82 | + assert ( |
| 83 | + client.general.sdk_configuration.server_url |
| 84 | + == "https://unstructured-000mock.api.unstructuredapp.io" |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def test_suggest_defining_url_issues_a_warning_on_a_401(): |
| 89 | + client = UnstructuredClient( |
| 90 | + api_key_auth=FAKE_KEY, |
| 91 | + ) |
| 92 | + |
| 93 | + filename = "_sample_docs/layout-parser-paper-fast.pdf" |
| 94 | + |
| 95 | + with open(filename, "rb") as f: |
| 96 | + files = shared.Files( |
| 97 | + content=f.read(), |
| 98 | + file_name=filename, |
| 99 | + ) |
| 100 | + |
| 101 | + req = shared.PartitionParameters( |
| 102 | + files=files, |
| 103 | + ) |
| 104 | + |
| 105 | + with pytest.raises(SDKError, match="API error occurred: Status 401"): |
| 106 | + with pytest.warns( |
| 107 | + UserWarning, |
| 108 | + match="If intending to use the paid API, please define `server_url` in your request.", |
| 109 | + ): |
| 110 | + client.general.partition(req) |
0 commit comments