-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add a few integration tests #48
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import asyncio | ||
|
||
from apify import Actor | ||
from apify.consts import ActorEventType | ||
|
||
from .conftest import ActorFactory | ||
|
||
|
||
class TestActorEvents: | ||
|
||
async def test_interval_events(self, make_actor: ActorFactory) -> None: | ||
async def main() -> None: | ||
import os | ||
|
||
from apify.consts import ActorEventType, ApifyEnvVars | ||
|
||
os.environ[ApifyEnvVars.PERSIST_STATE_INTERVAL_MILLIS] = '900' | ||
|
||
def on_event(event_type): # type: ignore | ||
async def log_event(data): # type: ignore | ||
await Actor.push_data({'event_type': event_type, 'data': data}) | ||
return log_event | ||
|
||
async with Actor: | ||
Actor.on(ActorEventType.SYSTEM_INFO, on_event(ActorEventType.SYSTEM_INFO)) # type: ignore | ||
Actor.on(ActorEventType.PERSIST_STATE, on_event(ActorEventType.PERSIST_STATE)) # type: ignore | ||
await asyncio.sleep(3) | ||
|
||
actor = await make_actor('actor-interval-events', main_func=main) | ||
|
||
run_result = await actor.call() | ||
|
||
assert run_result is not None | ||
assert run_result['status'] == 'SUCCEEDED' | ||
dataset_items_page = await actor.last_run().dataset().list_items() | ||
persist_state_events = [item for item in dataset_items_page.items if item['event_type'] == ActorEventType.PERSIST_STATE] | ||
system_info_events = [item for item in dataset_items_page.items if item['event_type'] == ActorEventType.SYSTEM_INFO] | ||
assert len(persist_state_events) > 2 | ||
assert len(system_info_events) > 0 | ||
|
||
async def test_off_event(self, make_actor: ActorFactory) -> None: | ||
async def main() -> None: | ||
import os | ||
|
||
from apify.consts import ActorEventType, ApifyEnvVars | ||
|
||
os.environ[ApifyEnvVars.PERSIST_STATE_INTERVAL_MILLIS] = '100' | ||
|
||
counter = 0 | ||
|
||
def count_event(data): # type: ignore | ||
nonlocal counter | ||
print(data) | ||
counter += 1 | ||
|
||
async with Actor: | ||
Actor.on(ActorEventType.PERSIST_STATE, count_event) | ||
await asyncio.sleep(0.5) | ||
assert counter > 1 | ||
last_count = counter | ||
Actor.off(ActorEventType.PERSIST_STATE, count_event) | ||
await asyncio.sleep(0.5) | ||
assert counter == last_count | ||
|
||
actor = await make_actor('actor-off-event', main_func=main) | ||
|
||
run = await actor.call() | ||
|
||
assert run is not None | ||
assert run['status'] == 'SUCCEEDED' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
from apify import Actor | ||
|
||
from .conftest import ActorFactory | ||
|
||
|
||
class TestActorInit: | ||
|
||
async def test_actor_init(self, make_actor: ActorFactory) -> None: | ||
async def main() -> None: | ||
my_actor = Actor() | ||
await my_actor.init() | ||
assert my_actor._is_initialized is True | ||
double_init = False | ||
try: | ||
await my_actor.init() | ||
double_init = True | ||
except RuntimeError as err: | ||
assert str(err) == 'The actor was already initialized!' | ||
except Exception as err: | ||
raise err | ||
try: | ||
await Actor.init() | ||
double_init = True | ||
except RuntimeError as err: | ||
assert str(err) == 'The actor was already initialized!' | ||
except Exception as err: | ||
raise err | ||
await my_actor.exit() | ||
assert double_init is False | ||
assert my_actor._is_initialized is False | ||
|
||
actor = await make_actor('actor-init', main_func=main) | ||
|
||
run_result = await actor.call() | ||
|
||
assert run_result is not None | ||
assert run_result['status'] == 'SUCCEEDED' | ||
|
||
async def test_async_with_actor_properly_initialize(self, make_actor: ActorFactory) -> None: | ||
async def main() -> None: | ||
async with Actor: | ||
assert Actor._get_default_instance()._is_initialized | ||
assert Actor._get_default_instance()._is_initialized is False | ||
|
||
actor = await make_actor('with-actor-init', main_func=main) | ||
|
||
run_result = await actor.call() | ||
|
||
assert run_result is not None | ||
assert run_result['status'] == 'SUCCEEDED' | ||
|
||
|
||
class TestActorExit: | ||
|
||
async def test_actor_exit_code(self, make_actor: ActorFactory) -> None: | ||
async def main() -> None: | ||
async with Actor: | ||
input = await Actor.get_input() | ||
await Actor.exit(**input) | ||
|
||
actor = await make_actor('actor-exit', main_func=main) | ||
|
||
for exit_code in [0, 1, 101]: | ||
run_result = await actor.call(run_input={'exit_code': exit_code}) | ||
assert run_result is not None | ||
assert run_result['exitCode'] == exit_code | ||
assert run_result['status'] == 'FAILED' if exit_code > 0 else 'SUCCEEDED' | ||
|
||
|
||
class TestActorFail: | ||
|
||
async def test_fail_exit_code(self, make_actor: ActorFactory) -> None: | ||
async def main() -> None: | ||
async with Actor: | ||
input = await Actor.get_input() | ||
await Actor.fail(**input) if input else await Actor.fail() | ||
|
||
actor = await make_actor('actor-fail', main_func=main) | ||
|
||
run_result = await actor.call() | ||
assert run_result is not None | ||
assert run_result['exitCode'] == 1 | ||
assert run_result['status'] == 'FAILED' | ||
|
||
for exit_code in [1, 10, 100]: | ||
run_result = await actor.call(run_input={'exit_code': exit_code}) | ||
assert run_result is not None | ||
assert run_result['exitCode'] == exit_code | ||
assert run_result['status'] == 'FAILED' | ||
|
||
async def test_with_actor_fail_correctly(self, make_actor: ActorFactory) -> None: | ||
async def main() -> None: | ||
async with Actor: | ||
raise Exception('This is a test exception') | ||
|
||
actor = await make_actor('with-actor-fail', main_func=main) | ||
run_result = await actor.call() | ||
assert run_result is not None | ||
assert run_result['exitCode'] == 91 | ||
assert run_result['status'] == 'FAILED' | ||
|
||
|
||
class TestActorMain: | ||
|
||
async def test_actor_main(self, make_actor: ActorFactory) -> None: | ||
async def main() -> None: | ||
async def actor_function() -> None: | ||
input = await Actor.get_input() | ||
if input.get('raise_exception'): | ||
raise Exception(input.get('raise_exception')) | ||
elif input.get('exit_code'): | ||
await Actor.exit(exit_code=input.get('exit_code')) | ||
elif input.get('fail'): | ||
await Actor.fail() | ||
elif input.get('set_output'): | ||
await Actor.set_value('OUTPUT', input.get('set_output')) | ||
print('Main function called') | ||
|
||
await Actor.main(actor_function) | ||
|
||
actor = await make_actor('actor-main', main_func=main) | ||
|
||
exception_run = await actor.call(run_input={'raise_exception': 'This is a test exception'}) | ||
assert exception_run is not None | ||
assert exception_run['status'] == 'FAILED' | ||
assert exception_run['exitCode'] == 91 | ||
|
||
exit_code = 10 | ||
exited_run = await actor.call(run_input={'exit_code': exit_code}) | ||
assert exited_run is not None | ||
assert exited_run['status'] == 'FAILED' | ||
assert exited_run['exitCode'] == exit_code | ||
|
||
failed_run = await actor.call(run_input={'fail': True}) | ||
assert failed_run is not None | ||
assert failed_run['status'] == 'FAILED' | ||
assert failed_run['exitCode'] == 1 | ||
|
||
test_output = {'test': 'output'} | ||
run_with_output = await actor.call(run_input={'set_output': test_output}) | ||
assert run_with_output is not None | ||
assert run_with_output['status'] == 'SUCCEEDED' | ||
output = await actor.last_run().key_value_store().get_record('OUTPUT') | ||
assert output is not None | ||
assert output['value'] == test_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test is in helpers, not sure why it was there as well.