|
| 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 from our mock so we can get out of the client code |
| 9 | +class StopClientException(Exception): |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "method_name", |
| 15 | + [ |
| 16 | + ("general.partition"), |
| 17 | + ("destinations.create_destination"), |
| 18 | + ("destinations.delete_destination"), |
| 19 | + ("destinations.get_destination"), |
| 20 | + ("destinations.list_destinations"), |
| 21 | + ("destinations.update_destination"), |
| 22 | + ("jobs.cancel_job"), |
| 23 | + ("jobs.get_job"), |
| 24 | + ("jobs.list_jobs"), |
| 25 | + ("sources.create_source"), |
| 26 | + ("sources.delete_source"), |
| 27 | + ("sources.get_source"), |
| 28 | + ("sources.list_sources"), |
| 29 | + ("sources.update_source"), |
| 30 | + ("workflows.create_workflow"), |
| 31 | + ("workflows.delete_workflow"), |
| 32 | + ("workflows.get_workflow"), |
| 33 | + ("workflows.list_workflows"), |
| 34 | + ("workflows.run_workflow"), |
| 35 | + ("workflows.update_workflow"), |
| 36 | + ], |
| 37 | +) |
| 38 | +def test_endpoint_uses_correct_url(monkeypatch, method_name): |
| 39 | + # Mock this to get past param validation |
| 40 | + def mock_unmarshal(*args, **kwargs): |
| 41 | + return {} |
| 42 | + |
| 43 | + monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) |
| 44 | + |
| 45 | + print(method_name) |
| 46 | + # Use these in the mock |
| 47 | + server_url = "http://localhost:8000" |
| 48 | + assertion_message = "" |
| 49 | + |
| 50 | + # Assert that the correct base_url makes it to here |
| 51 | + def mock_build_request(*args, base_url, **kwargs): |
| 52 | + nonlocal assertion_message |
| 53 | + nonlocal server_url |
| 54 | + |
| 55 | + assert base_url == server_url, assertion_message |
| 56 | + raise StopClientException # We're good, let's bail |
| 57 | + |
| 58 | + endpoint_class_name, endpoint_method_name = method_name.split(".") |
| 59 | + |
| 60 | + # Test 1 |
| 61 | + # Pass server_url to the client, no path |
| 62 | + with pytest.raises(StopClientException): |
| 63 | + assertion_message = "server_url was passed to client and ignored" |
| 64 | + s = UnstructuredClient(server_url="http://localhost:8000") |
| 65 | + |
| 66 | + endpoint_class = getattr(s, endpoint_class_name) |
| 67 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 68 | + |
| 69 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 70 | + endpoint_method(request={}) |
| 71 | + |
| 72 | + # Test 2 |
| 73 | + # Pass server_url to the client, with path |
| 74 | + with pytest.raises(StopClientException): |
| 75 | + assertion_message = "server_url was passed to client and was not stripped" |
| 76 | + s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") |
| 77 | + |
| 78 | + endpoint_class = getattr(s, endpoint_class_name) |
| 79 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 80 | + |
| 81 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 82 | + endpoint_method(request={}) |
| 83 | + |
| 84 | + # Test 3 |
| 85 | + # Pass server_url to the endpoint, no path |
| 86 | + with pytest.raises(StopClientException): |
| 87 | + assertion_message = "server_url was passed to endpoint and ignored" |
| 88 | + s = UnstructuredClient() |
| 89 | + |
| 90 | + endpoint_class = getattr(s, endpoint_class_name) |
| 91 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 92 | + |
| 93 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 94 | + endpoint_method( |
| 95 | + request={}, |
| 96 | + server_url="http://localhost:8000", |
| 97 | + ) |
| 98 | + |
| 99 | + # Test 4 |
| 100 | + # Pass server_url to the endpoint, with path |
| 101 | + with pytest.raises(StopClientException): |
| 102 | + assertion_message = "server_url was passed to endpoint and was not stripped" |
| 103 | + s = UnstructuredClient() |
| 104 | + |
| 105 | + endpoint_class = getattr(s, endpoint_class_name) |
| 106 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 107 | + |
| 108 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 109 | + endpoint_method( |
| 110 | + request={}, |
| 111 | + server_url="http://localhost:8000/my/endpoint", |
| 112 | + ) |
| 113 | + |
| 114 | + # Test 5 |
| 115 | + # No server_url, should take the default |
| 116 | + with pytest.raises(StopClientException): |
| 117 | + assertion_message = "Default url was not used" |
| 118 | + server_url = "https://api.unstructuredapp.io" if method_name == "general.partition" else "https://platform.unstructuredapp.io" |
| 119 | + s = UnstructuredClient() |
| 120 | + |
| 121 | + endpoint_class = getattr(s, endpoint_class_name) |
| 122 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 123 | + |
| 124 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 125 | + endpoint_method( |
| 126 | + request={}, |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.asyncio |
| 131 | +@pytest.mark.parametrize( |
| 132 | + "method_name", |
| 133 | + [ |
| 134 | + ("general.partition_async"), |
| 135 | + ("destinations.create_destination_async"), |
| 136 | + ("destinations.delete_destination_async"), |
| 137 | + ("destinations.get_destination_async"), |
| 138 | + ("destinations.list_destinations_async"), |
| 139 | + ("destinations.update_destination_async"), |
| 140 | + ("jobs.cancel_job_async"), |
| 141 | + ("jobs.get_job_async"), |
| 142 | + ("jobs.list_jobs_async"), |
| 143 | + ("sources.create_source_async"), |
| 144 | + ("sources.delete_source_async"), |
| 145 | + ("sources.get_source_async"), |
| 146 | + ("sources.list_sources_async"), |
| 147 | + ("sources.update_source_async"), |
| 148 | + ("workflows.create_workflow_async"), |
| 149 | + ("workflows.delete_workflow_async"), |
| 150 | + ("workflows.get_workflow_async"), |
| 151 | + ("workflows.list_workflows_async"), |
| 152 | + ("workflows.run_workflow_async"), |
| 153 | + ("workflows.update_workflow_async"), |
| 154 | + ], |
| 155 | +) |
| 156 | +async def test_async_endpoint_uses_correct_url(monkeypatch, method_name): |
| 157 | + # Mock this to get past param validation |
| 158 | + def mock_unmarshal(*args, **kwargs): |
| 159 | + return {} |
| 160 | + |
| 161 | + monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) |
| 162 | + |
| 163 | + print(method_name) |
| 164 | + # Use these in the mock |
| 165 | + server_url = "http://localhost:8000" |
| 166 | + assertion_message = "" |
| 167 | + |
| 168 | + # Assert that the correct base_url makes it to here |
| 169 | + def mock_build_request(*args, base_url, **kwargs): |
| 170 | + nonlocal assertion_message |
| 171 | + nonlocal server_url |
| 172 | + |
| 173 | + assert base_url == server_url, assertion_message |
| 174 | + raise StopClientException # We're good, let's bail |
| 175 | + |
| 176 | + endpoint_class_name, endpoint_method_name = method_name.split(".") |
| 177 | + |
| 178 | + # Test 1 |
| 179 | + # Pass server_url to the client, no path |
| 180 | + with pytest.raises(StopClientException): |
| 181 | + assertion_message = "server_url was passed to client and ignored" |
| 182 | + s = UnstructuredClient(server_url="http://localhost:8000") |
| 183 | + |
| 184 | + endpoint_class = getattr(s, endpoint_class_name) |
| 185 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 186 | + |
| 187 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 188 | + await endpoint_method(request={}) |
| 189 | + |
| 190 | + # Test 2 |
| 191 | + # Pass server_url to the client, with path |
| 192 | + with pytest.raises(StopClientException): |
| 193 | + assertion_message = "server_url was passed to client and was not stripped" |
| 194 | + s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") |
| 195 | + |
| 196 | + endpoint_class = getattr(s, endpoint_class_name) |
| 197 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 198 | + |
| 199 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 200 | + await endpoint_method(request={}) |
| 201 | + |
| 202 | + # Test 3 |
| 203 | + # Pass server_url to the endpoint, no path |
| 204 | + with pytest.raises(StopClientException): |
| 205 | + assertion_message = "server_url was passed to endpoint and ignored" |
| 206 | + s = UnstructuredClient() |
| 207 | + |
| 208 | + endpoint_class = getattr(s, endpoint_class_name) |
| 209 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 210 | + |
| 211 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 212 | + await endpoint_method( |
| 213 | + request={}, |
| 214 | + server_url="http://localhost:8000", |
| 215 | + ) |
| 216 | + |
| 217 | + # Test 4 |
| 218 | + # Pass server_url to the endpoint, with path |
| 219 | + with pytest.raises(StopClientException): |
| 220 | + assertion_message = "server_url was passed to endpoint and was not stripped" |
| 221 | + s = UnstructuredClient() |
| 222 | + |
| 223 | + endpoint_class = getattr(s, endpoint_class_name) |
| 224 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 225 | + |
| 226 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 227 | + await endpoint_method( |
| 228 | + request={}, |
| 229 | + server_url="http://localhost:8000/my/endpoint", |
| 230 | + ) |
| 231 | + |
| 232 | + # Test 5 |
| 233 | + # No server_url, should take the default |
| 234 | + with pytest.raises(StopClientException): |
| 235 | + assertion_message = "Default url was not used" |
| 236 | + server_url = "https://api.unstructuredapp.io" if method_name == "general.partition" else "https://platform.unstructuredapp.io" |
| 237 | + s = UnstructuredClient() |
| 238 | + |
| 239 | + endpoint_class = getattr(s, endpoint_class_name) |
| 240 | + endpoint_method = getattr(endpoint_class, endpoint_method_name) |
| 241 | + |
| 242 | + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) |
| 243 | + await endpoint_method( |
| 244 | + request={}, |
| 245 | + ) |
0 commit comments