|
| 1 | +import pytest |
| 2 | + |
| 3 | +from apify import Actor |
| 4 | +from apify.consts import ApifyEnvVars |
| 5 | +from apify_client import ApifyClientAsync |
| 6 | +from apify_client.consts import WebhookEventType |
| 7 | + |
| 8 | +from ..conftest import ApifyClientAsyncPatcher |
| 9 | + |
| 10 | + |
| 11 | +class TestActorNewClient: |
| 12 | + |
| 13 | + async def test_actor_new_client_config(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 14 | + token = 'my-token' |
| 15 | + monkeypatch.setenv(ApifyEnvVars.TOKEN, token) |
| 16 | + my_actor = Actor() |
| 17 | + await my_actor.init() |
| 18 | + client = my_actor.new_client() |
| 19 | + assert type(client) == ApifyClientAsync |
| 20 | + assert client.token == token |
| 21 | + passed_token = 'my-passed-token' |
| 22 | + client_with_token = my_actor.new_client(token=passed_token) |
| 23 | + assert type(client_with_token) == ApifyClientAsync |
| 24 | + assert client_with_token.token == passed_token |
| 25 | + await my_actor.exit() |
| 26 | + |
| 27 | + |
| 28 | +class TestActorCallStartAbortActor: |
| 29 | + |
| 30 | + async def test_actor_call(self, apify_client_async_patcher: ApifyClientAsyncPatcher) -> None: |
| 31 | + apify_client_async_patcher.patch('actor', 'call', return_value=None) |
| 32 | + actor_id = 'some-actor-id' |
| 33 | + my_actor = Actor() |
| 34 | + await my_actor.init() |
| 35 | + await my_actor.call(actor_id) |
| 36 | + assert len(apify_client_async_patcher.calls['actor']['call']) == 1 |
| 37 | + # The first argument is ActorClientAsync, which was called, let's check its id. |
| 38 | + assert apify_client_async_patcher.calls['actor']['call'][0][0][0].resource_id == actor_id |
| 39 | + await my_actor.exit() |
| 40 | + |
| 41 | + async def test_actor_call_task(self, apify_client_async_patcher: ApifyClientAsyncPatcher) -> None: |
| 42 | + apify_client_async_patcher.patch('task', 'call', return_value=None) |
| 43 | + task_id = 'some-task-id' |
| 44 | + my_actor = Actor() |
| 45 | + await my_actor.init() |
| 46 | + await my_actor.call_task(task_id) |
| 47 | + assert len(apify_client_async_patcher.calls['task']['call']) == 1 |
| 48 | + assert apify_client_async_patcher.calls['task']['call'][0][0][0].resource_id == task_id |
| 49 | + await my_actor.exit() |
| 50 | + |
| 51 | + async def test_actor_start(self, apify_client_async_patcher: ApifyClientAsyncPatcher) -> None: |
| 52 | + apify_client_async_patcher.patch('actor', 'start', return_value=None) |
| 53 | + actor_id = 'some-id' |
| 54 | + my_actor = Actor() |
| 55 | + await my_actor.init() |
| 56 | + await my_actor.start(actor_id) |
| 57 | + assert len(apify_client_async_patcher.calls['actor']['start']) == 1 |
| 58 | + assert apify_client_async_patcher.calls['actor']['start'][0][0][0].resource_id == actor_id |
| 59 | + await my_actor.exit() |
| 60 | + |
| 61 | + async def test_actor_abort(self, apify_client_async_patcher: ApifyClientAsyncPatcher) -> None: |
| 62 | + apify_client_async_patcher.patch('run', 'abort', return_value=None) |
| 63 | + run_id = 'some-run-id' |
| 64 | + my_actor = Actor() |
| 65 | + await my_actor.init() |
| 66 | + await my_actor.abort(run_id) |
| 67 | + assert len(apify_client_async_patcher.calls['run']['abort']) == 1 |
| 68 | + assert apify_client_async_patcher.calls['run']['abort'][0][0][0].resource_id == run_id |
| 69 | + await my_actor.exit() |
| 70 | + |
| 71 | + |
| 72 | +class TestActorMethodsWorksOnlyOnPlatform: |
| 73 | + # NOTE: These medhods will be tested properly using integrations tests. |
| 74 | + |
| 75 | + async def test_actor_metamorpth_not_work_locally(self, capfd: pytest.CaptureFixture) -> None: |
| 76 | + async with Actor() as my_actor: |
| 77 | + await my_actor.metamorph('random-id') |
| 78 | + out, err = capfd.readouterr() |
| 79 | + assert 'Actor.metamorph() is only supported when running on the Apify platform.' in out |
| 80 | + |
| 81 | + async def test_actor_reboot_not_work_locally(self, capfd: pytest.CaptureFixture) -> None: |
| 82 | + async with Actor() as my_actor: |
| 83 | + await my_actor.reboot() |
| 84 | + out, err = capfd.readouterr() |
| 85 | + assert 'Actor.reboot() is only supported when running on the Apify platform.' in out |
| 86 | + |
| 87 | + async def test_actor_add_webhook_not_work_locally(self, capfd: pytest.CaptureFixture) -> None: |
| 88 | + async with Actor() as my_actor: |
| 89 | + await my_actor.add_webhook(event_types=[WebhookEventType.ACTOR_BUILD_ABORTED], request_url='https://example.com') |
| 90 | + out, err = capfd.readouterr() |
| 91 | + assert 'Actor.add_webhook() is only supported when running on the Apify platform.' in out |
| 92 | + |
| 93 | + async def test_actor_set_status_message_not_work_locally(self, capfd: pytest.CaptureFixture) -> None: |
| 94 | + async with Actor() as my_actor: |
| 95 | + await my_actor.set_status_message('test') |
| 96 | + out, err = capfd.readouterr() |
| 97 | + assert 'Actor.set_status_message() is only supported when running on the Apify platform.' in out |
0 commit comments