1+ import os
12import pytest
2- from unittest .mock import AsyncMock , MagicMock , patch
3+ from unittest .mock import AsyncMock , patch
34from azure .cosmos .partition_key import PartitionKey
45from src .backend .context .cosmos_memory import CosmosBufferedChatCompletionContext
56
7+ # Set environment variables globally before importing modules
8+ os .environ ["COSMOSDB_ENDPOINT" ] = "https://mock-endpoint"
9+ os .environ ["COSMOSDB_KEY" ] = "mock-key"
10+ os .environ ["COSMOSDB_DATABASE" ] = "mock-database"
11+ os .environ ["COSMOSDB_CONTAINER" ] = "mock-container"
12+ os .environ ["AZURE_OPENAI_DEPLOYMENT_NAME" ] = "mock-deployment-name"
13+ os .environ ["AZURE_OPENAI_API_VERSION" ] = "2023-01-01"
14+ os .environ ["AZURE_OPENAI_ENDPOINT" ] = "https://mock-openai-endpoint"
15+
16+
17+ async def async_iterable (mock_items ):
18+ """Helper to create an async iterable."""
19+ for item in mock_items :
20+ yield item
21+
622
7- # Mock environment variables
823@pytest .fixture (autouse = True )
924def mock_env_variables (monkeypatch ):
1025 """Mock all required environment variables."""
@@ -21,14 +36,6 @@ def mock_env_variables(monkeypatch):
2136 monkeypatch .setenv (key , value )
2237
2338
24- @pytest .fixture (autouse = True )
25- def mock_azure_credentials ():
26- """Mock Azure DefaultAzureCredential for all tests."""
27- with patch ("azure.identity.aio.DefaultAzureCredential" ) as mock_cred :
28- mock_cred .return_value .get_token = AsyncMock (return_value = {"token" : "mock-token" })
29- yield
30-
31-
3239@pytest .fixture
3340def mock_cosmos_client ():
3441 """Fixture for mocking Cosmos DB client and container."""
@@ -48,143 +55,15 @@ def mock_config(mock_cosmos_client):
4855 yield
4956
5057
51- async def async_iterable (mock_items ):
52- """Helper to create an async iterable."""
53- for item in mock_items :
54- yield item
55-
56-
57- @pytest .mark .asyncio (loop_scope = "session" )
58+ @pytest .mark .asyncio
5859async def test_initialize (mock_config , mock_cosmos_client ):
5960 """Test if the Cosmos DB container is initialized correctly."""
6061 mock_client , mock_container = mock_cosmos_client
61- async with CosmosBufferedChatCompletionContext (
62- session_id = "test_session" , user_id = "test_user"
63- ) as context :
64- await context .initialize ()
65- mock_client .create_container_if_not_exists .assert_called_once_with (
66- id = "mock-container" , partition_key = PartitionKey (path = "/session_id" )
67- )
68- assert context ._container == mock_container
69-
70-
71- @pytest .mark .asyncio (loop_scope = "session" )
72- async def test_close_without_initialization (mock_config ):
73- """Test closing the context without prior initialization."""
74- async with CosmosBufferedChatCompletionContext (
75- session_id = "test_session" , user_id = "test_user"
76- ):
77- pass # Ensure proper cleanup without initialization
78-
79-
80- @pytest .mark .asyncio (loop_scope = "session" )
81- async def test_add_item (mock_config , mock_cosmos_client ):
82- """Test adding an item to Cosmos DB."""
83- _ , mock_container = mock_cosmos_client
84- mock_item = MagicMock ()
85- mock_item .model_dump .return_value = {"id" : "test-item" , "data" : "test-data" }
86-
87- async with CosmosBufferedChatCompletionContext (
88- session_id = "test_session" , user_id = "test_user"
89- ) as context :
90- await context .initialize ()
91- await context .add_item (mock_item )
92- mock_container .create_item .assert_called_once_with (
93- body = {"id" : "test-item" , "data" : "test-data" }
94- )
95-
96-
97- @pytest .mark .asyncio (loop_scope = "session" )
98- async def test_update_item (mock_config , mock_cosmos_client ):
99- """Test updating an item in Cosmos DB."""
100- _ , mock_container = mock_cosmos_client
101- mock_item = MagicMock ()
102- mock_item .model_dump .return_value = {"id" : "test-item" , "data" : "updated-data" }
103-
104- async with CosmosBufferedChatCompletionContext (
105- session_id = "test_session" , user_id = "test_user"
106- ) as context :
107- await context .initialize ()
108- await context .update_item (mock_item )
109- mock_container .upsert_item .assert_called_once_with (
110- body = {"id" : "test-item" , "data" : "updated-data" }
111- )
112-
113-
114- @pytest .mark .asyncio (loop_scope = "session" )
115- async def test_get_item_by_id (mock_config , mock_cosmos_client ):
116- """Test retrieving an item by ID from Cosmos DB."""
117- _ , mock_container = mock_cosmos_client
118- mock_item = {"id" : "test-item" , "data" : "retrieved-data" }
119- mock_container .read_item .return_value = mock_item
120-
121- mock_model_class = MagicMock ()
122- mock_model_class .model_validate .return_value = "validated_item"
123-
124- async with CosmosBufferedChatCompletionContext (
125- session_id = "test_session" , user_id = "test_user"
126- ) as context :
127- await context .initialize ()
128- result = await context .get_item_by_id (
129- "test-item" , "test-partition" , mock_model_class
130- )
131-
132- assert result == "validated_item"
133- mock_container .read_item .assert_called_once_with (
134- item = "test-item" , partition_key = "test-partition"
135- )
136-
137-
138- @pytest .mark .asyncio (loop_scope = "session" )
139- async def test_delete_item (mock_config , mock_cosmos_client ):
140- """Test deleting an item from Cosmos DB."""
141- _ , mock_container = mock_cosmos_client
142-
143- async with CosmosBufferedChatCompletionContext (
62+ context = CosmosBufferedChatCompletionContext (
14463 session_id = "test_session" , user_id = "test_user"
145- ) as context :
146- await context .initialize ()
147- await context .delete_item ("test-item" , "test-partition" )
148-
149- mock_container .delete_item .assert_called_once_with (
150- item = "test-item" , partition_key = "test-partition"
151- )
152-
153-
154- @pytest .mark .asyncio (loop_scope = "session" )
155- async def test_add_plan (mock_config , mock_cosmos_client ):
156- """Test adding a plan to Cosmos DB."""
157- _ , mock_container = mock_cosmos_client
158- mock_plan = MagicMock ()
159- mock_plan .model_dump .return_value = {"id" : "plan1" , "data" : "plan-data" }
160-
161- async with CosmosBufferedChatCompletionContext (
162- session_id = "test_session" , user_id = "test_user"
163- ) as context :
164- await context .initialize ()
165- await context .add_plan (mock_plan )
166-
167- mock_container .create_item .assert_called_once_with (
168- body = {"id" : "plan1" , "data" : "plan-data" }
169- )
170-
171-
172- @pytest .mark .asyncio (loop_scope = "session" )
173- async def test_update_plan (mock_config , mock_cosmos_client ):
174- """Test updating a plan in Cosmos DB."""
175- _ , mock_container = mock_cosmos_client
176- mock_plan = MagicMock ()
177- mock_plan .model_dump .return_value = {
178- "id" : "plan1" ,
179- "data" : "updated-plan-data" ,
180- }
181-
182- async with CosmosBufferedChatCompletionContext (
183- session_id = "test_session" , user_id = "test_user"
184- ) as context :
185- await context .initialize ()
186- await context .update_plan (mock_plan )
187-
188- mock_container .upsert_item .assert_called_once_with (
189- body = {"id" : "plan1" , "data" : "updated-plan-data" }
190- )
64+ )
65+ await context .initialize ()
66+ mock_client .create_container_if_not_exists .assert_called_once_with (
67+ id = "mock-container" , partition_key = PartitionKey (path = "/session_id" )
68+ )
69+ assert context ._container == mock_container
0 commit comments