-
Notifications
You must be signed in to change notification settings - Fork 515
/
Copy pathconftest.py
66 lines (52 loc) · 2.33 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from typing import Generator
from unittest import mock
import pytest
from fakeredis import FakeAsyncRedis
from pytest import FixtureRequest, MonkeyPatch
@pytest.fixture(scope="session")
def default_env_vars() -> dict[str, str]:
"""
Defines default environment variables for the test session.
This fixture provides a dictionary of default environment variables that are
commonly used across tests. It can be overridden in submodule scoped `conftest.py`
files or directly in tests.
:return: A dictionary of default environment variables.
"""
return {
"ENVIRONMENT": "test",
"AZURE_OPENAI_API_BASE_EAST_US": "https://api.openai.com",
"AZURE_OPENAI_API_KEY_EAST_US": "test",
}
@pytest.fixture()
def mock_env(
monkeypatch: MonkeyPatch, request: pytest.FixtureRequest, default_env_vars: dict[str, str]
) -> Generator[None, None, None]:
"""
Temporarily sets environment variables for testing.
This fixture allows tests to run with a modified set of environment variables,
either using the default set provided by `default_env_vars` or overridden by
test-specific parameters. It ensures that changes to environment variables do
not leak between tests.
:param monkeypatch: The pytest monkeypatch fixture for modifying environment variables.
:param request: The pytest FixtureRequest object for accessing test-specific overrides.
:param default_env_vars: A dictionary of default environment variables.
:yield: None. This is a setup-teardown fixture that cleans up after itself.
"""
envvars = default_env_vars.copy()
if hasattr(request, "param") and isinstance(request.param, dict):
envvars.update(request.param)
with mock.patch.dict(os.environ, envvars):
yield
@pytest.fixture
def redis_client(request: FixtureRequest) -> FakeAsyncRedis:
"""
Provides a fake Redis client for asynchronous operations.
This fixture can be used in tests that require a Redis client but should not
interact with a real Redis instance. It leverages fakeredis to simulate Redis
operations in memory without any external dependencies.
:param request: The pytest request object, used here for potential future extensions.
:return: An instance of a fake Redis client.
"""
redis_client = FakeAsyncRedis()
return redis_client