|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +import pytest |
| 7 | +from werkzeug.wrappers import Response |
| 8 | + |
| 9 | +from apify_client import ApifyClient, ApifyClientAsync |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from collections.abc import Callable |
| 13 | + |
| 14 | + from pytest_httpserver import HTTPServer |
| 15 | + from werkzeug.wrappers import Request |
| 16 | + |
| 17 | +_MOCK_LIST_RESPONSE = json.dumps( |
| 18 | + { |
| 19 | + 'data': { |
| 20 | + 'items': [], |
| 21 | + 'count': 0, |
| 22 | + 'offset': 0, |
| 23 | + 'limit': 100, |
| 24 | + 'total': 0, |
| 25 | + 'desc': False, |
| 26 | + } |
| 27 | + } |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def _make_handler(captured: dict) -> Callable[[Request], Response]: |
| 32 | + def handler(request: Request) -> Response: |
| 33 | + captured['args'] = dict(request.args) |
| 34 | + return Response(_MOCK_LIST_RESPONSE, content_type='application/json') |
| 35 | + |
| 36 | + return handler |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def client_urls(httpserver: HTTPServer) -> dict: |
| 41 | + server_url = httpserver.url_for('/').removesuffix('/') |
| 42 | + return {'api_url': server_url, 'api_public_url': server_url} |
| 43 | + |
| 44 | + |
| 45 | +def test_dataset_collection_list_ownership_sync(httpserver: HTTPServer, client_urls: dict) -> None: |
| 46 | + captured: dict = {} |
| 47 | + httpserver.expect_oneshot_request('/v2/datasets', method='GET').respond_with_handler(_make_handler(captured)) |
| 48 | + |
| 49 | + client = ApifyClient(token='placeholder_token', **client_urls) |
| 50 | + result = client.datasets().list(ownership='ownedByMe') |
| 51 | + |
| 52 | + assert result.total == 0 |
| 53 | + assert captured['args']['ownership'] == 'ownedByMe' |
| 54 | + |
| 55 | + |
| 56 | +async def test_dataset_collection_list_ownership_async(httpserver: HTTPServer, client_urls: dict) -> None: |
| 57 | + captured: dict = {} |
| 58 | + httpserver.expect_oneshot_request('/v2/datasets', method='GET').respond_with_handler(_make_handler(captured)) |
| 59 | + |
| 60 | + client = ApifyClientAsync(token='placeholder_token', **client_urls) |
| 61 | + result = await client.datasets().list(ownership='sharedWithMe') |
| 62 | + |
| 63 | + assert result.total == 0 |
| 64 | + assert captured['args']['ownership'] == 'sharedWithMe' |
| 65 | + |
| 66 | + |
| 67 | +def test_key_value_store_collection_list_ownership_sync(httpserver: HTTPServer, client_urls: dict) -> None: |
| 68 | + captured: dict = {} |
| 69 | + httpserver.expect_oneshot_request('/v2/key-value-stores', method='GET').respond_with_handler( |
| 70 | + _make_handler(captured) |
| 71 | + ) |
| 72 | + |
| 73 | + client = ApifyClient(token='placeholder_token', **client_urls) |
| 74 | + result = client.key_value_stores().list(ownership='ownedByMe') |
| 75 | + |
| 76 | + assert result.total == 0 |
| 77 | + assert captured['args']['ownership'] == 'ownedByMe' |
| 78 | + |
| 79 | + |
| 80 | +async def test_key_value_store_collection_list_ownership_async(httpserver: HTTPServer, client_urls: dict) -> None: |
| 81 | + captured: dict = {} |
| 82 | + httpserver.expect_oneshot_request('/v2/key-value-stores', method='GET').respond_with_handler( |
| 83 | + _make_handler(captured) |
| 84 | + ) |
| 85 | + |
| 86 | + client = ApifyClientAsync(token='placeholder_token', **client_urls) |
| 87 | + result = await client.key_value_stores().list(ownership='sharedWithMe') |
| 88 | + |
| 89 | + assert result.total == 0 |
| 90 | + assert captured['args']['ownership'] == 'sharedWithMe' |
| 91 | + |
| 92 | + |
| 93 | +def test_request_queue_collection_list_ownership_sync(httpserver: HTTPServer, client_urls: dict) -> None: |
| 94 | + captured: dict = {} |
| 95 | + httpserver.expect_oneshot_request('/v2/request-queues', method='GET').respond_with_handler(_make_handler(captured)) |
| 96 | + |
| 97 | + client = ApifyClient(token='placeholder_token', **client_urls) |
| 98 | + result = client.request_queues().list(ownership='ownedByMe') |
| 99 | + |
| 100 | + assert result.total == 0 |
| 101 | + assert captured['args']['ownership'] == 'ownedByMe' |
| 102 | + |
| 103 | + |
| 104 | +async def test_request_queue_collection_list_ownership_async(httpserver: HTTPServer, client_urls: dict) -> None: |
| 105 | + captured: dict = {} |
| 106 | + httpserver.expect_oneshot_request('/v2/request-queues', method='GET').respond_with_handler(_make_handler(captured)) |
| 107 | + |
| 108 | + client = ApifyClientAsync(token='placeholder_token', **client_urls) |
| 109 | + result = await client.request_queues().list(ownership='sharedWithMe') |
| 110 | + |
| 111 | + assert result.total == 0 |
| 112 | + assert captured['args']['ownership'] == 'sharedWithMe' |
0 commit comments