Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b89bbcf

Browse filesBrowse files
authored
feat: add test for storage actor methods (#39)
1 parent 8f55d46 commit b89bbcf
Copy full SHA for b89bbcf

File tree

Expand file treeCollapse file tree

4 files changed

+105
-32
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+105
-32
lines changed
+47-10Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
11

2+
import pytest
3+
24
from apify import Actor
5+
from apify.consts import ApifyEnvVars
6+
from apify.memory_storage import MemoryStorage
7+
8+
# NOTE: We only test the dataset methond available on Actor class/instance. Actual tests for the implementations are in storages/.
9+
10+
11+
class TestActorOpenDataset:
12+
13+
async def test_throws_without_init(self) -> None:
14+
with pytest.raises(RuntimeError):
15+
await Actor.open_dataset()
16+
17+
async def test_same_references(self) -> None:
18+
async with Actor:
19+
dataset1 = await Actor.open_dataset()
20+
dataset2 = await Actor.open_dataset()
21+
assert dataset1 is dataset2
22+
dataset_name = 'non-default'
23+
dataset_named1 = await Actor.open_dataset(dataset_name)
24+
dataset_named2 = await Actor.open_dataset(dataset_name)
25+
assert dataset_named1 is dataset_named2
26+
27+
async def test_open_datatset_based_env_var(
28+
self,
29+
monkeypatch: pytest.MonkeyPatch,
30+
memory_storage: MemoryStorage,
31+
) -> None:
32+
default_dataset_name = 'my-new-default-name'
33+
await memory_storage.datasets().get_or_create(name=default_dataset_name)
34+
monkeypatch.setenv(ApifyEnvVars.DEFAULT_DATASET_ID, default_dataset_name)
35+
async with Actor:
36+
ddt = await Actor.open_dataset()
37+
assert ddt._name == default_dataset_name
38+
await memory_storage.dataset(ddt._id).delete()
339

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

41+
class TestActorPushData:
642

7-
async def test_same_references() -> None:
8-
async with Actor:
9-
dataset1 = await Actor.open_dataset()
10-
dataset2 = await Actor.open_dataset()
11-
assert dataset1 is dataset2
12-
dataset_name = 'non-default'
13-
dataset_named1 = await Actor.open_dataset(dataset_name)
14-
dataset_named2 = await Actor.open_dataset(dataset_name)
15-
assert dataset_named1 is dataset_named2
43+
async def test_push_data(self) -> None:
44+
async with Actor() as my_actor:
45+
dataset = await my_actor.open_dataset()
46+
desired_item_count = 100
47+
await dataset.push_data([{'id': i} for i in range(desired_item_count)])
48+
dataset_info = await dataset.get_info()
49+
assert dataset_info is not None
50+
list_page = await dataset.get_data(limit=desired_item_count)
51+
assert list_page.items[0]['id'] == 0
52+
assert list_page.items[-1]['id'] == desired_item_count - 1
+40-10Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
1+
import pytest
2+
13
from apify import Actor
4+
from apify._utils import _json_dumps
5+
from apify.memory_storage import MemoryStorage
6+
7+
8+
# NOTE: We only test the key-value store methond available on Actor class/instance. Actual tests for the implementations are in storages/.
9+
class TestOpenKeyValueStore:
10+
11+
async def test_same_references(self) -> None:
12+
async with Actor:
13+
kvs1 = await Actor.open_key_value_store()
14+
kvs2 = await Actor.open_key_value_store()
15+
assert kvs1 is kvs2
16+
kvs_name = 'non-default'
17+
kvs_named1 = await Actor.open_key_value_store(kvs_name)
18+
kvs_named2 = await Actor.open_key_value_store(kvs_name)
19+
assert kvs_named1 is kvs_named2
20+
21+
22+
class TestKeyValueStoreOnActor:
223

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

28+
async def test_get_set_value(self) -> None:
29+
test_key = 'test_key'
30+
test_value = 'test_value'
31+
test_content_type = 'text/plain'
32+
async with Actor() as my_actor:
33+
await my_actor.set_value(key=test_key, value=test_value, content_type=test_content_type)
34+
value = await my_actor.get_value(key=test_key)
35+
assert value == test_value
536

6-
async def test_same_references() -> None:
7-
async with Actor:
8-
kvs1 = await Actor.open_key_value_store()
9-
kvs2 = await Actor.open_key_value_store()
10-
assert kvs1 is kvs2
11-
kvs_name = 'non-default'
12-
kvs_named1 = await Actor.open_key_value_store(kvs_name)
13-
kvs_named2 = await Actor.open_key_value_store(kvs_name)
14-
assert kvs_named1 is kvs_named2
37+
async def test_get_input(self, memory_storage: MemoryStorage) -> None:
38+
input_key = 'INPUT'
39+
test_input = {'foo': 'bar'}
40+
kvs_info = await memory_storage.key_value_stores().get_or_create(name='default')
41+
await memory_storage.key_value_store(kvs_info['id']).set_record(key=input_key, value=_json_dumps(test_input), content_type='application/json')
42+
async with Actor() as my_actor:
43+
input = await my_actor.get_input()
44+
assert input['foo'] == test_input['foo']
+17-9Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
import pytest
2+
13
from apify import Actor
24

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

57

6-
async def test_same_references() -> None:
7-
async with Actor:
8-
rq1 = await Actor.open_request_queue()
9-
rq2 = await Actor.open_request_queue()
10-
assert rq1 is rq2
11-
rq_name = 'non-default'
12-
rq_named1 = await Actor.open_request_queue(rq_name)
13-
rq_named2 = await Actor.open_request_queue(rq_name)
14-
assert rq_named1 is rq_named2
8+
class TestActorOpenRequestQueue:
9+
10+
async def test_throws_without_init(self) -> None:
11+
with pytest.raises(RuntimeError):
12+
await Actor.open_request_queue()
13+
14+
async def test_same_references(self) -> None:
15+
async with Actor:
16+
rq1 = await Actor.open_request_queue()
17+
rq2 = await Actor.open_request_queue()
18+
assert rq1 is rq2
19+
rq_name = 'non-default'
20+
rq_named1 = await Actor.open_request_queue(rq_name)
21+
rq_named2 = await Actor.open_request_queue(rq_name)
22+
assert rq_named1 is rq_named2

‎tests/unit/conftest.py

Copy file name to clipboardExpand all lines: tests/unit/conftest.py
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def get_storage_client() -> 'MemoryStorage':
2828
monkeypatch.setattr(StorageClientManager, 'get_storage_client', get_storage_client)
2929

3030

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.