ask-the-web-agent / tests /test_api.py
debashis2007's picture
Upload folder using huggingface_hub
75bea1c verified
"""Integration tests for the API."""
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch, AsyncMock, MagicMock
class TestAPIEndpoints:
"""Tests for API endpoints."""
@pytest.fixture
def client(self):
"""Create test client."""
from src.api.app import app
return TestClient(app)
def test_root_endpoint(self, client):
"""Test root endpoint."""
response = client.get("/")
assert response.status_code == 200
data = response.json()
assert "name" in data
assert "Ask-the-Web" in data["name"]
def test_health_endpoint(self, client):
"""Test health check endpoint."""
with patch("src.api.routes.get_settings") as mock_settings:
mock_settings.return_value = MagicMock(
openai_api_key="test-key",
anthropic_api_key=None,
tavily_api_key="test-tavily",
)
response = client.get("/api/v1/health")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert "version" in data
@patch("src.api.routes.AskTheWebAgent")
def test_query_endpoint(self, mock_agent_class, client):
"""Test query endpoint."""
# Setup mock
mock_agent = AsyncMock()
mock_agent.query = AsyncMock(return_value=MagicMock(
answer="Test answer",
sources=[{"title": "Test", "url": "https://test.com", "snippet": "snippet"}],
follow_up_questions=["Follow up?"],
confidence=0.8,
metadata={},
))
mock_agent_class.return_value = mock_agent
response = client.post(
"/api/v1/query",
json={"query": "What is Python?"},
)
assert response.status_code == 200
data = response.json()
assert "answer" in data
assert "sources" in data
assert "confidence" in data
def test_query_validation(self, client):
"""Test query validation."""
# Empty query should fail
response = client.post(
"/api/v1/query",
json={"query": ""},
)
assert response.status_code == 422 # Validation error
def test_conversation_not_found(self, client):
"""Test getting non-existent conversation."""
response = client.get("/api/v1/conversation/nonexistent-id")
assert response.status_code == 404
def test_delete_conversation_not_found(self, client):
"""Test deleting non-existent conversation."""
response = client.delete("/api/v1/conversation/nonexistent-id")
assert response.status_code == 404