|
6 | 6 | import pytest |
7 | 7 | from werkzeug import Response |
8 | 8 |
|
| 9 | +from apify_client import ApifyClient, ApifyClientAsync |
9 | 10 | from apify_client._http_clients import ImpitHttpClient, ImpitHttpClientAsync |
10 | 11 | from apify_client.errors import ( |
11 | 12 | ApifyApiError, |
@@ -211,3 +212,41 @@ def test_apify_api_error_falls_back_for_unparsable_body(httpserver: HTTPServer) |
211 | 212 |
|
212 | 213 | assert type(exc.value) is ApifyApiError |
213 | 214 | assert exc.value.type is None |
| 215 | + |
| 216 | + |
| 217 | +def _not_found_body() -> dict: |
| 218 | + return {'error': {'type': 'record-not-found', 'message': 'not found'}} |
| 219 | + |
| 220 | + |
| 221 | +def test_direct_get_returns_none_on_404(httpserver: HTTPServer) -> None: |
| 222 | + """`client.dataset("X").get()` — a direct, ID-identified fetch — swallows 404 into `None`.""" |
| 223 | + httpserver.expect_request('/v2/datasets/missing').respond_with_json(_not_found_body(), status=404) |
| 224 | + client = ApifyClient(token='test', api_url=httpserver.url_for('/').removesuffix('/')) |
| 225 | + |
| 226 | + assert client.dataset('missing').get() is None |
| 227 | + |
| 228 | + |
| 229 | +async def test_direct_get_returns_none_on_404_async(httpserver: HTTPServer) -> None: |
| 230 | + """Async mirror: direct `.get()` swallows 404.""" |
| 231 | + httpserver.expect_request('/v2/datasets/missing').respond_with_json(_not_found_body(), status=404) |
| 232 | + client = ApifyClientAsync(token='test', api_url=httpserver.url_for('/').removesuffix('/')) |
| 233 | + |
| 234 | + assert await client.dataset('missing').get() is None |
| 235 | + |
| 236 | + |
| 237 | +def test_chained_get_raises_on_404(httpserver: HTTPServer) -> None: |
| 238 | + """`run("X").dataset().get()` is ambiguous on 404 (run or dataset missing) — propagate instead of `None`.""" |
| 239 | + httpserver.expect_request('/v2/actor-runs/missing-run/dataset').respond_with_json(_not_found_body(), status=404) |
| 240 | + client = ApifyClient(token='test', api_url=httpserver.url_for('/').removesuffix('/')) |
| 241 | + |
| 242 | + with pytest.raises(NotFoundError): |
| 243 | + client.run('missing-run').dataset().get() |
| 244 | + |
| 245 | + |
| 246 | +async def test_chained_get_raises_on_404_async(httpserver: HTTPServer) -> None: |
| 247 | + """Async mirror: chained `.get()` propagates NotFoundError.""" |
| 248 | + httpserver.expect_request('/v2/actor-runs/missing-run/dataset').respond_with_json(_not_found_body(), status=404) |
| 249 | + client = ApifyClientAsync(token='test', api_url=httpserver.url_for('/').removesuffix('/')) |
| 250 | + |
| 251 | + with pytest.raises(NotFoundError): |
| 252 | + await client.run('missing-run').dataset().get() |
0 commit comments