Skip to content
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 the rest unit tests for actor #40

Merged
merged 4 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions tests/unit/actor/test_actor_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import pytest

from apify import Actor
from apify.consts import ApifyEnvVars
from apify_client import ApifyClientAsync
from apify_client.consts import WebhookEventType

from ..conftest import ApifyClientAsyncPatcher


class TestActorNewClient:

async def test_actor_new_client_config(self, monkeypatch: pytest.MonkeyPatch) -> None:
token = 'my-token'
monkeypatch.setenv(ApifyEnvVars.TOKEN, token)
my_actor = Actor()
await my_actor.init()
client = my_actor.new_client()
assert type(client) == ApifyClientAsync
assert client.token == token
passed_token = 'my-passed-token'
client_with_token = my_actor.new_client(token=passed_token)
assert type(client_with_token) == ApifyClientAsync
assert client_with_token.token == passed_token
await my_actor.exit()


class TestActorCallStartAbortActor:

async def test_actor_call(self, apify_client_async_patcher: ApifyClientAsyncPatcher) -> None:
apify_client_async_patcher.patch('actor', 'call', return_value=None)
actor_id = 'some-actor-id'
my_actor = Actor()
await my_actor.init()
await my_actor.call(actor_id)
assert len(apify_client_async_patcher.calls['actor']['call']) == 1
# The first argument is ActorClientAsync, which was called, let's check its id.
assert apify_client_async_patcher.calls['actor']['call'][0][0][0].resource_id == actor_id
drobnikj marked this conversation as resolved.
Show resolved Hide resolved
await my_actor.exit()

async def test_actor_call_task(self, apify_client_async_patcher: ApifyClientAsyncPatcher) -> None:
apify_client_async_patcher.patch('task', 'call', return_value=None)
task_id = 'some-task-id'
my_actor = Actor()
await my_actor.init()
await my_actor.call_task(task_id)
assert len(apify_client_async_patcher.calls['task']['call']) == 1
assert apify_client_async_patcher.calls['task']['call'][0][0][0].resource_id == task_id
await my_actor.exit()

async def test_actor_start(self, apify_client_async_patcher: ApifyClientAsyncPatcher) -> None:
apify_client_async_patcher.patch('actor', 'start', return_value=None)
actor_id = 'some-id'
my_actor = Actor()
await my_actor.init()
await my_actor.start(actor_id)
assert len(apify_client_async_patcher.calls['actor']['start']) == 1
assert apify_client_async_patcher.calls['actor']['start'][0][0][0].resource_id == actor_id
await my_actor.exit()

async def test_actor_abort(self, apify_client_async_patcher: ApifyClientAsyncPatcher) -> None:
apify_client_async_patcher.patch('run', 'abort', return_value=None)
run_id = 'some-run-id'
my_actor = Actor()
await my_actor.init()
await my_actor.abort(run_id)
assert len(apify_client_async_patcher.calls['run']['abort']) == 1
assert apify_client_async_patcher.calls['run']['abort'][0][0][0].resource_id == run_id
await my_actor.exit()


class TestActorMethodsWorksOnlyOnPlatform:
# NOTE: These medhods will be tested properly using integrations tests.

async def test_actor_metamorpth_not_work_locally(self, capfd: pytest.CaptureFixture) -> None:
async with Actor() as my_actor:
await my_actor.metamorph('random-id')
out, err = capfd.readouterr()
assert 'Actor.metamorph() is only supported when running on the Apify platform.' in out

async def test_actor_reboot_not_work_locally(self, capfd: pytest.CaptureFixture) -> None:
async with Actor() as my_actor:
await my_actor.reboot()
out, err = capfd.readouterr()
assert 'Actor.reboot() is only supported when running on the Apify platform.' in out

async def test_actor_add_webhook_not_work_locally(self, capfd: pytest.CaptureFixture) -> None:
async with Actor() as my_actor:
await my_actor.add_webhook(event_types=[WebhookEventType.ACTOR_BUILD_ABORTED], request_url='https://example.com')
out, err = capfd.readouterr()
assert 'Actor.add_webhook() is only supported when running on the Apify platform.' in out

async def test_actor_set_status_message_not_work_locally(self, capfd: pytest.CaptureFixture) -> None:
async with Actor() as my_actor:
await my_actor.set_status_message('test')
out, err = capfd.readouterr()
assert 'Actor.set_status_message() is only supported when running on the Apify platform.' in out
30 changes: 22 additions & 8 deletions tests/unit/actor/test_actor_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

from apify import Actor
from apify.consts import ActorEventType, ApifyEnvVars
from apify_client import ApifyClientAsync


class TestActorInit:

async def test_async_with_actor_properly_initialize(self) -> None:
async with Actor:
assert Actor._get_default_instance()._is_initialized
# TODO: More checks
assert Actor._get_default_instance()._is_initialized is False

async def test_actor_init(self) -> None:
Expand Down Expand Up @@ -77,16 +77,14 @@ async def test_raise_on_exit_witout_init(self) -> None:
class TestActorFail:

async def test_with_actor_fail(self) -> None:
my_actr = Actor()
async with my_actr:
assert my_actr._is_initialized
await my_actr.fail()
assert my_actr._is_initialized is False
async with Actor() as my_actor:
assert my_actor._is_initialized
await my_actor.fail()
assert my_actor._is_initialized is False

async def test_with_actor_failed(self) -> None:
my_actor = Actor()
try:
async with my_actor:
async with Actor() as my_actor:
assert my_actor._is_initialized
raise Exception('Failed')
except Exception:
Expand Down Expand Up @@ -139,3 +137,19 @@ async def actor_function() -> str:

returned_value = await my_actor.main(actor_function)
assert returned_value == expected_string

class TestActorNewClient:

async def test_actor_new_client_config(self, monkeypatch: pytest.MonkeyPatch) -> None:
token = 'my-token'
monkeypatch.setenv(ApifyEnvVars.TOKEN, token)
my_actor = Actor()
await my_actor.init()
client = my_actor.new_client()
assert type(client) == ApifyClientAsync
assert client.token == token
passed_token = 'my-passed-token'
client_with_token = my_actor.new_client(token=passed_token)
assert type(client_with_token) == ApifyClientAsync
assert client_with_token.token == passed_token
await my_actor.exit()