1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
+ import json
5
+ from typing import TYPE_CHECKING
4
6
7
+ from crawlee import service_locator
5
8
from crawlee ._consts import METADATA_FILENAME
6
9
7
- from apify import Configuration
8
- from apify .storage_clients ._file_system import ApifyFileSystemKeyValueStoreClient
10
+ from apify import Actor , Configuration
11
+ from apify .storage_clients ._file_system import ApifyFileSystemKeyValueStoreClient , ApifyFileSystemStorageClient
12
+
13
+ if TYPE_CHECKING :
14
+ from pathlib import Path
9
15
10
16
11
17
async def test_purge_preserves_input_file_and_metadata () -> None :
@@ -23,18 +29,21 @@ async def test_purge_preserves_input_file_and_metadata() -> None:
23
29
kvs_path = kvs_storage_client .path_to_kvs
24
30
25
31
# Create various files
26
- input_file = kvs_path / f'{ configuration .input_key } .json'
32
+ input_file = kvs_path / f'{ configuration .input_key } '
33
+ input_metadata_file = kvs_path / f'{ configuration .input_key } .{ METADATA_FILENAME } .json'
27
34
metadata_file = kvs_path / METADATA_FILENAME
28
35
regular_file1 = kvs_path / 'regular_file1.json'
29
36
regular_file2 = kvs_path / 'another_file.txt'
30
37
31
38
# Write content to files
32
39
await asyncio .to_thread (input_file .write_text , '{"test": "input"}' )
40
+ await asyncio .to_thread (input_metadata_file .write_text , 'some text content' )
33
41
await asyncio .to_thread (regular_file1 .write_text , '{"test": "data1"}' )
34
42
await asyncio .to_thread (regular_file2 .write_text , 'some text content' )
35
43
36
44
# Verify all files exist before purge
37
45
assert input_file .exists ()
46
+ assert input_metadata_file .exists ()
38
47
assert metadata_file .exists () # Should exist from client creation
39
48
assert regular_file1 .exists ()
40
49
assert regular_file2 .exists ()
@@ -53,3 +62,21 @@ async def test_purge_preserves_input_file_and_metadata() -> None:
53
62
# Verify INPUT.json content is unchanged
54
63
input_content = await asyncio .to_thread (input_file .read_text )
55
64
assert input_content == '{"test": "input"}'
65
+
66
+
67
+ async def test_pre_existing_input_used_by_actor (tmp_path : Path ) -> None :
68
+ pre_existing_input = {
69
+ 'foo' : 'bar' ,
70
+ }
71
+
72
+ configuration = Configuration .get_global_configuration ()
73
+ # Create pre-existing INPUT.json file
74
+ path_to_input = tmp_path / 'key_value_stores' / 'default'
75
+ path_to_input .mkdir (parents = True )
76
+ (path_to_input / f'{ configuration .input_key } .json' ).write_text (json .dumps (pre_existing_input ))
77
+
78
+ # Remove this line after https://github.com/apify/apify-sdk-python/pull/576
79
+ service_locator .set_storage_client (ApifyFileSystemStorageClient ())
80
+
81
+ async with Actor ():
82
+ assert pre_existing_input == await Actor .get_input ()
0 commit comments