|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from typing import TYPE_CHECKING, Any |
| 5 | + |
| 6 | +import pytest |
| 7 | +from werkzeug import Request, Response |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from collections.abc import Callable |
| 11 | + |
| 12 | + from pytest_httpserver import HTTPServer |
| 13 | + |
| 14 | + from apify_client import ApifyClient, ApifyClientAsync |
| 15 | + |
| 16 | +pytestmark = pytest.mark.usefixtures('http_client_classes') |
| 17 | + |
| 18 | +_MOCKED_ACTOR_ID = 'test_actor_id' |
| 19 | +_VERSION_NUMBER = '0.1' |
| 20 | +_VERSIONS_PATH = f'/v2/actors/{_MOCKED_ACTOR_ID}/versions' |
| 21 | +_VERSION_PATH = f'{_VERSIONS_PATH}/{_VERSION_NUMBER}' |
| 22 | + |
| 23 | +_ACTOR_PATH = 'actors/my-actor' |
| 24 | +_TARBALL_URL = 'https://example.com/monorepo.zip' |
| 25 | + |
| 26 | +_VERSION_RESPONSE = { |
| 27 | + 'data': { |
| 28 | + 'versionNumber': _VERSION_NUMBER, |
| 29 | + 'sourceType': 'TARBALL', |
| 30 | + 'tarballUrl': _TARBALL_URL, |
| 31 | + 'buildTag': 'latest', |
| 32 | + 'envVars': [], |
| 33 | + 'actorPath': _ACTOR_PATH, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +def capture(captured: list[Request]) -> Callable[[Request], Response]: |
| 39 | + def handler(request: Request) -> Response: |
| 40 | + captured.append(request) |
| 41 | + return Response(json.dumps(_VERSION_RESPONSE), status=200, mimetype='application/json') |
| 42 | + |
| 43 | + return handler |
| 44 | + |
| 45 | + |
| 46 | +def request_body(request: Request) -> dict[str, Any]: |
| 47 | + return json.loads(request.get_data()) |
| 48 | + |
| 49 | + |
| 50 | +def test_create_sends_actor_path(httpserver: HTTPServer, sync_client: ApifyClient) -> None: |
| 51 | + """`create` sends `actor_path` as `actorPath` and reads it back from the response.""" |
| 52 | + captured: list[Request] = [] |
| 53 | + httpserver.expect_request(_VERSIONS_PATH, method='POST').respond_with_handler(capture(captured)) |
| 54 | + |
| 55 | + versions_client = sync_client.actor(_MOCKED_ACTOR_ID).versions() |
| 56 | + version = versions_client.create( |
| 57 | + version_number=_VERSION_NUMBER, |
| 58 | + source_type='TARBALL', |
| 59 | + tarball_url=_TARBALL_URL, |
| 60 | + actor_path=_ACTOR_PATH, |
| 61 | + ) |
| 62 | + |
| 63 | + assert len(captured) == 1 |
| 64 | + assert request_body(captured[0]) == { |
| 65 | + 'versionNumber': _VERSION_NUMBER, |
| 66 | + 'sourceType': 'TARBALL', |
| 67 | + 'tarballUrl': _TARBALL_URL, |
| 68 | + 'actorPath': _ACTOR_PATH, |
| 69 | + } |
| 70 | + assert version.actor_path == _ACTOR_PATH |
| 71 | + |
| 72 | + |
| 73 | +async def test_create_sends_actor_path_async(httpserver: HTTPServer, async_client: ApifyClientAsync) -> None: |
| 74 | + """Async `create` sends `actor_path` as `actorPath` and reads it back from the response.""" |
| 75 | + captured: list[Request] = [] |
| 76 | + httpserver.expect_request(_VERSIONS_PATH, method='POST').respond_with_handler(capture(captured)) |
| 77 | + |
| 78 | + versions_client = async_client.actor(_MOCKED_ACTOR_ID).versions() |
| 79 | + version = await versions_client.create( |
| 80 | + version_number=_VERSION_NUMBER, |
| 81 | + source_type='TARBALL', |
| 82 | + tarball_url=_TARBALL_URL, |
| 83 | + actor_path=_ACTOR_PATH, |
| 84 | + ) |
| 85 | + |
| 86 | + assert len(captured) == 1 |
| 87 | + assert request_body(captured[0]) == { |
| 88 | + 'versionNumber': _VERSION_NUMBER, |
| 89 | + 'sourceType': 'TARBALL', |
| 90 | + 'tarballUrl': _TARBALL_URL, |
| 91 | + 'actorPath': _ACTOR_PATH, |
| 92 | + } |
| 93 | + assert version.actor_path == _ACTOR_PATH |
| 94 | + |
| 95 | + |
| 96 | +def test_create_omits_actor_path_when_not_passed(httpserver: HTTPServer, sync_client: ApifyClient) -> None: |
| 97 | + """`create` leaves `actorPath` out of the request body when `actor_path` is not passed.""" |
| 98 | + captured: list[Request] = [] |
| 99 | + httpserver.expect_request(_VERSIONS_PATH, method='POST').respond_with_handler(capture(captured)) |
| 100 | + |
| 101 | + versions_client = sync_client.actor(_MOCKED_ACTOR_ID).versions() |
| 102 | + versions_client.create( |
| 103 | + version_number=_VERSION_NUMBER, |
| 104 | + source_type='TARBALL', |
| 105 | + tarball_url=_TARBALL_URL, |
| 106 | + ) |
| 107 | + |
| 108 | + assert len(captured) == 1 |
| 109 | + assert 'actorPath' not in request_body(captured[0]) |
| 110 | + |
| 111 | + |
| 112 | +async def test_create_omits_actor_path_when_not_passed_async( |
| 113 | + httpserver: HTTPServer, async_client: ApifyClientAsync |
| 114 | +) -> None: |
| 115 | + """Async `create` leaves `actorPath` out of the request body when `actor_path` is not passed.""" |
| 116 | + captured: list[Request] = [] |
| 117 | + httpserver.expect_request(_VERSIONS_PATH, method='POST').respond_with_handler(capture(captured)) |
| 118 | + |
| 119 | + versions_client = async_client.actor(_MOCKED_ACTOR_ID).versions() |
| 120 | + await versions_client.create( |
| 121 | + version_number=_VERSION_NUMBER, |
| 122 | + source_type='TARBALL', |
| 123 | + tarball_url=_TARBALL_URL, |
| 124 | + ) |
| 125 | + |
| 126 | + assert len(captured) == 1 |
| 127 | + assert 'actorPath' not in request_body(captured[0]) |
| 128 | + |
| 129 | + |
| 130 | +def test_update_sends_actor_path(httpserver: HTTPServer, sync_client: ApifyClient) -> None: |
| 131 | + """`update` sends `actor_path` as `actorPath` and reads it back from the response.""" |
| 132 | + captured: list[Request] = [] |
| 133 | + httpserver.expect_request(_VERSION_PATH, method='PUT').respond_with_handler(capture(captured)) |
| 134 | + |
| 135 | + version_client = sync_client.actor(_MOCKED_ACTOR_ID).version(_VERSION_NUMBER) |
| 136 | + version = version_client.update( |
| 137 | + tarball_url=_TARBALL_URL, |
| 138 | + actor_path=_ACTOR_PATH, |
| 139 | + ) |
| 140 | + |
| 141 | + assert len(captured) == 1 |
| 142 | + assert request_body(captured[0]) == {'tarballUrl': _TARBALL_URL, 'actorPath': _ACTOR_PATH} |
| 143 | + assert version.actor_path == _ACTOR_PATH |
| 144 | + |
| 145 | + |
| 146 | +async def test_update_sends_actor_path_async(httpserver: HTTPServer, async_client: ApifyClientAsync) -> None: |
| 147 | + """Async `update` sends `actor_path` as `actorPath` and reads it back from the response.""" |
| 148 | + captured: list[Request] = [] |
| 149 | + httpserver.expect_request(_VERSION_PATH, method='PUT').respond_with_handler(capture(captured)) |
| 150 | + |
| 151 | + version_client = async_client.actor(_MOCKED_ACTOR_ID).version(_VERSION_NUMBER) |
| 152 | + version = await version_client.update( |
| 153 | + tarball_url=_TARBALL_URL, |
| 154 | + actor_path=_ACTOR_PATH, |
| 155 | + ) |
| 156 | + |
| 157 | + assert len(captured) == 1 |
| 158 | + assert request_body(captured[0]) == {'tarballUrl': _TARBALL_URL, 'actorPath': _ACTOR_PATH} |
| 159 | + assert version.actor_path == _ACTOR_PATH |
| 160 | + |
| 161 | + |
| 162 | +def test_update_omits_actor_path_when_not_passed(httpserver: HTTPServer, sync_client: ApifyClient) -> None: |
| 163 | + """`update` leaves `actorPath` out of the request body when `actor_path` is not passed.""" |
| 164 | + captured: list[Request] = [] |
| 165 | + httpserver.expect_request(_VERSION_PATH, method='PUT').respond_with_handler(capture(captured)) |
| 166 | + |
| 167 | + version_client = sync_client.actor(_MOCKED_ACTOR_ID).version(_VERSION_NUMBER) |
| 168 | + version_client.update(tarball_url=_TARBALL_URL) |
| 169 | + |
| 170 | + assert len(captured) == 1 |
| 171 | + assert request_body(captured[0]) == {'tarballUrl': _TARBALL_URL} |
| 172 | + |
| 173 | + |
| 174 | +async def test_update_omits_actor_path_when_not_passed_async( |
| 175 | + httpserver: HTTPServer, async_client: ApifyClientAsync |
| 176 | +) -> None: |
| 177 | + """Async `update` leaves `actorPath` out of the request body when `actor_path` is not passed.""" |
| 178 | + captured: list[Request] = [] |
| 179 | + httpserver.expect_request(_VERSION_PATH, method='PUT').respond_with_handler(capture(captured)) |
| 180 | + |
| 181 | + version_client = async_client.actor(_MOCKED_ACTOR_ID).version(_VERSION_NUMBER) |
| 182 | + await version_client.update(tarball_url=_TARBALL_URL) |
| 183 | + |
| 184 | + assert len(captured) == 1 |
| 185 | + assert request_body(captured[0]) == {'tarballUrl': _TARBALL_URL} |
0 commit comments