|
1 | | -import httpx |
2 | 1 | import pytest |
3 | | - |
4 | | -from unstructured_client.models import operations |
5 | | -from unstructured_client import UnstructuredClient, basesdk, utils |
| 2 | +from dataclasses import dataclass |
| 3 | +from unstructured_client import UnstructuredClient, utils |
6 | 4 |
|
7 | 5 |
|
8 | 6 | # Raise one of these from our mock to return to the test code |
@@ -53,195 +51,107 @@ def mock_build_request(*args, base_url, **kwargs): |
53 | 51 |
|
54 | 52 | return endpoint_method |
55 | 53 |
|
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 | | - |
| 54 | +@dataclass |
| 55 | +class URLTestCase: |
| 56 | + description: str |
| 57 | + sdk_endpoint_name: str |
| 58 | + # url when you init the client (global for all endpoints) |
| 59 | + client_url: str | None |
| 60 | + # url when you init the SDK endpoint (vary per endpoint) |
| 61 | + endpoint_url: str | None |
| 62 | + # expected url when actually making the HTTP request in build_request |
| 63 | + expected_url: str |
152 | 64 |
|
153 | 65 | @pytest.mark.asyncio |
154 | 66 | @pytest.mark.parametrize( |
155 | | - "sdk_endpoint_name", |
| 67 | + "case", |
156 | 68 | [ |
157 | | - ("general.partition_async"), |
158 | | - ], |
| 69 | + URLTestCase( |
| 70 | + description="non UNST domain client-level URL, no path", |
| 71 | + sdk_endpoint_name="general.partition_async", |
| 72 | + client_url="http://localhost:8000", |
| 73 | + endpoint_url=None, |
| 74 | + expected_url="http://localhost:8000" |
| 75 | + ), |
| 76 | + URLTestCase( |
| 77 | + description="non UNST domain client-level URL, with path", |
| 78 | + sdk_endpoint_name="general.partition_async", |
| 79 | + client_url="http://localhost:8000/my/endpoint", |
| 80 | + endpoint_url=None, |
| 81 | + expected_url="http://localhost:8000/my/endpoint" |
| 82 | + ), |
| 83 | + URLTestCase( |
| 84 | + description="non UNST domain endpoint-level URL, no path", |
| 85 | + sdk_endpoint_name="general.partition_async", |
| 86 | + client_url=None, |
| 87 | + endpoint_url="http://localhost:8000", |
| 88 | + expected_url="http://localhost:8000" |
| 89 | + ), |
| 90 | + URLTestCase( |
| 91 | + description="non UNST domain endpoint-level URL, with path", |
| 92 | + sdk_endpoint_name="general.partition_async", |
| 93 | + client_url=None, |
| 94 | + endpoint_url="http://localhost:8000/my/endpoint", |
| 95 | + expected_url="http://localhost:8000/my/endpoint" |
| 96 | + ), |
| 97 | + URLTestCase( |
| 98 | + description="UNST domain client-level URL, no path", |
| 99 | + sdk_endpoint_name="general.partition_async", |
| 100 | + client_url="https://unstructured-000mock.api.unstructuredapp.io", |
| 101 | + endpoint_url=None, |
| 102 | + expected_url="https://unstructured-000mock.api.unstructuredapp.io" |
| 103 | + ), |
| 104 | + URLTestCase( |
| 105 | + description="UNST domain client-level URL, with path", |
| 106 | + sdk_endpoint_name="general.partition_async", |
| 107 | + client_url="https://unstructured-000mock.api.unstructuredapp.io/my/endpoint/", |
| 108 | + endpoint_url=None, |
| 109 | + expected_url="https://unstructured-000mock.api.unstructuredapp.io" |
| 110 | + ), |
| 111 | + URLTestCase( |
| 112 | + description="UNST domain endpoint-level URL, no path", |
| 113 | + sdk_endpoint_name="general.partition_async", |
| 114 | + client_url=None, |
| 115 | + endpoint_url="https://unstructured-000mock.api.unstructuredapp.io", |
| 116 | + expected_url="https://unstructured-000mock.api.unstructuredapp.io" |
| 117 | + ), |
| 118 | + URLTestCase( |
| 119 | + description="UNST domain endpoint-level URL, with path", |
| 120 | + sdk_endpoint_name="general.partition_async", |
| 121 | + client_url=None, |
| 122 | + endpoint_url="https://unstructured-000mock.api.unstructuredapp.io/my/endpoint/", |
| 123 | + expected_url="https://unstructured-000mock.api.unstructuredapp.io" |
| 124 | + ), |
| 125 | + URLTestCase( |
| 126 | + description="default URL fallback", |
| 127 | + sdk_endpoint_name="general.partition_async", |
| 128 | + client_url=None, |
| 129 | + endpoint_url=None, |
| 130 | + expected_url="https://api.unstructuredapp.io" |
| 131 | + ), |
| 132 | + ] |
159 | 133 | ) |
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" |
| 134 | +async def test_async_endpoint_uses_correct_url(monkeypatch, case: URLTestCase): |
| 135 | + if case.client_url: |
| 136 | + s = UnstructuredClient(server_url=case.client_url) |
| 137 | + else: |
| 138 | + s = UnstructuredClient() |
233 | 139 |
|
234 | | - s = UnstructuredClient() |
235 | 140 | client_method = get_client_method_with_mock( |
236 | | - sdk_endpoint_name, |
| 141 | + case.sdk_endpoint_name, |
237 | 142 | s, |
238 | | - server_url, |
| 143 | + case.expected_url, |
239 | 144 | monkeypatch |
240 | 145 | ) |
241 | 146 |
|
242 | 147 | try: |
243 | | - await client_method(request={}) |
| 148 | + if case.endpoint_url: |
| 149 | + await client_method(request={}, server_url=case.endpoint_url) |
| 150 | + else: |
| 151 | + await client_method(request={}) |
244 | 152 | except BaseUrlCorrect: |
245 | 153 | pass |
246 | 154 | except BaseUrlIncorrect as e: |
247 | | - pytest.fail(f"Default url not used, got {e}") |
| 155 | + pytest.fail( |
| 156 | + f"{case.description}: Expected {case.expected_url}, got {e}" |
| 157 | + ) |
0 commit comments