|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +from typing import cast |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
7 | 8 | from crawlee import service_locator |
8 | 9 | from crawlee.storages import Dataset, KeyValueStore, RequestQueue |
9 | 10 |
|
10 | 11 | from apify import Actor, Configuration |
| 12 | +from apify._configuration import ActorStorages |
11 | 13 | from apify.storage_clients import ApifyStorageClient, MemoryStorageClient, SmartApifyStorageClient |
| 14 | +from apify.storage_clients._apify._alias_resolving import AliasResolver |
12 | 15 |
|
13 | 16 |
|
14 | 17 | @pytest.mark.parametrize( |
@@ -125,3 +128,53 @@ async def test_actor_implicit_storage_init(apify_token: str) -> None: |
125 | 128 | assert await Actor.open_dataset() is not await Actor.open_dataset(force_cloud=True) |
126 | 129 | assert await Actor.open_key_value_store() is not await Actor.open_key_value_store(force_cloud=True) |
127 | 130 | assert await Actor.open_request_queue() is not await Actor.open_request_queue(force_cloud=True) |
| 131 | + |
| 132 | + |
| 133 | +async def test_actor_storages_alias_resolving(apify_token: str) -> None: |
| 134 | + """Test that `AliasResolver.register_aliases` correctly updates default KVS with Actor storages.""" |
| 135 | + |
| 136 | + # Actor storages |
| 137 | + datasets = {'default': 'default_dataset_id', 'custom': 'custom_dataset_id'} |
| 138 | + request_queues = {'default': 'default_dataset_id', 'custom': 'custom_dataset_id'} |
| 139 | + key_value_stores = {'default': 'default_dataset_id', 'custom': 'custom_dataset_id'} |
| 140 | + |
| 141 | + # Set up the configuration and storage client for the test |
| 142 | + configuration = Configuration( |
| 143 | + default_key_value_store_id='default_kvs_id', |
| 144 | + token=apify_token, |
| 145 | + actor_storages=ActorStorages( |
| 146 | + datasets=datasets, request_queues=request_queues, key_value_stores=key_value_stores |
| 147 | + ), |
| 148 | + ) |
| 149 | + storage_client = ApifyStorageClient() |
| 150 | + service_locator.set_configuration(configuration) |
| 151 | + service_locator.set_storage_client(storage_client) |
| 152 | + |
| 153 | + client_cache_key = cast('tuple', storage_client.get_storage_client_cache_key(configuration))[-1] |
| 154 | + # Add some unrelated pre-existing alias mapping (it should be preserved after registering aliases) |
| 155 | + pre_existing_mapping = {f'KeyValueStore,pre_existing_alias,{client_cache_key}': 'pre_existing_id'} |
| 156 | + |
| 157 | + default_kvs = await KeyValueStore.open(configuration=configuration, storage_client=storage_client) |
| 158 | + await default_kvs.set_value(AliasResolver._ALIAS_MAPPING_KEY, pre_existing_mapping) |
| 159 | + |
| 160 | + # Construct the expected mapping |
| 161 | + expected_mapping = {} |
| 162 | + for storage_type, storage_map in ( |
| 163 | + ('Dataset', datasets), |
| 164 | + ('KeyValueStore', key_value_stores), |
| 165 | + ('RequestQueue', request_queues), |
| 166 | + ): |
| 167 | + for storage_alias, storage_id in storage_map.items(): |
| 168 | + expected_mapping[ |
| 169 | + ','.join( |
| 170 | + (storage_type, '__default__' if storage_alias == 'default' else storage_alias, client_cache_key) |
| 171 | + ) |
| 172 | + ] = storage_id |
| 173 | + expected_mapping.update(pre_existing_mapping) |
| 174 | + |
| 175 | + try: |
| 176 | + configuration.default_key_value_store_id = default_kvs.id |
| 177 | + await AliasResolver.register_aliases(configuration=configuration) |
| 178 | + assert await default_kvs.get_value(AliasResolver._ALIAS_MAPPING_KEY) == expected_mapping |
| 179 | + finally: |
| 180 | + await default_kvs.drop() |
0 commit comments