|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
| 7 | +from crawlee.storage_clients import FileSystemStorageClient |
| 8 | +from crawlee.storage_clients._file_system import FileSystemKeyValueStoreClient |
7 | 9 | from crawlee.storage_clients.models import StorageMetadata |
8 | 10 | from crawlee.storages._base import Storage |
9 | 11 |
|
10 | | -from apify import Configuration |
| 12 | +from apify import Actor, Configuration |
11 | 13 | from apify.storage_clients import ApifyStorageClient |
12 | 14 | from apify.storage_clients._apify import ApifyDatasetClient, ApifyKeyValueStoreClient, ApifyRequestQueueClient |
| 15 | +from apify.storage_clients._file_system import ApifyFileSystemKeyValueStoreClient, ApifyFileSystemStorageClient |
13 | 16 | from apify.storages import Dataset, KeyValueStore, RequestQueue |
14 | 17 |
|
15 | 18 |
|
@@ -61,3 +64,31 @@ def create_metadata(id: str) -> StorageMetadata: |
61 | 64 | # Equivalent configuration results in same storage clients. |
62 | 65 | assert storage_1 is storage_4 |
63 | 66 | assert storage_3 is storage_5 |
| 67 | + |
| 68 | + |
| 69 | +async def test_no_double_purge_for_filesystem_storage_client() -> None: |
| 70 | + expected_value = 'some value' |
| 71 | + expected_key = 'some key' |
| 72 | + |
| 73 | + async with Actor(): |
| 74 | + await Actor.set_value(expected_key, expected_value) |
| 75 | + # RQ uses KVS under the hood for persistence, so it will try to open same default KVS as it was already opened, |
| 76 | + # but based on different client - FileSystemStorageClient. |
| 77 | + await Actor.open_request_queue() |
| 78 | + assert expected_value == await Actor.get_value(expected_key) |
| 79 | + |
| 80 | + |
| 81 | +async def test_first_filesystem_storage_client_wins() -> None: |
| 82 | + """Test that when two different FileSystemStorageClient variants are used to open the same storage, they both use |
| 83 | + the same client that was used to open the storage first""" |
| 84 | + kvs_1 = await KeyValueStore.open(storage_client=ApifyFileSystemStorageClient()) |
| 85 | + kvs_2 = await KeyValueStore.open(storage_client=FileSystemStorageClient()) |
| 86 | + |
| 87 | + kvs_3 = await KeyValueStore.open(name='a', storage_client=FileSystemStorageClient()) |
| 88 | + kvs_4 = await KeyValueStore.open(name='a', storage_client=ApifyFileSystemStorageClient()) |
| 89 | + |
| 90 | + assert kvs_1 is kvs_2 |
| 91 | + assert type(kvs_2._client) is ApifyFileSystemKeyValueStoreClient |
| 92 | + |
| 93 | + assert kvs_3 is kvs_4 |
| 94 | + assert type(kvs_4._client) is FileSystemKeyValueStoreClient |
0 commit comments