|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from apify_client import ApifyClient, ApifyClientAsync |
| 8 | +from apify_client.errors import NotFoundError |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from pytest_httpserver import HTTPServer |
| 12 | + |
| 13 | +_CHILD_CLIENT_PARAMS = [ |
| 14 | + pytest.param('dataset', 'dataset', id='dataset'), |
| 15 | + pytest.param('key_value_store', 'key-value-store', id='key-value-store'), |
| 16 | + pytest.param('request_queue', 'request-queue', id='request-queue'), |
| 17 | + pytest.param('log', 'log', id='log'), |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +_NOT_FOUND_BODY = {'error': {'type': 'record-not-found', 'message': 'not found'}} |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize(('child_method', 'child_path'), _CHILD_CLIENT_PARAMS) |
| 25 | +def test_last_run_filters_propagate_to_chained_clients( |
| 26 | + httpserver: HTTPServer, |
| 27 | + child_method: str, |
| 28 | + child_path: str, |
| 29 | +) -> None: |
| 30 | + """`last_run(status=..., origin=...)` filters must be sent by the chained storage clients (regression vs 1.x).""" |
| 31 | + httpserver.expect_request(f'/v2/actors/actor-id/runs/last/{child_path}').respond_with_json( |
| 32 | + _NOT_FOUND_BODY, status=404 |
| 33 | + ) |
| 34 | + client = ApifyClient(token='test-token', api_url=httpserver.url_for('/').removesuffix('/')) |
| 35 | + |
| 36 | + last_run = client.actor('actor-id').last_run(status='SUCCEEDED', origin='WEB') |
| 37 | + with pytest.raises(NotFoundError): |
| 38 | + getattr(last_run, child_method)().get() |
| 39 | + |
| 40 | + request, _ = httpserver.log[-1] |
| 41 | + assert request.args.get('status') == 'SUCCEEDED' |
| 42 | + assert request.args.get('origin') == 'WEB' |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize(('child_method', 'child_path'), _CHILD_CLIENT_PARAMS) |
| 46 | +async def test_last_run_filters_propagate_to_chained_clients_async( |
| 47 | + httpserver: HTTPServer, |
| 48 | + child_method: str, |
| 49 | + child_path: str, |
| 50 | +) -> None: |
| 51 | + """`last_run(status=..., origin=...)` filters must be sent by the chained storage clients (regression vs 1.x).""" |
| 52 | + httpserver.expect_request(f'/v2/actors/actor-id/runs/last/{child_path}').respond_with_json( |
| 53 | + _NOT_FOUND_BODY, status=404 |
| 54 | + ) |
| 55 | + client = ApifyClientAsync(token='test-token', api_url=httpserver.url_for('/').removesuffix('/')) |
| 56 | + |
| 57 | + last_run = client.actor('actor-id').last_run(status='SUCCEEDED', origin='WEB') |
| 58 | + with pytest.raises(NotFoundError): |
| 59 | + await getattr(last_run, child_method)().get() |
| 60 | + |
| 61 | + request, _ = httpserver.log[-1] |
| 62 | + assert request.args.get('status') == 'SUCCEEDED' |
| 63 | + assert request.args.get('origin') == 'WEB' |
0 commit comments