|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | + |
| 4 | +from unstructured_client.models import operations |
| 5 | +from unstructured_client import UnstructuredClient, basesdk, utils |
| 6 | + |
| 7 | + |
| 8 | +# Raise one of these from our mock to return to the test code |
| 9 | +class BaseUrlCorrect(Exception): |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +class BaseUrlIncorrect(Exception): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +def get_client_method_with_mock( |
| 18 | + sdk_endpoint_name, |
| 19 | + client_instance, |
| 20 | + mocked_server_url, |
| 21 | + monkeypatch |
| 22 | +): |
| 23 | + """ |
| 24 | + Given an endpoint name, e.g. "general.partition", return a reference |
| 25 | + to that method off of the given client instance. |
| 26 | +
|
| 27 | + The client's _build_request will have the following mock: |
| 28 | + Assert that the provided server_url is passed into _build_request. |
| 29 | + Raise a custom exception to get back to the test. |
| 30 | + """ |
| 31 | + # Mock this to get past param validation |
| 32 | + def mock_unmarshal(*args, **kwargs): |
| 33 | + return {} |
| 34 | + |
| 35 | + monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) |
| 36 | + |
| 37 | + # Assert that the correct base_url makes it to here |
| 38 | + def mock_build_request(*args, base_url, **kwargs): |
| 39 | + if base_url == mocked_server_url: |
| 40 | + raise BaseUrlCorrect |
| 41 | + else: |
| 42 | + raise BaseUrlIncorrect(base_url) |
| 43 | + |
| 44 | + # Find the method from the given string |
| 45 | + class_name, method_name = sdk_endpoint_name.split(".") |
| 46 | + endpoint_class = getattr(client_instance, class_name) |
| 47 | + endpoint_method = getattr(endpoint_class, method_name) |
| 48 | + |
| 49 | + if "async" in method_name: |
| 50 | + monkeypatch.setattr(endpoint_class, "_build_request_async", mock_build_request) |
| 51 | + else: |
| 52 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 53 | + |
| 54 | + return endpoint_method |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize( |
| 58 | + "sdk_endpoint_name", |
| 59 | + [ |
| 60 | + ("general.partition"), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): |
| 64 | + # Test 1 |
| 65 | + # Pass server_url to the client, no path |
| 66 | + s = UnstructuredClient(server_url="http://localhost:8000") |
| 67 | + client_method = get_client_method_with_mock( |
| 68 | + sdk_endpoint_name, |
| 69 | + s, |
| 70 | + "http://localhost:8000", |
| 71 | + monkeypatch |
| 72 | + ) |
| 73 | + |
| 74 | + try: |
| 75 | + client_method(request={}) |
| 76 | + except BaseUrlCorrect: |
| 77 | + pass |
| 78 | + except BaseUrlIncorrect as e: |
| 79 | + pytest.fail(f"server_url was passed to client and ignored, got {e}") |
| 80 | + |
| 81 | + # Test 2 |
| 82 | + # Pass server_url to the client, with path |
| 83 | + s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") |
| 84 | + client_method = get_client_method_with_mock( |
| 85 | + sdk_endpoint_name, |
| 86 | + s, |
| 87 | + "http://localhost:8000", |
| 88 | + monkeypatch |
| 89 | + ) |
| 90 | + |
| 91 | + try: |
| 92 | + client_method(request={}) |
| 93 | + except BaseUrlCorrect: |
| 94 | + pass |
| 95 | + except BaseUrlIncorrect as e: |
| 96 | + pytest.fail(f"server_url was passed to client and was not stripped, got {e}") |
| 97 | + |
| 98 | + # Test 3 |
| 99 | + # Pass server_url to the endpoint, no path |
| 100 | + s = UnstructuredClient() |
| 101 | + client_method = get_client_method_with_mock( |
| 102 | + sdk_endpoint_name, |
| 103 | + s, |
| 104 | + "http://localhost:8000", |
| 105 | + monkeypatch |
| 106 | + ) |
| 107 | + |
| 108 | + try: |
| 109 | + client_method(request={}, server_url="http://localhost:8000") |
| 110 | + except BaseUrlCorrect: |
| 111 | + pass |
| 112 | + except BaseUrlIncorrect as e: |
| 113 | + pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") |
| 114 | + |
| 115 | + # Test 4 |
| 116 | + # Pass server_url to the endpoint, with path |
| 117 | + s = UnstructuredClient() |
| 118 | + client_method = get_client_method_with_mock( |
| 119 | + sdk_endpoint_name, |
| 120 | + s, |
| 121 | + "http://localhost:8000", |
| 122 | + monkeypatch |
| 123 | + ) |
| 124 | + |
| 125 | + try: |
| 126 | + client_method(request={}, server_url="http://localhost:8000/my/endpoint") |
| 127 | + except BaseUrlCorrect: |
| 128 | + pass |
| 129 | + except BaseUrlIncorrect as e: |
| 130 | + pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") |
| 131 | + |
| 132 | + |
| 133 | + # Test 5 |
| 134 | + # No server_url, should take the default |
| 135 | + server_url = "https://api.unstructuredapp.io" if "partition" in sdk_endpoint_name else "https://platform.unstructuredapp.io" |
| 136 | + |
| 137 | + s = UnstructuredClient() |
| 138 | + client_method = get_client_method_with_mock( |
| 139 | + sdk_endpoint_name, |
| 140 | + s, |
| 141 | + server_url, |
| 142 | + monkeypatch |
| 143 | + ) |
| 144 | + |
| 145 | + try: |
| 146 | + client_method(request={}) |
| 147 | + except BaseUrlCorrect: |
| 148 | + pass |
| 149 | + except BaseUrlIncorrect as e: |
| 150 | + pytest.fail(f"Default url not used, got {e}") |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.asyncio |
| 154 | +@pytest.mark.parametrize( |
| 155 | + "sdk_endpoint_name", |
| 156 | + [ |
| 157 | + ("general.partition_async"), |
| 158 | + ], |
| 159 | +) |
| 160 | +async def test_async_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): |
| 161 | + # Test 1 |
| 162 | + # Pass server_url to the client, no path |
| 163 | + s = UnstructuredClient(server_url="http://localhost:8000") |
| 164 | + client_method = get_client_method_with_mock( |
| 165 | + sdk_endpoint_name, |
| 166 | + s, |
| 167 | + "http://localhost:8000", |
| 168 | + monkeypatch |
| 169 | + ) |
| 170 | + |
| 171 | + try: |
| 172 | + await client_method(request={}) |
| 173 | + except BaseUrlCorrect: |
| 174 | + pass |
| 175 | + except BaseUrlIncorrect as e: |
| 176 | + pytest.fail(f"server_url was passed to client and ignored, got {e}") |
| 177 | + |
| 178 | + # Test 2 |
| 179 | + # Pass server_url to the client, with path |
| 180 | + s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") |
| 181 | + client_method = get_client_method_with_mock( |
| 182 | + sdk_endpoint_name, |
| 183 | + s, |
| 184 | + "http://localhost:8000", |
| 185 | + monkeypatch |
| 186 | + ) |
| 187 | + |
| 188 | + try: |
| 189 | + await client_method(request={}) |
| 190 | + except BaseUrlCorrect: |
| 191 | + pass |
| 192 | + except BaseUrlIncorrect as e: |
| 193 | + pytest.fail(f"server_url was passed to client and was not stripped, got {e}") |
| 194 | + |
| 195 | + # Test 3 |
| 196 | + # Pass server_url to the endpoint, no path |
| 197 | + s = UnstructuredClient() |
| 198 | + client_method = get_client_method_with_mock( |
| 199 | + sdk_endpoint_name, |
| 200 | + s, |
| 201 | + "http://localhost:8000", |
| 202 | + monkeypatch |
| 203 | + ) |
| 204 | + |
| 205 | + try: |
| 206 | + await client_method(request={}, server_url="http://localhost:8000") |
| 207 | + except BaseUrlCorrect: |
| 208 | + pass |
| 209 | + except BaseUrlIncorrect as e: |
| 210 | + pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") |
| 211 | + |
| 212 | + # Test 4 |
| 213 | + # Pass server_url to the endpoint, with path |
| 214 | + s = UnstructuredClient() |
| 215 | + client_method = get_client_method_with_mock( |
| 216 | + sdk_endpoint_name, |
| 217 | + s, |
| 218 | + "http://localhost:8000", |
| 219 | + monkeypatch |
| 220 | + ) |
| 221 | + |
| 222 | + try: |
| 223 | + await client_method(request={}, server_url="http://localhost:8000/my/endpoint") |
| 224 | + except BaseUrlCorrect: |
| 225 | + pass |
| 226 | + except BaseUrlIncorrect as e: |
| 227 | + pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") |
| 228 | + |
| 229 | + |
| 230 | + # Test 5 |
| 231 | + # No server_url, should take the default |
| 232 | + server_url = "https://api.unstructuredapp.io" if "partition" in sdk_endpoint_name else "https://platform.unstructuredapp.io" |
| 233 | + |
| 234 | + s = UnstructuredClient() |
| 235 | + client_method = get_client_method_with_mock( |
| 236 | + sdk_endpoint_name, |
| 237 | + s, |
| 238 | + server_url, |
| 239 | + monkeypatch |
| 240 | + ) |
| 241 | + |
| 242 | + try: |
| 243 | + await client_method(request={}) |
| 244 | + except BaseUrlCorrect: |
| 245 | + pass |
| 246 | + except BaseUrlIncorrect as e: |
| 247 | + pytest.fail(f"Default url not used, got {e}") |
0 commit comments