|
| 1 | +from apify import Actor |
| 2 | + |
| 3 | +from .conftest import ActorFactory |
| 4 | + |
| 5 | + |
| 6 | +class TestActorInit: |
| 7 | + |
| 8 | + async def test_actor_init(self, make_actor: ActorFactory) -> None: |
| 9 | + async def main() -> None: |
| 10 | + my_actor = Actor() |
| 11 | + await my_actor.init() |
| 12 | + assert my_actor._is_initialized is True |
| 13 | + double_init = False |
| 14 | + try: |
| 15 | + await my_actor.init() |
| 16 | + double_init = True |
| 17 | + except RuntimeError as err: |
| 18 | + assert str(err) == 'The actor was already initialized!' |
| 19 | + except Exception as err: |
| 20 | + raise err |
| 21 | + try: |
| 22 | + await Actor.init() |
| 23 | + double_init = True |
| 24 | + except RuntimeError as err: |
| 25 | + assert str(err) == 'The actor was already initialized!' |
| 26 | + except Exception as err: |
| 27 | + raise err |
| 28 | + await my_actor.exit() |
| 29 | + assert double_init is False |
| 30 | + assert my_actor._is_initialized is False |
| 31 | + |
| 32 | + actor = await make_actor('actor-init', main_func=main) |
| 33 | + |
| 34 | + run_result = await actor.call() |
| 35 | + |
| 36 | + assert run_result is not None |
| 37 | + assert run_result['status'] == 'SUCCEEDED' |
| 38 | + |
| 39 | + async def test_async_with_actor_properly_initialize(self, make_actor: ActorFactory) -> None: |
| 40 | + async def main() -> None: |
| 41 | + async with Actor: |
| 42 | + assert Actor._get_default_instance()._is_initialized |
| 43 | + assert Actor._get_default_instance()._is_initialized is False |
| 44 | + |
| 45 | + actor = await make_actor('with-actor-init', main_func=main) |
| 46 | + |
| 47 | + run_result = await actor.call() |
| 48 | + |
| 49 | + assert run_result is not None |
| 50 | + assert run_result['status'] == 'SUCCEEDED' |
| 51 | + |
| 52 | + |
| 53 | +class TestActorExit: |
| 54 | + |
| 55 | + async def test_actor_exit_code(self, make_actor: ActorFactory) -> None: |
| 56 | + async def main() -> None: |
| 57 | + async with Actor: |
| 58 | + input = await Actor.get_input() |
| 59 | + await Actor.exit(**input) |
| 60 | + |
| 61 | + actor = await make_actor('actor-exit', main_func=main) |
| 62 | + |
| 63 | + for exit_code in [0, 1, 101]: |
| 64 | + run_result = await actor.call(run_input={'exit_code': exit_code}) |
| 65 | + assert run_result is not None |
| 66 | + assert run_result['exitCode'] == exit_code |
| 67 | + assert run_result['status'] == 'FAILED' if exit_code > 0 else 'SUCCEEDED' |
| 68 | + |
| 69 | + |
| 70 | +class TestActorFail: |
| 71 | + |
| 72 | + async def test_fail_exit_code(self, make_actor: ActorFactory) -> None: |
| 73 | + async def main() -> None: |
| 74 | + async with Actor: |
| 75 | + input = await Actor.get_input() |
| 76 | + await Actor.fail(**input) if input else await Actor.fail() |
| 77 | + |
| 78 | + actor = await make_actor('actor-fail', main_func=main) |
| 79 | + |
| 80 | + run_result = await actor.call() |
| 81 | + assert run_result is not None |
| 82 | + assert run_result['exitCode'] == 1 |
| 83 | + assert run_result['status'] == 'FAILED' |
| 84 | + |
| 85 | + for exit_code in [1, 10, 100]: |
| 86 | + run_result = await actor.call(run_input={'exit_code': exit_code}) |
| 87 | + assert run_result is not None |
| 88 | + assert run_result['exitCode'] == exit_code |
| 89 | + assert run_result['status'] == 'FAILED' |
| 90 | + |
| 91 | + async def test_with_actor_fail_correctly(self, make_actor: ActorFactory) -> None: |
| 92 | + async def main() -> None: |
| 93 | + async with Actor: |
| 94 | + raise Exception('This is a test exception') |
| 95 | + |
| 96 | + actor = await make_actor('with-actor-fail', main_func=main) |
| 97 | + run_result = await actor.call() |
| 98 | + assert run_result is not None |
| 99 | + assert run_result['exitCode'] == 91 |
| 100 | + assert run_result['status'] == 'FAILED' |
| 101 | + |
| 102 | + |
| 103 | +class TestActorMain: |
| 104 | + |
| 105 | + async def test_actor_main(self, make_actor: ActorFactory) -> None: |
| 106 | + async def main() -> None: |
| 107 | + async def actor_function() -> None: |
| 108 | + input = await Actor.get_input() |
| 109 | + if input.get('raise_exception'): |
| 110 | + raise Exception(input.get('raise_exception')) |
| 111 | + elif input.get('exit_code'): |
| 112 | + await Actor.exit(exit_code=input.get('exit_code')) |
| 113 | + elif input.get('fail'): |
| 114 | + await Actor.fail() |
| 115 | + elif input.get('set_output'): |
| 116 | + await Actor.set_value('OUTPUT', input.get('set_output')) |
| 117 | + print('Main function called') |
| 118 | + |
| 119 | + await Actor.main(actor_function) |
| 120 | + |
| 121 | + actor = await make_actor('actor-main', main_func=main) |
| 122 | + |
| 123 | + exception_run = await actor.call(run_input={'raise_exception': 'This is a test exception'}) |
| 124 | + assert exception_run is not None |
| 125 | + assert exception_run['status'] == 'FAILED' |
| 126 | + assert exception_run['exitCode'] == 91 |
| 127 | + |
| 128 | + exit_code = 10 |
| 129 | + exited_run = await actor.call(run_input={'exit_code': exit_code}) |
| 130 | + assert exited_run is not None |
| 131 | + assert exited_run['status'] == 'FAILED' |
| 132 | + assert exited_run['exitCode'] == exit_code |
| 133 | + |
| 134 | + failed_run = await actor.call(run_input={'fail': True}) |
| 135 | + assert failed_run is not None |
| 136 | + assert failed_run['status'] == 'FAILED' |
| 137 | + assert failed_run['exitCode'] == 1 |
| 138 | + |
| 139 | + test_output = {'test': 'output'} |
| 140 | + run_with_output = await actor.call(run_input={'set_output': test_output}) |
| 141 | + assert run_with_output is not None |
| 142 | + assert run_with_output['status'] == 'SUCCEEDED' |
| 143 | + output = await actor.last_run().key_value_store().get_record('OUTPUT') |
| 144 | + assert output is not None |
| 145 | + assert output['value'] == test_output |
0 commit comments