forked from apify/apify-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_actor_helpers.py
More file actions
176 lines (131 loc) · 6.13 KB
/
test_actor_helpers.py
File metadata and controls
176 lines (131 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from apify_client import ApifyClientAsync
from apify_shared.consts import ApifyEnvVars, WebhookEventType
from apify import Actor, Webhook
from apify._actor import _ActorType
if TYPE_CHECKING:
from ..conftest import ApifyClientAsyncPatcher
@pytest.fixture
def fake_actor_run() -> dict:
return {
'id': 'asdfasdf',
'buildId': '3ads35',
'buildNumber': '3.4.5',
'actId': 'actor_id',
'actorId': 'actor_id',
'userId': 'user_id',
'startedAt': '2024-08-08 12:12:44',
'status': 'RUNNING',
'meta': {'origin': 'API'},
'containerUrl': 'http://0.0.0.0:3333',
'defaultDatasetId': 'dhasdrfughaerguoi',
'defaultKeyValueStoreId': 'asjkldhguiofg',
'defaultRequestQueueId': 'lkjgklserjghios',
'stats': {
'inputBodyLen': 0,
'restartCount': 0,
'resurrectCount': 0,
'memAvgBytes': 0,
'memMaxBytes': 0,
'memCurrentBytes': 0,
'cpuAvgUsage': 0,
'cpuMaxUsage': 0,
'cpuCurrentUsage': 0,
'netRxBytes': 0,
'netTxBytes': 0,
'durationMillis': 3333,
'runTimeSecs': 33,
'metamorph': 0,
'computeUnits': 4.33,
},
'options': {
'build': '',
'timeoutSecs': 44,
'memoryMbytes': 4096,
'diskMbytes': 16384,
},
}
async def test_new_client_config_creation(monkeypatch: pytest.MonkeyPatch) -> None:
token = 'my-token'
monkeypatch.setenv(ApifyEnvVars.TOKEN, token)
my_actor = _ActorType()
await my_actor.init()
client = my_actor.new_client()
assert isinstance(client, ApifyClientAsync)
assert client.token == token
passed_token = 'my-passed-token'
client_with_token = my_actor.new_client(token=passed_token)
assert isinstance(client_with_token, ApifyClientAsync)
assert client_with_token.token == passed_token
await my_actor.exit()
async def test_call_actor(apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict) -> None:
apify_client_async_patcher.patch('actor', 'call', return_value=fake_actor_run)
actor_id = 'some-actor-id'
async with Actor:
await Actor.call(actor_id)
assert len(apify_client_async_patcher.calls['actor']['call']) == 1
# The first argument is ActorClientAsync, which was called, let's check its id.
assert apify_client_async_patcher.calls['actor']['call'][0][0][0].resource_id == actor_id
async def test_call_actor_task(apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict) -> None:
apify_client_async_patcher.patch('task', 'call', return_value=fake_actor_run)
task_id = 'some-task-id'
async with Actor:
await Actor.call_task(task_id)
assert len(apify_client_async_patcher.calls['task']['call']) == 1
assert apify_client_async_patcher.calls['task']['call'][0][0][0].resource_id == task_id
async def test_start_actor(apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict) -> None:
apify_client_async_patcher.patch('actor', 'start', return_value=fake_actor_run)
actor_id = 'some-id'
async with Actor:
await Actor.start(actor_id)
assert len(apify_client_async_patcher.calls['actor']['start']) == 1
assert apify_client_async_patcher.calls['actor']['start'][0][0][0].resource_id == actor_id
async def test_abort_actor_run(apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict) -> None:
apify_client_async_patcher.patch('run', 'abort', return_value=fake_actor_run)
run_id = 'some-run-id'
async with Actor:
await Actor.abort(run_id)
assert len(apify_client_async_patcher.calls['run']['abort']) == 1
assert apify_client_async_patcher.calls['run']['abort'][0][0][0].resource_id == run_id
# NOTE: The following methods are properly tested using integrations tests.
async def test_metamorph_fails_locally(caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level('WARNING')
async with Actor:
await Actor.metamorph('random-id')
assert len(caplog.records) == 1
assert caplog.records[0].levelname == 'ERROR'
assert 'Actor.metamorph() is only supported when running on the Apify platform.' in caplog.records[0].message
async def test_reboot_fails_locally(caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level('WARNING')
async with Actor:
await Actor.reboot()
assert len(caplog.records) == 1
assert caplog.records[0].levelname == 'ERROR'
assert 'Actor.reboot() is only supported when running on the Apify platform.' in caplog.records[0].message
async def test_add_webhook_fails_locally(caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level('WARNING')
async with Actor:
await Actor.add_webhook(
Webhook(event_types=[WebhookEventType.ACTOR_BUILD_ABORTED], request_url='https://example.com')
)
assert len(caplog.records) == 1
assert caplog.records[0].levelname == 'ERROR'
assert 'Actor.add_webhook() is only supported when running on the Apify platform.' in caplog.records[0].message
async def test_set_status_message_locally(caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level('INFO')
async with Actor:
await Actor.set_status_message('test-status-message')
matching_records = [record for record in caplog.records if 'test-status-message' in record.message]
assert len(matching_records) == 1
assert matching_records[0].levelname == 'INFO'
assert '[Status message]: test-status-message' in matching_records[0].message
async def test_set_terminal_status_message_locally(caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level('INFO')
async with Actor:
await Actor.fail(status_message='test-terminal-message')
matching_records = [record for record in caplog.records if 'test-terminal-message' in record.message]
assert len(matching_records) == 1
assert matching_records[0].levelname == 'INFO'
assert '[Terminal status message]: test-terminal-message' in matching_records[0].message