Skip to content

Commit 72c2f35

Browse files
committed
Add uni tests, WIP
TODO - how should it behave locally?
1 parent 3b36459 commit 72c2f35

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

src/apify/_actor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ async def __aenter__(self) -> Self:
204204
if not Actor.is_at_home():
205205
# Make sure that the input related KVS is initialized to ensure that the input aware client is used
206206
await self.open_key_value_store()
207-
else:
208-
# Load non-default aliased storages from configuration
209-
await AliasResolver.register_aliases(configuration=self.configuration)
207+
208+
# Load non-default aliased storages from configuration
209+
await AliasResolver.register_aliases(configuration=self.configuration)
210210
return self
211211

212212
async def __aexit__(

src/apify/_configuration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ class ActorStorages:
4444
request_queues: dict[str, str]
4545

4646

47-
def _load_storage_keys(data: None | str | dict) -> ActorStorages | None:
47+
def _load_storage_keys(data: None | str | dict | ActorStorages) -> ActorStorages | None:
4848
"""Load storage keys from environment."""
4949
if data is None:
5050
return None
51-
51+
if isinstance(data, ActorStorages):
52+
return data
5253
storage_mapping = data if isinstance(data, dict) else json.loads(data)
5354
return ActorStorages(
5455
key_value_stores=storage_mapping.get('keyValueStores', {}),

src/apify/storage_clients/_apify/_alias_resolving.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ async def register_aliases(cls, configuration: Configuration) -> None:
291291
)._storage_key
292292
] = storage_id
293293

294-
client = await cls._get_default_kvs_client(configuration=configuration)
295-
existing_mapping = ((await client.get_record(cls._ALIAS_MAPPING_KEY)) or {'value': {}}).get('value', {})
296-
await client.set_record(cls._ALIAS_MAPPING_KEY, {**existing_mapping, **configuration_mapping})
294+
if configuration.is_at_home:
295+
# Bulk update the mapping in the default KVS with the configuration mapping.
296+
client = await cls._get_default_kvs_client(configuration=configuration)
297+
existing_mapping = ((await client.get_record(cls._ALIAS_MAPPING_KEY)) or {'value': {}}).get('value', {})
298+
# Update the existing mapping with the configuration mapping.
299+
existing_mapping.update(configuration_mapping)
300+
# Store the updated mapping back in the KVS and in memory.
301+
await client.set_record(cls._ALIAS_MAPPING_KEY, existing_mapping)
302+
cls._alias_map = existing_mapping
303+
else:
304+
# Update only in-memory mapping when not at home
305+
cls._alias_map.update(configuration_mapping)

tests/unit/actor/test_configuration.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,26 @@ def test_actor_pricing_info_from_json_env_var(monkeypatch: pytest.MonkeyPatch) -
300300
config = ApifyConfiguration()
301301
assert config.actor_pricing_info is not None
302302
assert config.actor_pricing_info.pricing_model == 'PAY_PER_EVENT'
303+
304+
305+
def test_actor_storage_json_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
306+
"""Test that actor_storages_json is parsed from JSON env var."""
307+
import json
308+
309+
datasets = {"default": "default_dataset_id", "custom": "custom_dataset_id"}
310+
request_queues = {"default": "default_dataset_id", "custom": "custom_dataset_id"}
311+
key_value_stores = {"default": "default_dataset_id", "custom": "custom_dataset_id"}
312+
313+
actor_storages_json = json.dumps(
314+
{
315+
'datasets': datasets,
316+
'requestQueues': request_queues,
317+
'keyValueStores': key_value_stores,
318+
}
319+
)
320+
monkeypatch.setenv('ACTOR_STORAGES_JSON', actor_storages_json)
321+
config = ApifyConfiguration()
322+
assert config.actor_storages
323+
assert config.actor_storages.datasets == datasets
324+
assert config.actor_storages.request_queues == request_queues
325+
assert config.actor_storages.key_value_stores == key_value_stores

tests/unit/storage_clients/test_alias_resolver.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from __future__ import annotations
22

3-
from apify._configuration import Configuration
3+
from crawlee import service_locator
4+
5+
from apify import Actor
6+
from apify._configuration import Configuration, ActorStorages
7+
from apify.storage_clients import SmartApifyStorageClient, ApifyStorageClient
48
from apify.storage_clients._apify._alias_resolving import AliasResolver
59

610

@@ -76,3 +80,30 @@ async def test_get_alias_map_returns_in_memory_map() -> None:
7680
AliasResolver._alias_map = {}
7781
result = await AliasResolver._get_alias_map(config)
7882
assert result == {}
83+
84+
85+
async def test_register_aliases() -> None:
86+
"""Test that _get_alias_map loads the map from KVS when at home.
87+
88+
AliasResolver works locally only """
89+
90+
91+
datasets = {"default": "default_dataset_id", "custom": "custom_dataset_id"}
92+
request_queues = {"default": "default_dataset_id", "custom": "custom_dataset_id"}
93+
key_value_stores = {"default": "default_dataset_id", "custom": "custom_dataset_id"}
94+
95+
config = Configuration(is_at_home=False,
96+
token='test-token',
97+
actor_storages= ActorStorages(
98+
datasets = datasets,
99+
request_queues = request_queues,
100+
key_value_stores = key_value_stores
101+
),
102+
)
103+
storage_client = ApifyStorageClient()
104+
service_locator.set_storage_client(
105+
SmartApifyStorageClient(local_storage_client=storage_client, cloud_storage_client=storage_client)
106+
)
107+
async with Actor(configuration=config):
108+
d = await Actor.open_dataset(alias='default')
109+
assert d

0 commit comments

Comments
 (0)