brief-rags-bench/tests/integration/test_query_integration.py

296 lines
9.9 KiB
Python

"""Integration tests for query endpoints (DB API interaction only).
Note: These tests check DB API integration for user settings retrieval.
RAG backend calls are not tested here (require actual RAG infrastructure).
"""
import pytest
from unittest.mock import patch, AsyncMock, MagicMock
@pytest.mark.integration
class TestQueryDBApiIntegration:
"""Integration tests for query endpoints - DB API interaction only."""
def test_bench_query_retrieves_user_settings(self, client, auth_headers):
"""Test that bench query retrieves user settings from DB API."""
settings_data = {
"settings": {
"ift": {
"apiMode": "bench",
"bearerToken": "test-token",
"systemPlatform": "test-platform",
"systemPlatformUser": "test-user",
"platformUserId": "user-123",
"platformId": "platform-123",
"withClassify": False,
"resetSessionMode": True
}
}
}
client.put("/api/v1/settings", json=settings_data, headers=auth_headers)
with patch('app.api.v1.query.RagService') as MockRagService:
mock_rag = AsyncMock()
mock_rag.send_bench_query = AsyncMock(return_value={
"answers": [{"answer": "Test answer", "docs": []}]
})
mock_rag.close = AsyncMock()
MockRagService.return_value = mock_rag
query_data = {
"environment": "ift",
"questions": [{"body": "Test question", "with_docs": True}]
}
response = client.post(
"/api/v1/query/bench",
json=query_data,
headers=auth_headers
)
assert response.status_code == 200
mock_rag.send_bench_query.assert_called_once()
call_kwargs = mock_rag.send_bench_query.call_args[1]
assert call_kwargs["environment"] == "ift"
assert "user_settings" in call_kwargs
user_settings = call_kwargs["user_settings"]
assert user_settings["bearerToken"] == "test-token"
def test_backend_query_retrieves_user_settings(self, client, auth_headers):
"""Test that backend query retrieves user settings from DB API."""
settings_data = {
"settings": {
"psi": {
"apiMode": "backend",
"bearerToken": "backend-token",
"systemPlatform": "backend-platform",
"systemPlatformUser": "backend-user",
"platformUserId": "user-456",
"platformId": "platform-456",
"withClassify": True,
"resetSessionMode": False
}
}
}
client.put("/api/v1/settings", json=settings_data, headers=auth_headers)
with patch('app.api.v1.query.RagService') as MockRagService:
mock_rag = AsyncMock()
mock_rag.send_backend_query = AsyncMock(return_value=[
{"answer": "Test answer", "confidence": 0.95}
])
mock_rag.close = AsyncMock()
MockRagService.return_value = mock_rag
query_data = {
"environment": "psi",
"questions": [{"body": "Test question", "with_docs": True}],
"reset_session": False
}
response = client.post(
"/api/v1/query/backend",
json=query_data,
headers=auth_headers
)
assert response.status_code == 200
mock_rag.send_backend_query.assert_called_once()
call_kwargs = mock_rag.send_backend_query.call_args[1]
assert call_kwargs["environment"] == "psi"
assert call_kwargs["reset_session"] is False
user_settings = call_kwargs["user_settings"]
assert user_settings["bearerToken"] == "backend-token"
assert user_settings["withClassify"] is True
def test_bench_query_wrong_api_mode(self, client, auth_headers):
"""Test bench query fails when settings configured for backend mode."""
settings_data = {
"settings": {
"ift": {
"apiMode": "backend",
"bearerToken": "token",
"systemPlatform": "platform",
"systemPlatformUser": "user",
"platformUserId": "user-id",
"platformId": "platform-id",
"withClassify": False,
"resetSessionMode": True
}
}
}
client.put("/api/v1/settings", json=settings_data, headers=auth_headers)
query_data = {
"environment": "ift",
"questions": [{"body": "Test", "with_docs": True}]
}
response = client.post(
"/api/v1/query/bench",
json=query_data,
headers=auth_headers
)
assert response.status_code in [400, 500]
def test_backend_query_wrong_api_mode(self, client, auth_headers):
"""Test backend query fails when settings configured for bench mode."""
settings_data = {
"settings": {
"prod": {
"apiMode": "bench",
"bearerToken": "token",
"systemPlatform": "platform",
"systemPlatformUser": "user",
"platformUserId": "user-id",
"platformId": "platform-id",
"withClassify": False,
"resetSessionMode": True
}
}
}
client.put("/api/v1/settings", json=settings_data, headers=auth_headers)
query_data = {
"environment": "prod",
"questions": [{"body": "Test", "with_docs": True}],
"reset_session": True
}
response = client.post(
"/api/v1/query/backend",
json=query_data,
headers=auth_headers
)
assert response.status_code in [400, 500]
def test_query_invalid_environment(self, client, auth_headers):
"""Test query with invalid environment name."""
query_data = {
"environment": "invalid_env",
"questions": [{"body": "Test", "with_docs": True}]
}
response = client.post(
"/api/v1/query/bench",
json=query_data,
headers=auth_headers
)
assert response.status_code == 400
query_data["reset_session"] = True
response = client.post(
"/api/v1/query/backend",
json=query_data,
headers=auth_headers
)
assert response.status_code == 400
def test_query_requires_authentication(self, client):
"""Test that query endpoints require authentication."""
query_data = {
"environment": "ift",
"questions": [{"body": "Test", "with_docs": True}]
}
response = client.post("/api/v1/query/bench", json=query_data)
assert response.status_code == 401
query_data["reset_session"] = True
response = client.post("/api/v1/query/backend", json=query_data)
assert response.status_code == 401
def test_settings_update_affects_query(self, client, auth_headers):
"""Test that updating settings affects subsequent queries."""
initial_settings = {
"settings": {
"ift": {
"apiMode": "bench",
"bearerToken": "initial-token",
"systemPlatform": "initial",
"systemPlatformUser": "initial",
"platformUserId": "initial",
"platformId": "initial",
"withClassify": False,
"resetSessionMode": True
}
}
}
client.put("/api/v1/settings", json=initial_settings, headers=auth_headers)
with patch('app.api.v1.query.RagService') as MockRagService:
mock_rag = AsyncMock()
mock_rag.send_bench_query = AsyncMock(return_value={"answers": []})
mock_rag.close = AsyncMock()
MockRagService.return_value = mock_rag
query_data = {
"environment": "ift",
"questions": [{"body": "Test", "with_docs": True}]
}
client.post("/api/v1/query/bench", json=query_data, headers=auth_headers)
first_call = mock_rag.send_bench_query.call_args[1]
assert first_call["user_settings"]["bearerToken"] == "initial-token"
updated_settings = {
"settings": {
"ift": {
"apiMode": "bench",
"bearerToken": "updated-token",
"systemPlatform": "updated",
"systemPlatformUser": "updated",
"platformUserId": "updated",
"platformId": "updated",
"withClassify": True,
"resetSessionMode": False
}
}
}
client.put("/api/v1/settings", json=updated_settings, headers=auth_headers)
mock_rag.send_bench_query.reset_mock()
client.post("/api/v1/query/bench", json=query_data, headers=auth_headers)
second_call = mock_rag.send_bench_query.call_args[1]
assert second_call["user_settings"]["bearerToken"] == "updated-token"
assert second_call["user_settings"]["withClassify"] is True