|
| 1 | +import pytest |
| 2 | +from unittest.mock import AsyncMock, patch |
| 3 | +from azure.cosmos import exceptions |
| 4 | +from backend.batch.utilities.chat_history.cosmosdb import CosmosConversationClient |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def mock_cosmos_client(): |
| 9 | + mock_client = AsyncMock() |
| 10 | + mock_database_client = AsyncMock() |
| 11 | + mock_container_client = AsyncMock() |
| 12 | + |
| 13 | + mock_client.get_database_client.return_value = mock_database_client |
| 14 | + mock_database_client.get_container_client.return_value = mock_container_client |
| 15 | + |
| 16 | + return mock_client, mock_database_client, mock_container_client |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def cosmos_client(mock_cosmos_client): |
| 21 | + cosmosdb_client, database_client, container_client = mock_cosmos_client |
| 22 | + with patch("azure.cosmos.aio.CosmosClient", return_value=cosmosdb_client): |
| 23 | + client = CosmosConversationClient( |
| 24 | + cosmosdb_endpoint="https://test-cosmosdb.com", |
| 25 | + credential="test-credential", |
| 26 | + database_name="test-database", |
| 27 | + container_name="test-container", |
| 28 | + ) |
| 29 | + client.cosmosdb_client = cosmosdb_client |
| 30 | + client.database_client = database_client |
| 31 | + client.container_client = container_client |
| 32 | + return client |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.asyncio |
| 36 | +async def test_initialize_client_success(cosmos_client): |
| 37 | + client = cosmos_client |
| 38 | + |
| 39 | + assert client.cosmosdb_endpoint == "https://test-cosmosdb.com" |
| 40 | + assert client.credential == "test-credential" |
| 41 | + assert client.database_name == "test-database" |
| 42 | + assert client.container_name == "test-container" |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_ensure_client_initialized_success(cosmos_client): |
| 47 | + client = cosmos_client |
| 48 | + client.database_client.read = AsyncMock() |
| 49 | + client.container_client.read = AsyncMock() |
| 50 | + |
| 51 | + result, message = await client.ensure() |
| 52 | + |
| 53 | + assert result is True |
| 54 | + assert message == "CosmosDB client initialized successfully" |
| 55 | + client.database_client.read.assert_called_once() |
| 56 | + client.container_client.read.assert_called_once() |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +async def test_ensure_client_not_initialized(cosmos_client): |
| 61 | + client = cosmos_client |
| 62 | + client.database_client.read = AsyncMock( |
| 63 | + side_effect=exceptions.CosmosHttpResponseError |
| 64 | + ) |
| 65 | + client.container_client.read = AsyncMock() |
| 66 | + |
| 67 | + result, message = await client.ensure() |
| 68 | + |
| 69 | + assert result is False |
| 70 | + assert "not found" in message.lower() |
| 71 | + client.database_client.read.assert_called_once() |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_create_conversation_success(cosmos_client): |
| 76 | + client = cosmos_client |
| 77 | + client.container_client.upsert_item = AsyncMock( |
| 78 | + return_value={"id": "500e77bd-26b9-441a-8fe3-cd0e02993671"} |
| 79 | + ) |
| 80 | + |
| 81 | + response = await client.create_conversation( |
| 82 | + "user-123", "500e77bd-26b9-441a-8fe3-cd0e02993671", "Test Conversation" |
| 83 | + ) |
| 84 | + |
| 85 | + assert response["id"] == "500e77bd-26b9-441a-8fe3-cd0e02993671" |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_create_conversation_failure(cosmos_client): |
| 90 | + client = cosmos_client |
| 91 | + client.container_client.upsert_item = AsyncMock(return_value=None) |
| 92 | + |
| 93 | + response = await client.create_conversation( |
| 94 | + "user-123", "500e77bd-26b9-441a-8fe3-cd0e02993671", "Test Conversation" |
| 95 | + ) |
| 96 | + |
| 97 | + assert response is False |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.asyncio |
| 101 | +async def test_upsert_conversation_success(cosmos_client): |
| 102 | + client = cosmos_client |
| 103 | + client.container_client.upsert_item = AsyncMock( |
| 104 | + return_value={"id": "500e77bd-26b9-441a-8fe3-cd0e02993671"} |
| 105 | + ) |
| 106 | + |
| 107 | + conversation = { |
| 108 | + "id": "500e77bd-26b9-441a-8fe3-cd0e02993671", |
| 109 | + "type": "conversation", |
| 110 | + "userId": "user-123", |
| 111 | + "title": "Updated Conversation", |
| 112 | + } |
| 113 | + response = await client.upsert_conversation(conversation) |
| 114 | + |
| 115 | + assert response["id"] == "500e77bd-26b9-441a-8fe3-cd0e02993671" |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.asyncio |
| 119 | +async def test_delete_conversation_success(cosmos_client): |
| 120 | + client = cosmos_client |
| 121 | + client.container_client.read_item = AsyncMock( |
| 122 | + return_value={"id": "500e77bd-26b9-441a-8fe3-cd0e02993671"} |
| 123 | + ) |
| 124 | + client.container_client.delete_item = AsyncMock(return_value={"status": "deleted"}) |
| 125 | + |
| 126 | + response = await client.delete_conversation( |
| 127 | + "user-123", "500e77bd-26b9-441a-8fe3-cd0e02993671" |
| 128 | + ) |
| 129 | + |
| 130 | + assert response["status"] == "deleted" |
| 131 | + client.container_client.delete_item.assert_called_once_with( |
| 132 | + item="500e77bd-26b9-441a-8fe3-cd0e02993671", partition_key="user-123" |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.asyncio |
| 137 | +async def test_delete_messages_success(cosmos_client): |
| 138 | + client = cosmos_client |
| 139 | + client.get_messages = AsyncMock( |
| 140 | + return_value=[ |
| 141 | + {"id": "39c395da-e2f7-49c9-bca5-c9511d3c5172"}, |
| 142 | + {"id": "39c395da-e2f7-49c9-bca5-c9511d3c5174"}, |
| 143 | + ] |
| 144 | + ) |
| 145 | + client.container_client.delete_item = AsyncMock() |
| 146 | + |
| 147 | + response = await client.delete_messages( |
| 148 | + "500e77bd-26b9-441a-8fe3-cd0e02993671", "user-123" |
| 149 | + ) |
| 150 | + |
| 151 | + assert len(response) == 2 |
| 152 | + client.get_messages.assert_called_once_with( |
| 153 | + "user-123", "500e77bd-26b9-441a-8fe3-cd0e02993671" |
| 154 | + ) |
| 155 | + client.container_client.delete_item.assert_any_call( |
| 156 | + item="39c395da-e2f7-49c9-bca5-c9511d3c5172", partition_key="user-123" |
| 157 | + ) |
| 158 | + client.container_client.delete_item.assert_any_call( |
| 159 | + item="39c395da-e2f7-49c9-bca5-c9511d3c5174", partition_key="user-123" |
| 160 | + ) |
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.asyncio |
| 164 | +async def test_update_message_feedback_success(cosmos_client): |
| 165 | + client = cosmos_client |
| 166 | + client.container_client.read_item = AsyncMock( |
| 167 | + return_value={"id": "39c395da-e2f7-49c9-bca5-c9511d3c5172", "feedback": ""} |
| 168 | + ) |
| 169 | + client.container_client.upsert_item = AsyncMock( |
| 170 | + return_value={ |
| 171 | + "id": "39c395da-e2f7-49c9-bca5-c9511d3c5172", |
| 172 | + "feedback": "positive", |
| 173 | + } |
| 174 | + ) |
| 175 | + |
| 176 | + response = await client.update_message_feedback( |
| 177 | + "user-123", "39c395da-e2f7-49c9-bca5-c9511d3c5172", "positive" |
| 178 | + ) |
| 179 | + |
| 180 | + assert response["feedback"] == "positive" |
| 181 | + client.container_client.upsert_item.assert_called_once() |
0 commit comments