|
7 | 7 |
|
8 | 8 |
|
9 | 9 | class MockCosmosDBResultsIterator:
|
10 |
| - def __init__(self): |
11 |
| - self.data = [ |
12 |
| - [ |
13 |
| - { |
14 |
| - "id": "123", |
15 |
| - "entra_oid": "OID_X", |
16 |
| - "title": "This is a test message", |
17 |
| - "timestamp": 123456789, |
18 |
| - "answers": [["This is a test message"]], |
19 |
| - } |
| 10 | + def __init__(self, empty=False): |
| 11 | + if empty: |
| 12 | + self.data = [] |
| 13 | + else: |
| 14 | + self.data = [ |
| 15 | + [ |
| 16 | + { |
| 17 | + "id": "123", |
| 18 | + "entra_oid": "OID_X", |
| 19 | + "title": "This is a test message", |
| 20 | + "timestamp": 123456789, |
| 21 | + "answers": [["This is a test message"]], |
| 22 | + } |
| 23 | + ] |
20 | 24 | ]
|
21 |
| - ] |
22 | 25 |
|
23 | 26 | def __aiter__(self):
|
24 | 27 | return self
|
@@ -141,6 +144,24 @@ def mock_query_items(container_proxy, query, **kwargs):
|
141 | 144 | snapshot.assert_match(json.dumps(result, indent=4), "result.json")
|
142 | 145 |
|
143 | 146 |
|
| 147 | +@pytest.mark.asyncio |
| 148 | +async def test_chathistory_query_continuation(auth_public_documents_client, monkeypatch, snapshot): |
| 149 | + |
| 150 | + def mock_query_items(container_proxy, query, **kwargs): |
| 151 | + return MockCosmosDBResultsIterator(empty=True) |
| 152 | + |
| 153 | + monkeypatch.setattr(ContainerProxy, "query_items", mock_query_items) |
| 154 | + |
| 155 | + response = await auth_public_documents_client.post( |
| 156 | + "/chat_history/items", |
| 157 | + headers={"Authorization": "Bearer MockToken"}, |
| 158 | + json={"count": 20}, |
| 159 | + ) |
| 160 | + assert response.status_code == 200 |
| 161 | + result = await response.get_json() |
| 162 | + snapshot.assert_match(json.dumps(result, indent=4), "result.json") |
| 163 | + |
| 164 | + |
144 | 165 | @pytest.mark.asyncio
|
145 | 166 | async def test_chathistory_query_error_disabled(client, monkeypatch):
|
146 | 167 |
|
@@ -198,3 +219,131 @@ def mock_query_items(container_proxy, query, **kwargs):
|
198 | 219 | assert (await response.get_json()) == {
|
199 | 220 | "error": "The app encountered an error processing your request.\nIf you are an administrator of the app, view the full error in the logs. See aka.ms/appservice-logs for more information.\nError type: <class 'Exception'>\n"
|
200 | 221 | }
|
| 222 | + |
| 223 | + |
| 224 | +# Tests for getting an individual chat history item |
| 225 | +@pytest.mark.asyncio |
| 226 | +async def test_chathistory_getitem(auth_public_documents_client, monkeypatch, snapshot): |
| 227 | + |
| 228 | + async def mock_read_item(container_proxy, item, partition_key, **kwargs): |
| 229 | + return { |
| 230 | + "id": "123", |
| 231 | + "entra_oid": "OID_X", |
| 232 | + "title": "This is a test message", |
| 233 | + "timestamp": 123456789, |
| 234 | + "answers": [["This is a test message"]], |
| 235 | + } |
| 236 | + |
| 237 | + monkeypatch.setattr(ContainerProxy, "read_item", mock_read_item) |
| 238 | + |
| 239 | + response = await auth_public_documents_client.get( |
| 240 | + "/chat_history/items/123", |
| 241 | + headers={"Authorization": "Bearer MockToken"}, |
| 242 | + ) |
| 243 | + assert response.status_code == 200 |
| 244 | + result = await response.get_json() |
| 245 | + snapshot.assert_match(json.dumps(result, indent=4), "result.json") |
| 246 | + |
| 247 | + |
| 248 | +# Error handling tests for getting an individual chat history item |
| 249 | +@pytest.mark.asyncio |
| 250 | +async def test_chathistory_getitem_error_disabled(client, monkeypatch): |
| 251 | + |
| 252 | + response = await client.get( |
| 253 | + "/chat_history/items/123", |
| 254 | + headers={"Authorization": "BearerMockToken"}, |
| 255 | + ) |
| 256 | + assert response.status_code == 400 |
| 257 | + |
| 258 | + |
| 259 | +@pytest.mark.asyncio |
| 260 | +async def test_chathistory_getitem_error_container(auth_public_documents_client, monkeypatch): |
| 261 | + auth_public_documents_client.app.config["cosmos_history_container"] = None |
| 262 | + response = await auth_public_documents_client.get( |
| 263 | + "/chat_history/items/123", |
| 264 | + headers={"Authorization": "BearerMockToken"}, |
| 265 | + ) |
| 266 | + assert response.status_code == 400 |
| 267 | + |
| 268 | + |
| 269 | +@pytest.mark.asyncio |
| 270 | +async def test_chathistory_getitem_error_entra(auth_public_documents_client, monkeypatch): |
| 271 | + response = await auth_public_documents_client.get( |
| 272 | + "/chat_history/items/123", |
| 273 | + ) |
| 274 | + assert response.status_code == 401 |
| 275 | + |
| 276 | + |
| 277 | +@pytest.mark.asyncio |
| 278 | +async def test_chathistory_getitem_error_runtime(auth_public_documents_client, monkeypatch): |
| 279 | + |
| 280 | + async def mock_read_item(container_proxy, item, partition_key, **kwargs): |
| 281 | + raise Exception("Test Exception") |
| 282 | + |
| 283 | + monkeypatch.setattr(ContainerProxy, "read_item", mock_read_item) |
| 284 | + |
| 285 | + response = await auth_public_documents_client.get( |
| 286 | + "/chat_history/items/123", |
| 287 | + headers={"Authorization": "Bearer MockToken"}, |
| 288 | + ) |
| 289 | + assert response.status_code == 500 |
| 290 | + |
| 291 | + |
| 292 | +# Tests for deleting an individual chat history item |
| 293 | +@pytest.mark.asyncio |
| 294 | +async def test_chathistory_deleteitem(auth_public_documents_client, monkeypatch): |
| 295 | + |
| 296 | + async def mock_delete_item(container_proxy, item, partition_key, **kwargs): |
| 297 | + assert item == "123" |
| 298 | + assert partition_key == "OID_X" |
| 299 | + |
| 300 | + monkeypatch.setattr(ContainerProxy, "delete_item", mock_delete_item) |
| 301 | + |
| 302 | + response = await auth_public_documents_client.delete( |
| 303 | + "/chat_history/items/123", |
| 304 | + headers={"Authorization": "Bearer MockToken"}, |
| 305 | + ) |
| 306 | + assert response.status_code == 204 |
| 307 | + |
| 308 | + |
| 309 | +@pytest.mark.asyncio |
| 310 | +async def test_chathistory_deleteitem_error_disabled(client, monkeypatch): |
| 311 | + |
| 312 | + response = await client.delete( |
| 313 | + "/chat_history/items/123", |
| 314 | + headers={"Authorization": "Bearer MockToken"}, |
| 315 | + ) |
| 316 | + assert response.status_code == 400 |
| 317 | + |
| 318 | + |
| 319 | +@pytest.mark.asyncio |
| 320 | +async def test_chathistory_deleteitem_error_container(auth_public_documents_client, monkeypatch): |
| 321 | + auth_public_documents_client.app.config["cosmos_history_container"] = None |
| 322 | + response = await auth_public_documents_client.delete( |
| 323 | + "/chat_history/items/123", |
| 324 | + headers={"Authorization": "Bearer MockToken"}, |
| 325 | + ) |
| 326 | + assert response.status_code == 400 |
| 327 | + |
| 328 | + |
| 329 | +@pytest.mark.asyncio |
| 330 | +async def test_chathistory_deleteitem_error_entra(auth_public_documents_client, monkeypatch): |
| 331 | + response = await auth_public_documents_client.delete( |
| 332 | + "/chat_history/items/123", |
| 333 | + ) |
| 334 | + assert response.status_code == 401 |
| 335 | + |
| 336 | + |
| 337 | +@pytest.mark.asyncio |
| 338 | +async def test_chathistory_deleteitem_error_runtime(auth_public_documents_client, monkeypatch): |
| 339 | + |
| 340 | + async def mock_delete_item(container_proxy, item, partition_key, **kwargs): |
| 341 | + raise Exception("Test Exception") |
| 342 | + |
| 343 | + monkeypatch.setattr(ContainerProxy, "delete_item", mock_delete_item) |
| 344 | + |
| 345 | + response = await auth_public_documents_client.delete( |
| 346 | + "/chat_history/items/123", |
| 347 | + headers={"Authorization": "Bearer MockToken"}, |
| 348 | + ) |
| 349 | + assert response.status_code == 500 |
0 commit comments