Skip to content

Commit 7e853dd

Browse files
committed
.
1 parent 234b04c commit 7e853dd

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ typeCheckingMode = "standard"
188188
include = ["src", "tests"]
189189

190190
[tool.coverage.report]
191-
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:", "assert_never()"]
191+
exclude_lines = [
192+
"pragma: no cover",
193+
"if TYPE_CHECKING:",
194+
"assert_never()",
195+
]
192196

193197
[tool.ipdb]
194198
context = 7

tests/integration/conftest.py

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
from filelock import FileLock
1414

1515
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
1719

1820
import apify._actor
1921
from ._utils import generate_unique_resource_name
@@ -29,16 +31,67 @@
2931
_SDK_ROOT_PATH = Path(__file__).parent.parent.parent.resolve()
3032

3133

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+
3283
@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.
3589
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.
3892
"""
39-
delattr(apify._actor.Actor, '__wrapped__')
4093

41-
# TODO: StorageClientManager local storage client purge # noqa: TD003
94+
prepare_test_env()
4295

4396

4497
@pytest.fixture

tests/unit/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import pytest
1111

12-
from apify_client.client import ApifyClientAsync
12+
from apify_client import ApifyClientAsync
1313
from apify_shared.consts import ApifyEnvVars
1414
from crawlee import service_locator
1515
from crawlee.configuration import Configuration as CrawleeConfiguration

0 commit comments

Comments
 (0)