-
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 test for storage actor methods #39
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4760483
chore: better commets
drobnikj 9f2a882
Merge branch 'master' into feat/actor_storage_method_tests
drobnikj 8d63a05
feat: add test for storage actor methods
drobnikj 5324823
fix: Frantas PR comments
drobnikj 042c1cd
Merge branch 'master' into feat/actor_storage_method_tests
drobnikj 5742564
fix: pr review fix
drobnikj 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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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'] |
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 |
---|---|---|
@@ -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 |
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
Oops, something went wrong.
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.
Is this line even needed?