|
18 | 18 | from crawlee.storages import _creation_management
|
19 | 19 |
|
20 | 20 | if TYPE_CHECKING:
|
21 |
| - from collections.abc import AsyncGenerator |
| 21 | + from collections.abc import AsyncGenerator, Generator |
22 | 22 | from pathlib import Path
|
23 | 23 |
|
24 | 24 |
|
25 | 25 | @pytest.fixture
|
26 |
| -def reset_globals(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callable[[], None]: |
27 |
| - def reset() -> None: |
28 |
| - # Set the environment variable for the local storage directory to the temporary path |
| 26 | +def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callable[[], None]: |
| 27 | + """Prepare the testing environment by resetting the global state before each test. |
| 28 | +
|
| 29 | + This fixture ensures that the global state of the package is reset to a known baseline before each test runs. |
| 30 | + It also configures a temporary storage directory for test isolation. |
| 31 | +
|
| 32 | + Args: |
| 33 | + monkeypatch: Test utility provided by pytest for patching. |
| 34 | + tmp_path: A unique temporary directory path provided by pytest for test isolation. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + A callable that prepares the test environment. |
| 38 | + """ |
| 39 | + |
| 40 | + def _prepare_test_env() -> None: |
| 41 | + # Set the environment variable for the local storage directory to the temporary path. |
29 | 42 | monkeypatch.setenv('CRAWLEE_STORAGE_DIR', str(tmp_path))
|
30 | 43 |
|
31 |
| - # Reset services in crawlee.service_container |
32 |
| - service_container.set_configuration(Configuration(), force=True) |
33 |
| - service_container.set_storage_client(MemoryStorageClient(), force=True) |
34 |
| - service_container.set_event_manager(LocalEventManager(), force=True) |
| 44 | + # Initialize services in the service container with default values. |
| 45 | + service_container.set_configuration(Configuration()) |
| 46 | + service_container.set_storage_client(MemoryStorageClient()) |
| 47 | + service_container.set_event_manager(LocalEventManager()) |
| 48 | + |
| 49 | + # Reset the global state flags in the service locator. |
| 50 | + service_container._service_locator._configuration_was_set = False |
| 51 | + service_container._service_locator._storage_client_was_set = False |
| 52 | + service_container._service_locator._event_manager_was_set = False |
35 | 53 |
|
36 |
| - # Clear creation-related caches to ensure no state is carried over between tests |
| 54 | + # Clear creation-related caches to ensure no state is carried over between tests. |
37 | 55 | monkeypatch.setattr(_creation_management, '_cache_dataset_by_id', {})
|
38 | 56 | monkeypatch.setattr(_creation_management, '_cache_dataset_by_name', {})
|
39 | 57 | monkeypatch.setattr(_creation_management, '_cache_kvs_by_id', {})
|
40 | 58 | monkeypatch.setattr(_creation_management, '_cache_kvs_by_name', {})
|
41 | 59 | monkeypatch.setattr(_creation_management, '_cache_rq_by_id', {})
|
42 | 60 | monkeypatch.setattr(_creation_management, '_cache_rq_by_name', {})
|
43 | 61 |
|
44 |
| - # Verify that the environment variable is set correctly |
| 62 | + # Verify that the test environment was set up correctly. |
45 | 63 | assert os.environ.get('CRAWLEE_STORAGE_DIR') == str(tmp_path)
|
| 64 | + assert service_container._service_locator.configuration_was_set is False |
| 65 | + assert service_container._service_locator.storage_client_was_set is False |
| 66 | + assert service_container._service_locator.event_manager_was_set is False |
46 | 67 |
|
47 |
| - return reset |
| 68 | + return _prepare_test_env |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture |
| 72 | +def cleanup_test_env() -> Callable[[], None]: |
| 73 | + """Clean up and reset global state after a test has completed. |
| 74 | +
|
| 75 | + This fixture ensures that any modifications to the global state during a test are undone |
| 76 | + after the test finishes. Restoring the environment for the subsequent tests. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + A callable that cleans up the test environment. |
| 80 | + """ |
| 81 | + |
| 82 | + def _cleanup_test_env() -> None: |
| 83 | + # Reset the flags in the service locator to indicate no services are explicitly set. |
| 84 | + service_container._service_locator._configuration_was_set = False |
| 85 | + service_container._service_locator._storage_client_was_set = False |
| 86 | + service_container._service_locator._event_manager_was_set = False |
| 87 | + |
| 88 | + return _cleanup_test_env |
48 | 89 |
|
49 | 90 |
|
50 | 91 | @pytest.fixture(autouse=True)
|
51 |
| -def _isolate_test_environment(reset_globals: Callable[[], None]) -> None: |
52 |
| - """Isolate tests by resetting the storage clients, clearing caches, and setting the environment variables. |
| 92 | +def _isolate_test_environment( |
| 93 | + prepare_test_env: Callable[[], None], |
| 94 | + cleanup_test_env: Callable[[], None], |
| 95 | +) -> Generator[None, None, None]: |
| 96 | + """Isolate the testing environment by resetting global state before and after each test. |
53 | 97 |
|
54 |
| - The fixture is applied automatically to all test cases. |
| 98 | + This fixture ensures that each test starts with a clean slate and that any modifications during the test |
| 99 | + do not affect subsequent tests. It runs automatically for all tests. |
55 | 100 |
|
56 | 101 | Args:
|
57 |
| - monkeypatch: Test utility provided by pytest. |
58 |
| - tmp_path: A unique temporary directory path provided by pytest for test isolation. |
59 |
| - """ |
| 102 | + prepare_test_env: Fixture to prepare the environment before each test. |
| 103 | + cleanup_test_env: Fixture to clean up the environment after each test. |
60 | 104 |
|
61 |
| - reset_globals() |
| 105 | + Yields: |
| 106 | + None. This fixture works as a setup and teardown mechanism. |
| 107 | + """ |
| 108 | + prepare_test_env() |
| 109 | + yield |
| 110 | + cleanup_test_env() |
62 | 111 |
|
63 | 112 |
|
64 | 113 | @pytest.fixture
|
|
0 commit comments