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 test for storage actor methods #39

Merged
merged 6 commits into from
Jan 31, 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
57 changes: 47 additions & 10 deletions tests/unit/actor/test_actor_dataset.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@

import pytest

from apify import Actor
from apify.consts import ApifyEnvVars
from apify.memory_storage import MemoryStorage

# NOTE: We only test the dataset methond available on Actor class/instance. Actual tests for the implementations are in storages/.


class TestActorOpenDataset:

async def test_throws_without_init(self) -> None:
with pytest.raises(RuntimeError):
await Actor.open_dataset()

async def test_same_references(self) -> None:
async with Actor:
dataset1 = await Actor.open_dataset()
dataset2 = await Actor.open_dataset()
assert dataset1 is dataset2
dataset_name = 'non-default'
dataset_named1 = await Actor.open_dataset(dataset_name)
dataset_named2 = await Actor.open_dataset(dataset_name)
assert dataset_named1 is dataset_named2

async def test_open_datatset_based_env_var(
self,
monkeypatch: pytest.MonkeyPatch,
memory_storage: MemoryStorage,
) -> None:
default_dataset_name = 'my-new-default-name'
await memory_storage.datasets().get_or_create(name=default_dataset_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line even needed?

monkeypatch.setenv(ApifyEnvVars.DEFAULT_DATASET_ID, default_dataset_name)
async with Actor:
ddt = await Actor.open_dataset()
assert ddt._name == default_dataset_name
await memory_storage.dataset(ddt._id).delete()

# NOTE: We only test the references here. Actual tests for the implementations are in storages/

class TestActorPushData:

async def test_same_references() -> None:
async with Actor:
dataset1 = await Actor.open_dataset()
dataset2 = await Actor.open_dataset()
assert dataset1 is dataset2
dataset_name = 'non-default'
dataset_named1 = await Actor.open_dataset(dataset_name)
dataset_named2 = await Actor.open_dataset(dataset_name)
assert dataset_named1 is dataset_named2
async def test_push_data(self) -> None:
async with Actor() as my_actor:
dataset = await my_actor.open_dataset()
desired_item_count = 100
await dataset.push_data([{'id': i} for i in range(desired_item_count)])
dataset_info = await dataset.get_info()
assert dataset_info is not None
list_page = await dataset.get_data(limit=desired_item_count)
assert list_page.items[0]['id'] == 0
assert list_page.items[-1]['id'] == desired_item_count - 1
50 changes: 40 additions & 10 deletions tests/unit/actor/test_actor_key_value_store.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
import pytest

from apify import Actor
from apify._utils import _json_dumps
from apify.memory_storage import MemoryStorage


# NOTE: We only test the key-value store methond available on Actor class/instance. Actual tests for the implementations are in storages/.
class TestOpenKeyValueStore:

async def test_same_references(self) -> None:
async with Actor:
kvs1 = await Actor.open_key_value_store()
kvs2 = await Actor.open_key_value_store()
assert kvs1 is kvs2
kvs_name = 'non-default'
kvs_named1 = await Actor.open_key_value_store(kvs_name)
kvs_named2 = await Actor.open_key_value_store(kvs_name)
assert kvs_named1 is kvs_named2


class TestKeyValueStoreOnActor:

# NOTE: We only test the references here. Actual tests for the implementations are in storages/
async def test_throws_without_init(self) -> None:
with pytest.raises(RuntimeError):
await Actor.open_key_value_store()

async def test_get_set_value(self) -> None:
test_key = 'test_key'
test_value = 'test_value'
test_content_type = 'text/plain'
async with Actor() as my_actor:
await my_actor.set_value(key=test_key, value=test_value, content_type=test_content_type)
value = await my_actor.get_value(key=test_key)
assert value == test_value

async def test_same_references() -> None:
async with Actor:
kvs1 = await Actor.open_key_value_store()
kvs2 = await Actor.open_key_value_store()
assert kvs1 is kvs2
kvs_name = 'non-default'
kvs_named1 = await Actor.open_key_value_store(kvs_name)
kvs_named2 = await Actor.open_key_value_store(kvs_name)
assert kvs_named1 is kvs_named2
async def test_get_input(self, memory_storage: MemoryStorage) -> None:
input_key = 'INPUT'
test_input = {'foo': 'bar'}
kvs_info = await memory_storage.key_value_stores().get_or_create(name='default')
await memory_storage.key_value_store(kvs_info['id']).set_record(key=input_key, value=_json_dumps(test_input), content_type='application/json')
async with Actor() as my_actor:
input = await my_actor.get_input()
assert input['foo'] == test_input['foo']
26 changes: 17 additions & 9 deletions tests/unit/actor/test_actor_request_queue.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import pytest

from apify import Actor

# NOTE: We only test the references here. Actual tests for the implementations are in storages/


async def test_same_references() -> None:
async with Actor:
rq1 = await Actor.open_request_queue()
rq2 = await Actor.open_request_queue()
assert rq1 is rq2
rq_name = 'non-default'
rq_named1 = await Actor.open_request_queue(rq_name)
rq_named2 = await Actor.open_request_queue(rq_name)
assert rq_named1 is rq_named2
class TestActorOpenRequestQueue:

async def test_throws_without_init(self) -> None:
with pytest.raises(RuntimeError):
await Actor.open_request_queue()

async def test_same_references(self) -> None:
async with Actor:
rq1 = await Actor.open_request_queue()
rq2 = await Actor.open_request_queue()
assert rq1 is rq2
rq_name = 'non-default'
rq_named1 = await Actor.open_request_queue(rq_name)
rq_named2 = await Actor.open_request_queue(rq_name)
assert rq_named1 is rq_named2
4 changes: 1 addition & 3 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def get_storage_client() -> 'MemoryStorage':
monkeypatch.setattr(StorageClientManager, 'get_storage_client', get_storage_client)


# TODO: decide if this is worth maintaining
# We could just mock the Apify API HTTP responses with respx and get the same results
# (but this was fun to write!)
# This class is used to patch the ApifyClientAsync methods to return a fixed value or be replaced with another method.
class ApifyClientAsyncPatcher:
def __init__(self, monkeypatch: pytest.MonkeyPatch) -> None:
self.monkeypatch = monkeypatch
Expand Down
Loading