|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from decimal import Decimal |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +import pytest_asyncio |
| 8 | + |
| 9 | +from apify_shared.consts import ActorJobStatus |
| 10 | + |
| 11 | +from apify import Actor |
| 12 | +from apify._models import ActorRun |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from apify_client import ApifyClientAsync |
| 16 | + from apify_client.clients import ActorClientAsync |
| 17 | + |
| 18 | + from .conftest import MakeActorFunction, RunActorFunction |
| 19 | + |
| 20 | + |
| 21 | +@pytest_asyncio.fixture(scope='module', loop_scope='module') |
| 22 | +async def ppe_actor_build(make_actor: MakeActorFunction) -> str: |
| 23 | + async def main() -> None: |
| 24 | + from dataclasses import asdict |
| 25 | + |
| 26 | + async with Actor: |
| 27 | + charge_result = await Actor.charge( |
| 28 | + event_name='foobar', |
| 29 | + count=4, |
| 30 | + ) |
| 31 | + Actor.log.info('Charged', extra=asdict(charge_result)) |
| 32 | + |
| 33 | + actor_client = await make_actor('ppe', main_func=main) |
| 34 | + |
| 35 | + await actor_client.update( |
| 36 | + pricing_infos=[ |
| 37 | + { |
| 38 | + 'pricingModel': 'PAY_PER_EVENT', |
| 39 | + 'pricingPerEvent': { |
| 40 | + 'actorChargeEvents': { |
| 41 | + 'foobar': { |
| 42 | + 'eventTitle': 'Foo bar', |
| 43 | + 'eventPriceUsd': 0.1, |
| 44 | + 'eventDescription': 'Foo foo bar bar', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + ] |
| 50 | + ) |
| 51 | + |
| 52 | + actor = await actor_client.get() |
| 53 | + |
| 54 | + assert actor is not None |
| 55 | + return str(actor['id']) |
| 56 | + |
| 57 | + |
| 58 | +@pytest_asyncio.fixture(scope='function', loop_scope='module') |
| 59 | +async def ppe_actor( |
| 60 | + ppe_actor_build: str, |
| 61 | + apify_client_async: ApifyClientAsync, |
| 62 | +) -> ActorClientAsync: |
| 63 | + return apify_client_async.actor(ppe_actor_build) |
| 64 | + |
| 65 | + |
| 66 | +async def test_actor_charge_basic( |
| 67 | + ppe_actor: ActorClientAsync, |
| 68 | + run_actor: RunActorFunction, |
| 69 | + apify_client_async: ApifyClientAsync, |
| 70 | +) -> None: |
| 71 | + run = await run_actor(ppe_actor) |
| 72 | + |
| 73 | + # Wait until the platform gets its act together and refetch |
| 74 | + await asyncio.sleep(6) |
| 75 | + updated_run = await apify_client_async.run(run.id).get() |
| 76 | + run = ActorRun.model_validate(updated_run) |
| 77 | + |
| 78 | + assert run.status == ActorJobStatus.SUCCEEDED |
| 79 | + assert run.charged_event_counts == {'foobar': 4} |
| 80 | + |
| 81 | + |
| 82 | +async def test_actor_charge_limit( |
| 83 | + ppe_actor: ActorClientAsync, |
| 84 | + run_actor: RunActorFunction, |
| 85 | + apify_client_async: ApifyClientAsync, |
| 86 | +) -> None: |
| 87 | + run = await run_actor(ppe_actor, max_total_charge_usd=Decimal('0.2')) |
| 88 | + |
| 89 | + # Wait until the platform gets its act together and refetch |
| 90 | + await asyncio.sleep(6) |
| 91 | + updated_run = await apify_client_async.run(run.id).get() |
| 92 | + run = ActorRun.model_validate(updated_run) |
| 93 | + |
| 94 | + assert run.status == ActorJobStatus.SUCCEEDED |
| 95 | + assert run.charged_event_counts == {'foobar': 2} |
0 commit comments