Skip to content

Commit b36a260

Browse files
committed
add azure openai env vars and use session scoped fixutres
1 parent eac85f0 commit b36a260

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

tests/conftest.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ def mock_session_env(monkeypatch_session):
5353
monkeypatch_session.setenv("RUNNING_IN_PRODUCTION", "False")
5454
# Azure Subscription
5555
monkeypatch_session.setenv("AZURE_SUBSCRIPTION_ID", "test-storage-subid")
56-
# OpenAI
57-
monkeypatch_session.setenv("AZURE_OPENAI_CHATGPT_MODEL", "gpt-35-turbo")
58-
monkeypatch_session.setenv("OPENAI_API_KEY", "fakekey")
56+
# Azure OpenAI
57+
monkeypatch_session.setenv("OPENAI_CHAT_HOST", "azure")
58+
monkeypatch_session.setenv("OPENAI_EMBED_HOST", "azure")
59+
monkeypatch_session.setenv("AZURE_OPENAI_VERSION", "2024-03-01-preview")
60+
monkeypatch_session.setenv("AZURE_OPENAI_CHAT_DEPLOYMENT", "gpt-35-turbo")
61+
monkeypatch_session.setenv("AZURE_OPENAI_CHAT_MODEL", "gpt-35-turbo")
62+
monkeypatch_session.setenv("AZURE_OPENAI_EMBED_DEPLOYMENT", "text-embedding-ada-002")
63+
monkeypatch_session.setenv("AZURE_OPENAI_EMBED_MODEL", "text-embedding-ada-002")
64+
monkeypatch_session.setenv("AZURE_OPENAI_EMBED_MODEL_DIMENSIONS", "1536")
65+
monkeypatch_session.setenv("AZURE_OPENAI_KEY", "fakekey")
5966
# Allowed Origin
6067
monkeypatch_session.setenv("ALLOWED_ORIGIN", "https://frontend.com")
6168

@@ -82,16 +89,8 @@ async def app(mock_session_env):
8289
return app
8390

8491

85-
@pytest.fixture(scope="function")
86-
def mock_default_azure_credential(mock_session_env):
87-
"""Mock the Azure credential for testing."""
88-
with mock.patch("azure.identity.DefaultAzureCredential") as mock_default_azure_credential:
89-
mock_default_azure_credential.return_value = MockAzureCredential()
90-
yield mock_default_azure_credential
91-
92-
93-
@pytest.fixture(autouse=True)
94-
def mock_openai_embedding(monkeypatch):
92+
@pytest.fixture(scope="session")
93+
def mock_openai_embedding(monkeypatch_session):
9594
async def mock_acreate(*args, **kwargs):
9695
return CreateEmbeddingResponse(
9796
object="list",
@@ -106,14 +105,13 @@ async def mock_acreate(*args, **kwargs):
106105
usage=Usage(prompt_tokens=8, total_tokens=8),
107106
)
108107

109-
def patch():
110-
monkeypatch.setattr(openai.resources.AsyncEmbeddings, "create", mock_acreate)
108+
monkeypatch_session.setattr(openai.resources.AsyncEmbeddings, "create", mock_acreate)
111109

112-
return patch
110+
yield
113111

114112

115-
@pytest.fixture
116-
def mock_openai_chatcompletion(monkeypatch):
113+
@pytest.fixture(scope="session")
114+
def mock_openai_chatcompletion(monkeypatch_session):
117115
class AsyncChatCompletionIterator:
118116
def __init__(self, answer: str):
119117
chunk_id = "test-id"
@@ -215,19 +213,22 @@ async def mock_acreate(*args, **kwargs):
215213
model="test-model",
216214
)
217215

218-
def patch():
219-
monkeypatch.setattr(openai.resources.chat.completions.AsyncCompletions, "create", mock_acreate)
216+
monkeypatch_session.setattr(openai.resources.chat.completions.AsyncCompletions, "create", mock_acreate)
220217

221-
return patch
218+
yield
219+
220+
221+
@pytest.fixture(scope="function")
222+
def mock_default_azure_credential(mock_session_env):
223+
"""Mock the Azure credential for testing."""
224+
with mock.patch("azure.identity.DefaultAzureCredential") as mock_default_azure_credential:
225+
mock_default_azure_credential.return_value = MockAzureCredential()
226+
yield
222227

223228

224229
@pytest_asyncio.fixture(scope="function")
225-
async def test_client(
226-
monkeypatch, app, mock_default_azure_credential, mock_openai_embedding, mock_openai_chatcompletion
227-
):
230+
async def test_client(app, mock_default_azure_credential, mock_openai_embedding, mock_openai_chatcompletion):
228231
"""Create a test client."""
229-
mock_openai_embedding()
230-
mock_openai_chatcompletion()
231232
with TestClient(app) as test_client:
232233
yield test_client
233234

0 commit comments

Comments
 (0)