Repository navigation
Expand file tree
/
Copy pathtest_task.py
More file actions
495 lines (400 loc) · 15.8 KB
/
Copy pathtest_task.py
File metadata and controls
495 lines (400 loc) · 15.8 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
"""Unified tests for task (sync + async)."""
from __future__ import annotations
from collections.abc import AsyncIterator, Iterator
from datetime import timedelta
from typing import TYPE_CHECKING
from .._utils import collect_iterate_until_present, get_random_resource_name, maybe_await, poll_until_condition
from apify_client._models import (
ActorResource,
ListOfRuns,
ListOfTasks,
ListOfWebhooks,
Run,
RunListItem,
Task,
TaskListItem,
)
if TYPE_CHECKING:
from apify_client import ApifyClient, ApifyClientAsync
# Use a simple, fast public actor for testing
HELLO_WORLD_ACTOR = 'apify/hello-world'
async def test_task_create_and_get(client: ApifyClient | ApifyClientAsync) -> None:
"""Test creating a task and retrieving it."""
task_name = get_random_resource_name('task')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
actor_id = actor.id
# Create task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor_id,
name=task_name,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
assert created_task.id is not None
assert created_task.name == task_name
assert created_task.act_id == actor_id
# Get the same task
retrieved_task = await maybe_await(task_client.get())
assert isinstance(retrieved_task, Task)
assert retrieved_task.id == created_task.id
assert retrieved_task.name == task_name
finally:
await maybe_await(task_client.delete())
async def test_task_update(client: ApifyClient | ApifyClientAsync) -> None:
"""Test updating task properties."""
task_name = get_random_resource_name('task')
new_name = get_random_resource_name('task-updated')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
# Update the task
updated_task = await maybe_await(
task_client.update(
name=new_name,
timeout=timedelta(seconds=300),
)
)
assert isinstance(updated_task, Task)
assert updated_task.name == new_name
assert updated_task.id == created_task.id
# Verify the update persisted
retrieved_task = await maybe_await(task_client.get())
assert isinstance(retrieved_task, Task)
assert retrieved_task.name == new_name
finally:
await maybe_await(task_client.delete())
async def test_task_list(client: ApifyClient | ApifyClientAsync) -> None:
"""Test listing tasks."""
task_name = get_random_resource_name('task')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create a task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
)
)
assert isinstance(created_task, Task)
try:
# Poll until the fresh task appears in the listing (eventual consistency)
async def list_tasks() -> ListOfTasks:
page = await maybe_await(client.tasks().list(limit=100))
assert isinstance(page, ListOfTasks)
return page
tasks_page = await poll_until_condition(
list_tasks,
lambda page: created_task.id in {t.id for t in page.items},
)
# Verify our task is in the list
task_ids = [t.id for t in tasks_page.items]
assert created_task.id in task_ids
finally:
await maybe_await(client.task(created_task.id).delete())
async def test_task_get_input(client: ApifyClient | ApifyClientAsync) -> None:
"""Test getting and updating task input."""
task_name = get_random_resource_name('task')
test_input = {'message': 'Hello from test'}
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create task with input
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
task_input=test_input,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
# Get input
retrieved_input = await maybe_await(task_client.get_input())
assert isinstance(retrieved_input, dict)
assert retrieved_input.get('message') == 'Hello from test'
# Update input
new_input = {'message': 'Updated message'}
updated_input = await maybe_await(task_client.update_input(task_input=new_input))
assert isinstance(updated_input, dict)
assert updated_input.get('message') == 'Updated message'
finally:
await maybe_await(task_client.delete())
async def test_task_start(client: ApifyClient | ApifyClientAsync) -> None:
"""Test starting a task run."""
task_name = get_random_resource_name('task')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
# Start the task
run = await maybe_await(task_client.start())
assert isinstance(run, Run)
assert run.id is not None
assert run.act_id == actor.id
# Wait for the run to finish
finished_run = await maybe_await(client.run(run.id).wait_for_finish())
assert isinstance(finished_run, Run)
assert finished_run.status == 'SUCCEEDED'
# Cleanup run
await maybe_await(client.run(run.id).delete())
finally:
await maybe_await(task_client.delete())
async def test_task_call(client: ApifyClient | ApifyClientAsync) -> None:
"""Test calling a task and waiting for completion."""
task_name = get_random_resource_name('task')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
# Call the task (waits for finish)
run = await maybe_await(task_client.call())
assert isinstance(run, Run)
assert run.id is not None
assert run.status == 'SUCCEEDED'
# Cleanup run
await maybe_await(client.run(run.id).delete())
finally:
await maybe_await(task_client.delete())
async def test_task_delete(client: ApifyClient | ApifyClientAsync) -> None:
"""Test deleting a task."""
task_name = get_random_resource_name('task')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
# Delete task
await maybe_await(task_client.delete())
# Verify it's gone
retrieved_task = await maybe_await(task_client.get())
assert retrieved_task is None
async def test_task_runs(client: ApifyClient | ApifyClientAsync) -> None:
"""Test listing task runs."""
task_name = get_random_resource_name('task')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
# Run the task
run = await maybe_await(task_client.call())
assert isinstance(run, Run)
# Poll until the run appears in the task's run listing (eventual consistency)
async def list_runs() -> ListOfRuns:
page = await maybe_await(task_client.runs().list(limit=10))
assert isinstance(page, ListOfRuns)
return page
runs_page = await poll_until_condition(list_runs, lambda page: run.id in {r.id for r in page.items})
assert run.id in {r.id for r in runs_page.items}
# Cleanup run
await maybe_await(client.run(run.id).delete())
finally:
# Cleanup task
await maybe_await(task_client.delete())
async def test_task_last_run(client: ApifyClient | ApifyClientAsync) -> None:
"""Test getting the last run of a task."""
task_name = get_random_resource_name('task')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
# Run the task
run = await maybe_await(task_client.call())
assert isinstance(run, Run)
# Poll until the last run resolves to this run (eventual consistency)
last_run_client = task_client.last_run()
last_run = await poll_until_condition(
lambda: maybe_await(last_run_client.get()),
lambda r: isinstance(r, Run) and r.id == run.id,
)
assert isinstance(last_run, Run)
assert last_run.id == run.id
# Cleanup run
await maybe_await(client.run(run.id).delete())
finally:
# Cleanup task
await maybe_await(task_client.delete())
async def test_task_webhooks(client: ApifyClient | ApifyClientAsync) -> None:
"""Test listing webhooks for a task."""
task_name = get_random_resource_name('task')
# Get the actor ID for hello-world
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
# Create task
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
# Get webhooks client
webhooks_client = task_client.webhooks()
webhooks_page = await maybe_await(webhooks_client.list())
assert isinstance(webhooks_page, ListOfWebhooks)
assert webhooks_page.items is not None
# New task should have no webhooks
assert len(webhooks_page.items) == 0
finally:
# Cleanup task
await maybe_await(task_client.delete())
async def test_task_collection_iterate(client: ApifyClient | ApifyClientAsync, *, is_async: bool) -> None:
"""Test paginated iteration over user tasks."""
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
created_ids: list[str] = []
for _ in range(3):
task = await maybe_await(client.tasks().create(actor_id=actor.id, name=get_random_resource_name('task')))
assert isinstance(task, Task)
created_ids.append(task.id)
try:
collected = await collect_iterate_until_present(
lambda: client.tasks().iterate(desc=True),
set(created_ids),
item_type=TaskListItem,
is_async=is_async,
)
collected_ids = {t.id for t in collected}
for task_id in created_ids:
assert task_id in collected_ids
finally:
for task_id in created_ids:
await maybe_await(client.task(task_id).delete())
async def test_task_start_with_input_override(client: ApifyClient | ApifyClientAsync) -> None:
"""Test starting a task with run input that overrides the saved input."""
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
task_name = get_random_resource_name('task')
created_task = await maybe_await(
client.tasks().create(
actor_id=actor.id,
name=task_name,
task_input={'message': 'original'},
)
)
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
run = await maybe_await(task_client.start(task_input={'message': 'overridden'}, memory_mbytes=256))
assert isinstance(run, Run)
# Wait and clean up
await maybe_await(client.run(run.id).wait_for_finish())
await maybe_await(client.run(run.id).delete())
finally:
await maybe_await(task_client.delete())
async def test_task_call_with_build_override(client: ApifyClient | ApifyClientAsync) -> None:
"""Test calling a task with explicit build/memory overrides."""
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
task_name = get_random_resource_name('task')
created_task = await maybe_await(client.tasks().create(actor_id=actor.id, name=task_name))
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
run = await maybe_await(
task_client.call(
build='latest',
memory_mbytes=256,
run_timeout=timedelta(seconds=120),
)
)
assert isinstance(run, Run)
assert run.status == 'SUCCEEDED'
assert run.options is not None
assert run.options.memory_mbytes == 256
await maybe_await(client.run(run.id).delete())
finally:
await maybe_await(task_client.delete())
async def test_task_runs_iterate(client: ApifyClient | ApifyClientAsync, *, is_async: bool) -> None:
"""Test paginated iteration over a task's runs."""
actor = await maybe_await(client.actor(HELLO_WORLD_ACTOR).get())
assert isinstance(actor, ActorResource)
task_name = get_random_resource_name('task')
created_task = await maybe_await(client.tasks().create(actor_id=actor.id, name=task_name))
assert isinstance(created_task, Task)
task_client = client.task(created_task.id)
try:
run = await maybe_await(task_client.call())
assert isinstance(run, Run)
iterator = task_client.runs().iterate(limit=5)
collected: list[RunListItem] = []
if is_async:
assert isinstance(iterator, AsyncIterator)
async for r in iterator:
assert isinstance(r, RunListItem)
collected.append(r)
else:
assert isinstance(iterator, Iterator)
for r in iterator:
assert isinstance(r, RunListItem)
collected.append(r)
run_ids = [r.id for r in collected]
assert run.id in run_ids
await maybe_await(client.run(run.id).delete())
finally:
await maybe_await(task_client.delete())
async def test_task_get_nonexistent_returns_none(client: ApifyClient | ApifyClientAsync) -> None:
"""Test that get() on a non-existent task returns None."""
task = await maybe_await(client.task('NoNeXiStEnTtAsK1').get())
assert task is None