|
13 | 13 | from filelock import FileLock
|
14 | 14 |
|
15 | 15 | from apify_client import ApifyClientAsync
|
16 |
| -from apify_shared.consts import ActorJobStatus, ActorSourceType |
| 16 | +from apify_shared.consts import ActorJobStatus, ActorSourceType, ApifyEnvVars |
| 17 | +from crawlee import service_locator |
| 18 | +from crawlee.storages import _creation_management |
17 | 19 |
|
18 | 20 | import apify._actor
|
19 | 21 | from ._utils import generate_unique_resource_name
|
|
29 | 31 | _SDK_ROOT_PATH = Path(__file__).parent.parent.parent.resolve()
|
30 | 32 |
|
31 | 33 |
|
| 34 | +@pytest.fixture |
| 35 | +def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callable[[], None]: |
| 36 | + """Prepare the testing environment by resetting the global state before each test. |
| 37 | +
|
| 38 | + This fixture ensures that the global state of the package is reset to a known baseline before each test runs. |
| 39 | + It also configures a temporary storage directory for test isolation. |
| 40 | +
|
| 41 | + Args: |
| 42 | + monkeypatch: Test utility provided by pytest for patching. |
| 43 | + tmp_path: A unique temporary directory path provided by pytest for test isolation. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + A callable that prepares the test environment. |
| 47 | + """ |
| 48 | + |
| 49 | + def _prepare_test_env() -> None: |
| 50 | + delattr(apify._actor.Actor, '__wrapped__') |
| 51 | + |
| 52 | + # Set the environment variable for the local storage directory to the temporary path. |
| 53 | + monkeypatch.setenv(ApifyEnvVars.LOCAL_STORAGE_DIR, str(tmp_path)) |
| 54 | + |
| 55 | + # Reset the flags in the service locator to indicate that no services are explicitly set. This ensures |
| 56 | + # a clean state, as services might have been set during a previous test and not reset properly. |
| 57 | + service_locator._configuration_was_set = False |
| 58 | + service_locator._storage_client_was_set = False |
| 59 | + service_locator._event_manager_was_set = False |
| 60 | + |
| 61 | + # Reset the services in the service locator. |
| 62 | + service_locator._configuration = None |
| 63 | + service_locator._event_manager = None |
| 64 | + service_locator._storage_client = None |
| 65 | + |
| 66 | + # Clear creation-related caches to ensure no state is carried over between tests. |
| 67 | + monkeypatch.setattr(_creation_management, '_cache_dataset_by_id', {}) |
| 68 | + monkeypatch.setattr(_creation_management, '_cache_dataset_by_name', {}) |
| 69 | + monkeypatch.setattr(_creation_management, '_cache_kvs_by_id', {}) |
| 70 | + monkeypatch.setattr(_creation_management, '_cache_kvs_by_name', {}) |
| 71 | + monkeypatch.setattr(_creation_management, '_cache_rq_by_id', {}) |
| 72 | + monkeypatch.setattr(_creation_management, '_cache_rq_by_name', {}) |
| 73 | + |
| 74 | + # Verify that the test environment was set up correctly. |
| 75 | + assert os.environ.get(ApifyEnvVars.LOCAL_STORAGE_DIR) == str(tmp_path) |
| 76 | + assert service_locator._configuration_was_set is False |
| 77 | + assert service_locator._storage_client_was_set is False |
| 78 | + assert service_locator._event_manager_was_set is False |
| 79 | + |
| 80 | + return _prepare_test_env |
| 81 | + |
| 82 | + |
32 | 83 | @pytest.fixture(autouse=True)
|
33 |
| -def _reset_and_patch_default_instances() -> None: |
34 |
| - """Reset the used singletons and patch the default storage client with a temporary directory. |
| 84 | +def _isolate_test_environment(prepare_test_env: Callable[[], None]) -> None: |
| 85 | + """Isolate the testing environment by resetting global state before and after each test. |
| 86 | +
|
| 87 | + This fixture ensures that each test starts with a clean slate and that any modifications during the test |
| 88 | + do not affect subsequent tests. It runs automatically for all tests. |
35 | 89 |
|
36 |
| - To isolate the tests, we need to reset the used singletons before each test case. We also patch the default |
37 |
| - storage client with a tmp_path. |
| 90 | + Args: |
| 91 | + prepare_test_env: Fixture to prepare the environment before each test. |
38 | 92 | """
|
39 |
| - delattr(apify._actor.Actor, '__wrapped__') |
40 | 93 |
|
41 |
| - # TODO: StorageClientManager local storage client purge # noqa: TD003 |
| 94 | + prepare_test_env() |
42 | 95 |
|
43 | 96 |
|
44 | 97 | @pytest.fixture
|
|
0 commit comments