Skip to content

Commit 7911c48

Browse files
committed
Update tests
1 parent c2c8ca5 commit 7911c48

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/apify/storage_clients/_apify/_utils.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ def __init__(self, storage_type: _StorageT, alias: str, token: str, api_url: str
3838
self.token = token
3939

4040
@classmethod
41-
def get_additional_cache_key(cls, api_url: str, token: str) -> str:
41+
def get_additional_cache_key(cls, api_url: str, token: str, *, encrypted: bool = False) -> str:
42+
if not encrypted:
43+
return cls.ADDITIONAL_CACHE_KEY_SEPARATOR.join([api_url, token])
44+
4245
encryption_key = Configuration.get_global_configuration().token
4346
if encryption_key is not None:
4447
encrypted_token = cls._create_fernet(encryption_key).encrypt(token.encode()).decode()
@@ -47,7 +50,7 @@ def get_additional_cache_key(cls, api_url: str, token: str) -> str:
4750

4851
@property
4952
def additional_cache_key(self) -> str:
50-
return self.get_additional_cache_key(self.api_url, self.token)
53+
return self.get_additional_cache_key(self.api_url, self.token, encrypted=False)
5154

5255
@classmethod
5356
def from_exported_string(cls, alias_as_string: str) -> _Alias:
@@ -67,8 +70,14 @@ def from_exported_string(cls, alias_as_string: str) -> _Alias:
6770

6871
return cls(storage_type=storage_map[storage_class_name], alias=alias, api_url=api_url, token=token)
6972

70-
def __str__(self) -> str:
71-
return self.ALIAS_SEPARATOR.join([self.storage_type.__name__, self.alias, self.additional_cache_key])
73+
def get_storage_key(self) -> str:
74+
return self.ALIAS_SEPARATOR.join(
75+
[
76+
self.storage_type.__name__,
77+
self.alias,
78+
self.get_additional_cache_key(api_url=self.api_url, token=self.token, encrypted=True),
79+
]
80+
)
7281

7382
@staticmethod
7483
def _create_fernet(token: str) -> Fernet:
@@ -91,10 +100,11 @@ async def store_mapping_to_apify_kvs(self, storage_id: str) -> None:
91100
record = record['value']
92101

93102
# Update or create the record with the new alias mapping
103+
94104
if isinstance(record, dict):
95-
record[str(self)] = storage_id
105+
record[self.get_storage_key()] = storage_id
96106
else:
97-
record = {str(self): storage_id}
107+
record = {self.get_storage_key(): storage_id}
98108

99109
# Store the mapping back in the KVS.
100110
await default_kvs_client.set_record(_ALIAS_MAPPING_KEY, record)

tests/unit/actor/test_apify_storage.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from datetime import datetime, timezone
12
from unittest import mock
23
from unittest.mock import AsyncMock
34

45
import pytest
56

7+
from crawlee.storage_clients.models import StorageMetadata
68
from crawlee.storages import Dataset, KeyValueStore, RequestQueue
79
from crawlee.storages._base import Storage
810

@@ -23,7 +25,12 @@ async def test_get_additional_cache_key(
2325
storage: Storage, _storage_client: ApifyDatasetClient | ApifyKeyValueStoreClient | ApifyRequestQueueClient
2426
) -> None:
2527
"""Test that Storages based on `ApifyStorageClient` include `token` and `api_base_url` in additional cache key."""
26-
storage_names = iter(['1', '2', '3', '1', '3'])
28+
29+
def create_metadata(id: str) -> StorageMetadata:
30+
now = datetime.now(tz=timezone.utc)
31+
return StorageMetadata(id=id, name=None, accessed_at=now, created_at=now, modified_at=now)
32+
33+
storage_ids = iter(['1', '2', '3', '1', '3'])
2734

2835
apify_storage_client = ApifyStorageClient()
2936

@@ -34,8 +41,9 @@ async def test_get_additional_cache_key(
3441
config_4 = Configuration(token='a')
3542
config_5 = Configuration(token='a', api_base_url='https://super_custom_api.com')
3643

37-
mocked_open = AsyncMock(spec=_storage_client.open)
38-
mocked_open.get_metadata = AsyncMock(storage_names)
44+
mocked_client = AsyncMock(spec=type[_storage_client])
45+
mocked_client.get_metadata = AsyncMock(side_effect=lambda: create_metadata(next(storage_ids)))
46+
mocked_open = AsyncMock(spec=_storage_client.open, return_value=mocked_client)
3947

4048
with mock.patch.object(_storage_client, 'open', mocked_open):
4149
storage_1 = await storage.open(storage_client=apify_storage_client, configuration=config_1)

0 commit comments

Comments
 (0)