diff --git a/tests/unit/actor/test_actor_dataset.py b/tests/unit/actor/test_actor_dataset.py index 624403d2..ce5a46af 100644 --- a/tests/unit/actor/test_actor_dataset.py +++ b/tests/unit/actor/test_actor_dataset.py @@ -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) + 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 diff --git a/tests/unit/actor/test_actor_key_value_store.py b/tests/unit/actor/test_actor_key_value_store.py index 420c1e34..cfc86d02 100644 --- a/tests/unit/actor/test_actor_key_value_store.py +++ b/tests/unit/actor/test_actor_key_value_store.py @@ -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'] diff --git a/tests/unit/actor/test_actor_request_queue.py b/tests/unit/actor/test_actor_request_queue.py index 8bdcf614..3e2db583 100644 --- a/tests/unit/actor/test_actor_request_queue.py +++ b/tests/unit/actor/test_actor_request_queue.py @@ -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 diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 26ec12aa..92bf463b 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -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