|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import warnings |
| 4 | +from datetime import timedelta |
3 | 5 | from typing import TYPE_CHECKING |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
7 | 9 | from apify_client import ApifyClientAsync |
8 | 10 | from apify_shared.consts import ApifyEnvVars, WebhookEventType |
| 11 | +from crawlee.events._types import Event |
9 | 12 |
|
10 | 13 | from apify import Actor, Webhook |
11 | 14 | from apify._actor import _ActorType |
@@ -174,3 +177,179 @@ async def test_set_terminal_status_message_locally(caplog: pytest.LogCaptureFixt |
174 | 177 | assert len(matching_records) == 1 |
175 | 178 | assert matching_records[0].levelname == 'INFO' |
176 | 179 | assert '[Terminal status message]: test-terminal-message' in matching_records[0].message |
| 180 | + |
| 181 | + |
| 182 | +async def test_push_data_with_empty_data() -> None: |
| 183 | + """Test that push_data returns None when data is empty.""" |
| 184 | + async with Actor: |
| 185 | + result = await Actor.push_data([]) |
| 186 | + assert result is None |
| 187 | + |
| 188 | + result = await Actor.push_data({}) |
| 189 | + assert result is None |
| 190 | + |
| 191 | + |
| 192 | +async def test_off_removes_event_listener() -> None: |
| 193 | + """Test that Actor.off() removes an event listener.""" |
| 194 | + called = False |
| 195 | + |
| 196 | + async def listener(_data: object) -> None: |
| 197 | + nonlocal called |
| 198 | + called = True |
| 199 | + |
| 200 | + async with Actor: |
| 201 | + Actor.on(Event.PERSIST_STATE, listener) |
| 202 | + Actor.off(Event.PERSIST_STATE, listener) |
| 203 | + |
| 204 | + |
| 205 | +async def test_start_actor_with_webhooks( |
| 206 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 207 | +) -> None: |
| 208 | + """Test that start() correctly serializes webhooks.""" |
| 209 | + apify_client_async_patcher.patch('actor', 'start', return_value=fake_actor_run) |
| 210 | + |
| 211 | + async with Actor: |
| 212 | + await Actor.start( |
| 213 | + 'some-actor-id', |
| 214 | + webhooks=[Webhook(event_types=[WebhookEventType.ACTOR_RUN_SUCCEEDED], request_url='https://example.com')], |
| 215 | + ) |
| 216 | + |
| 217 | + assert len(apify_client_async_patcher.calls['actor']['start']) == 1 |
| 218 | + |
| 219 | + |
| 220 | +async def test_start_actor_with_timedelta_timeout( |
| 221 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 222 | +) -> None: |
| 223 | + """Test that start() accepts a timedelta timeout.""" |
| 224 | + apify_client_async_patcher.patch('actor', 'start', return_value=fake_actor_run) |
| 225 | + |
| 226 | + async with Actor: |
| 227 | + await Actor.start('some-actor-id', timeout=timedelta(seconds=120)) |
| 228 | + |
| 229 | + assert len(apify_client_async_patcher.calls['actor']['start']) == 1 |
| 230 | + |
| 231 | + |
| 232 | +async def test_start_actor_with_invalid_timeout( |
| 233 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 234 | +) -> None: |
| 235 | + """Test that start() raises ValueError for invalid timeout.""" |
| 236 | + apify_client_async_patcher.patch('actor', 'start', return_value=fake_actor_run) |
| 237 | + |
| 238 | + async with Actor: |
| 239 | + with pytest.raises(ValueError, match='Invalid timeout'): |
| 240 | + await Actor.start('some-actor-id', timeout='invalid') # type: ignore[arg-type] |
| 241 | + |
| 242 | + |
| 243 | +async def test_call_actor_with_webhooks( |
| 244 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 245 | +) -> None: |
| 246 | + """Test that call() correctly serializes webhooks.""" |
| 247 | + apify_client_async_patcher.patch('actor', 'call', return_value=fake_actor_run) |
| 248 | + |
| 249 | + async with Actor: |
| 250 | + await Actor.call( |
| 251 | + 'some-actor-id', |
| 252 | + webhooks=[Webhook(event_types=[WebhookEventType.ACTOR_RUN_SUCCEEDED], request_url='https://example.com')], |
| 253 | + ) |
| 254 | + |
| 255 | + assert len(apify_client_async_patcher.calls['actor']['call']) == 1 |
| 256 | + |
| 257 | + |
| 258 | +async def test_call_actor_with_timedelta_timeout( |
| 259 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 260 | +) -> None: |
| 261 | + """Test that call() accepts a timedelta timeout.""" |
| 262 | + apify_client_async_patcher.patch('actor', 'call', return_value=fake_actor_run) |
| 263 | + |
| 264 | + async with Actor: |
| 265 | + await Actor.call('some-actor-id', timeout=timedelta(seconds=120)) |
| 266 | + |
| 267 | + assert len(apify_client_async_patcher.calls['actor']['call']) == 1 |
| 268 | + |
| 269 | + |
| 270 | +async def test_call_actor_with_remaining_time_deprecation( |
| 271 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 272 | +) -> None: |
| 273 | + """Test that call() with RemainingTime emits deprecation warning.""" |
| 274 | + apify_client_async_patcher.patch('actor', 'call', return_value=fake_actor_run) |
| 275 | + |
| 276 | + async with Actor: |
| 277 | + with warnings.catch_warnings(record=True) as w: |
| 278 | + warnings.simplefilter('always') |
| 279 | + await Actor.call('some-actor-id', timeout='RemainingTime') |
| 280 | + deprecation_warnings = [x for x in w if issubclass(x.category, DeprecationWarning)] |
| 281 | + assert len(deprecation_warnings) == 1 |
| 282 | + assert 'RemainingTime' in str(deprecation_warnings[0].message) |
| 283 | + |
| 284 | + |
| 285 | +async def test_call_actor_with_invalid_timeout( |
| 286 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 287 | +) -> None: |
| 288 | + """Test that call() raises ValueError for invalid timeout.""" |
| 289 | + apify_client_async_patcher.patch('actor', 'call', return_value=fake_actor_run) |
| 290 | + |
| 291 | + async with Actor: |
| 292 | + with pytest.raises(ValueError, match='Invalid timeout'): |
| 293 | + await Actor.call('some-actor-id', timeout='invalid') # type: ignore[arg-type] |
| 294 | + |
| 295 | + |
| 296 | +async def test_call_task_with_webhooks( |
| 297 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 298 | +) -> None: |
| 299 | + """Test that call_task() correctly serializes webhooks.""" |
| 300 | + apify_client_async_patcher.patch('task', 'call', return_value=fake_actor_run) |
| 301 | + |
| 302 | + async with Actor: |
| 303 | + await Actor.call_task( |
| 304 | + 'some-task-id', |
| 305 | + webhooks=[Webhook(event_types=[WebhookEventType.ACTOR_RUN_SUCCEEDED], request_url='https://example.com')], |
| 306 | + ) |
| 307 | + |
| 308 | + assert len(apify_client_async_patcher.calls['task']['call']) == 1 |
| 309 | + |
| 310 | + |
| 311 | +async def test_call_task_with_timedelta_timeout( |
| 312 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 313 | +) -> None: |
| 314 | + """Test that call_task() accepts a timedelta timeout.""" |
| 315 | + apify_client_async_patcher.patch('task', 'call', return_value=fake_actor_run) |
| 316 | + |
| 317 | + async with Actor: |
| 318 | + await Actor.call_task('some-task-id', timeout=timedelta(seconds=120)) |
| 319 | + |
| 320 | + assert len(apify_client_async_patcher.calls['task']['call']) == 1 |
| 321 | + |
| 322 | + |
| 323 | +async def test_call_task_with_invalid_timeout( |
| 324 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 325 | +) -> None: |
| 326 | + """Test that call_task() raises ValueError for invalid timeout.""" |
| 327 | + apify_client_async_patcher.patch('task', 'call', return_value=fake_actor_run) |
| 328 | + |
| 329 | + async with Actor: |
| 330 | + with pytest.raises(ValueError, match='Invalid timeout'): |
| 331 | + await Actor.call_task('some-task-id', timeout='invalid') # type: ignore[arg-type] |
| 332 | + |
| 333 | + |
| 334 | +async def test_abort_with_status_message( |
| 335 | + apify_client_async_patcher: ApifyClientAsyncPatcher, fake_actor_run: dict |
| 336 | +) -> None: |
| 337 | + """Test that abort() updates status message before aborting.""" |
| 338 | + apify_client_async_patcher.patch('run', 'update', return_value=fake_actor_run) |
| 339 | + apify_client_async_patcher.patch('run', 'abort', return_value=fake_actor_run) |
| 340 | + |
| 341 | + async with Actor: |
| 342 | + await Actor.abort('run-id', status_message='Aborting due to error') |
| 343 | + |
| 344 | + assert len(apify_client_async_patcher.calls['run']['update']) == 1 |
| 345 | + assert len(apify_client_async_patcher.calls['run']['abort']) == 1 |
| 346 | + |
| 347 | + |
| 348 | +async def test_get_remaining_time_warns_when_not_at_home(caplog: pytest.LogCaptureFixture) -> None: |
| 349 | + """Test that _get_remaining_time logs warning when not at home.""" |
| 350 | + caplog.set_level('WARNING') |
| 351 | + async with Actor: |
| 352 | + # Actor is not at home, so _get_remaining_time should return None and log warning |
| 353 | + result = Actor._get_remaining_time() |
| 354 | + assert result is None |
| 355 | + assert any('inherit' in msg or 'RemainingTime' in msg for msg in caplog.messages) |
0 commit comments