Skip to content

Commit 72d92ea

Browse files
drobnikjfnesveda
andauthored
feat: add the rest unit tests for actor (#40)
Co-authored-by: František Nesveda <fnesveda@users.noreply.github.com>
1 parent 829bf79 commit 72d92ea

File tree

2 files changed

+119
-8
lines changed

2 files changed

+119
-8
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

tests/unit/actor/test_actor_lifecycle.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
from apify import Actor
88
from apify.consts import ActorEventType, ApifyEnvVars
9+
from apify_client import ApifyClientAsync
910

1011

1112
class TestActorInit:
1213

1314
async def test_async_with_actor_properly_initialize(self) -> None:
1415
async with Actor:
1516
assert Actor._get_default_instance()._is_initialized
16-
# TODO: More checks
1717
assert Actor._get_default_instance()._is_initialized is False
1818

1919
async def test_actor_init(self) -> None:
@@ -77,16 +77,14 @@ async def test_raise_on_exit_witout_init(self) -> None:
7777
class TestActorFail:
7878

7979
async def test_with_actor_fail(self) -> None:
80-
my_actr = Actor()
81-
async with my_actr:
82-
assert my_actr._is_initialized
83-
await my_actr.fail()
84-
assert my_actr._is_initialized is False
80+
async with Actor() as my_actor:
81+
assert my_actor._is_initialized
82+
await my_actor.fail()
83+
assert my_actor._is_initialized is False
8584

8685
async def test_with_actor_failed(self) -> None:
87-
my_actor = Actor()
8886
try:
89-
async with my_actor:
87+
async with Actor() as my_actor:
9088
assert my_actor._is_initialized
9189
raise Exception('Failed')
9290
except Exception:
@@ -139,3 +137,19 @@ async def actor_function() -> str:
139137

140138
returned_value = await my_actor.main(actor_function)
141139
assert returned_value == expected_string
140+
141+
class TestActorNewClient:
142+
143+
async def test_actor_new_client_config(self, monkeypatch: pytest.MonkeyPatch) -> None:
144+
token = 'my-token'
145+
monkeypatch.setenv(ApifyEnvVars.TOKEN, token)
146+
my_actor = Actor()
147+
await my_actor.init()
148+
client = my_actor.new_client()
149+
assert type(client) == ApifyClientAsync
150+
assert client.token == token
151+
passed_token = 'my-passed-token'
152+
client_with_token = my_actor.new_client(token=passed_token)
153+
assert type(client_with_token) == ApifyClientAsync
154+
assert client_with_token.token == passed_token
155+
await my_actor.exit()

0 commit comments

Comments
 (0)