brief-rags-bench/tests/test_db_api_client.py

228 lines
8.4 KiB
Python

"""Tests for DBApiClient."""
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from app.interfaces.db_api_client import DBApiClient
from app.models.auth import LoginRequest, UserResponse
from app.models.settings import UserSettings, UserSettingsUpdate, EnvironmentSettings
from app.models.analysis import SessionCreate, SessionResponse, SessionList, SessionListItem
class TestDBApiClient:
"""Tests for DBApiClient class."""
@pytest.mark.asyncio
async def test_login_user(self):
"""Test login_user calls post correctly."""
with patch('app.interfaces.base.httpx.AsyncClient'):
client = DBApiClient(api_prefix="http://db-api:8080/api/v1")
# Mock the post method
mock_user_response = UserResponse(
user_id="user-123",
login="12345678",
last_login_at="2024-01-01T00:00:00Z",
created_at="2024-01-01T00:00:00Z"
)
client.post = AsyncMock(return_value=mock_user_response)
login_request = LoginRequest(login="12345678", client_ip="127.0.0.1")
result = await client.login_user(login_request)
assert result == mock_user_response
client.post.assert_called_once_with(
"/users/login",
body=login_request,
response_model=UserResponse
)
@pytest.mark.asyncio
async def test_get_user_settings(self):
"""Test get_user_settings calls get correctly."""
with patch('app.interfaces.base.httpx.AsyncClient'):
client = DBApiClient(api_prefix="http://db-api:8080/api/v1")
mock_settings = UserSettings(
user_id="user-123",
settings={
"ift": EnvironmentSettings(
apiMode="bench",
bearerToken="",
systemPlatform="",
systemPlatformUser="",
platformUserId="",
platformId="",
withClassify=False,
resetSessionMode=True
)
},
updated_at="2024-01-01T00:00:00Z"
)
client.get = AsyncMock(return_value=mock_settings)
result = await client.get_user_settings("user-123")
assert result == mock_settings
client.get.assert_called_once_with(
"/users/user-123/settings",
response_model=UserSettings
)
@pytest.mark.asyncio
async def test_update_user_settings(self):
"""Test update_user_settings calls put correctly."""
with patch('app.interfaces.base.httpx.AsyncClient'):
client = DBApiClient(api_prefix="http://db-api:8080/api/v1")
settings_update = UserSettingsUpdate(
settings={
"ift": EnvironmentSettings(
apiMode="backend",
bearerToken="",
systemPlatform="",
systemPlatformUser="",
platformUserId="",
platformId="",
withClassify=True,
resetSessionMode=False
)
}
)
mock_updated_settings = UserSettings(
user_id="user-123",
settings=settings_update.settings,
updated_at="2024-01-01T01:00:00Z"
)
client.put = AsyncMock(return_value=mock_updated_settings)
result = await client.update_user_settings("user-123", settings_update)
assert result == mock_updated_settings
client.put.assert_called_once_with(
"/users/user-123/settings",
body=settings_update,
response_model=UserSettings
)
@pytest.mark.asyncio
async def test_save_session(self):
"""Test save_session calls post correctly."""
with patch('app.interfaces.base.httpx.AsyncClient'):
client = DBApiClient(api_prefix="http://db-api:8080/api/v1")
session_data = SessionCreate(
environment="ift",
api_mode="bench",
request=[{"question": "test"}],
response={"answer": "test"},
annotations={}
)
mock_session_response = SessionResponse(
session_id="session-123",
user_id="user-123",
environment="ift",
api_mode="bench",
request=[{"question": "test"}],
response={"answer": "test"},
annotations={},
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z"
)
client.post = AsyncMock(return_value=mock_session_response)
result = await client.save_session("user-123", session_data)
assert result == mock_session_response
client.post.assert_called_once_with(
"/users/user-123/sessions",
body=session_data,
response_model=SessionResponse
)
@pytest.mark.asyncio
async def test_get_sessions(self):
"""Test get_sessions calls get correctly."""
with patch('app.interfaces.base.httpx.AsyncClient'):
client = DBApiClient(api_prefix="http://db-api:8080/api/v1")
mock_sessions = SessionList(
sessions=[
SessionListItem(
session_id="session-1",
environment="ift",
created_at="2024-01-01T00:00:00Z"
)
],
total=1
)
client.get = AsyncMock(return_value=mock_sessions)
result = await client.get_sessions("user-123", environment="ift", limit=10, offset=0)
assert result == mock_sessions
client.get.assert_called_once_with(
"/users/user-123/sessions",
params={"limit": 10, "offset": 0, "environment": "ift"},
response_model=SessionList
)
@pytest.mark.asyncio
async def test_get_sessions_without_environment(self):
"""Test get_sessions without environment filter."""
with patch('app.interfaces.base.httpx.AsyncClient'):
client = DBApiClient(api_prefix="http://db-api:8080/api/v1")
mock_sessions = SessionList(sessions=[], total=0)
client.get = AsyncMock(return_value=mock_sessions)
result = await client.get_sessions("user-123", limit=50, offset=0)
assert result == mock_sessions
client.get.assert_called_once_with(
"/users/user-123/sessions",
params={"limit": 50, "offset": 0},
response_model=SessionList
)
@pytest.mark.asyncio
async def test_get_session(self):
"""Test get_session calls get correctly."""
with patch('app.interfaces.base.httpx.AsyncClient'):
client = DBApiClient(api_prefix="http://db-api:8080/api/v1")
mock_session = SessionResponse(
session_id="session-123",
user_id="user-123",
environment="ift",
api_mode="bench",
request=[],
response={},
annotations={},
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z"
)
client.get = AsyncMock(return_value=mock_session)
result = await client.get_session("user-123", "session-123")
assert result == mock_session
client.get.assert_called_once_with(
"/users/user-123/sessions/session-123",
response_model=SessionResponse
)
@pytest.mark.asyncio
async def test_delete_session(self):
"""Test delete_session calls delete correctly."""
with patch('app.interfaces.base.httpx.AsyncClient'):
client = DBApiClient(api_prefix="http://db-api:8080/api/v1")
client.delete = AsyncMock(return_value={})
result = await client.delete_session("user-123", "session-123")
assert result == {}
client.delete.assert_called_once_with(
"/users/user-123/sessions/session-123"
)