Spaces:
Build error
Build error
File size: 5,293 Bytes
ffb1fbb 7a473e5 ffb1fbb 7a473e5 ffb1fbb 7a473e5 ffb1fbb 7a473e5 ffb1fbb 7a473e5 ffb1fbb 7a473e5 ffb1fbb 7a473e5 ffb1fbb 7a473e5 ffb1fbb 7a473e5 ffb1fbb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | """
Integration Tests for FastAPI Endpoints
Tests for API endpoints using TestClient.
"""
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch, MagicMock
@pytest.fixture
def client():
"""Create a test client for the API."""
from src.api.main import app
return TestClient(app)
class TestHealthEndpoints:
"""Tests for health check endpoints."""
def test_health_check(self, client):
"""Test basic health check endpoint."""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
def test_health_detailed(self, client):
"""Test detailed health check endpoint."""
response = client.get("/health/detailed")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert "components" in data
def test_readiness_check(self, client):
"""Test readiness check endpoint."""
response = client.get("/ready")
assert response.status_code in [200, 503]
def test_liveness_check(self, client):
"""Test liveness check endpoint."""
response = client.get("/live")
assert response.status_code == 200
class TestQueryEndpoints:
"""Tests for query endpoints."""
@patch("src.api.routes.query.get_pipeline")
def test_query_endpoint(self, mock_get_pipeline, client):
"""Test the /api/query endpoint."""
# Setup mock pipeline
mock_pipeline = MagicMock()
mock_pipeline.query.return_value = {
"answer": "This is a test answer.",
"question": "Test question?",
"sources": [],
}
mock_get_pipeline.return_value = mock_pipeline
response = client.post(
"/api/query",
json={
"question": "Test question?",
"collection_name": "test_collection",
"top_k": 5,
}
)
assert response.status_code == 200
data = response.json()
assert "answer" in data
def test_query_validation_error(self, client):
"""Test query with invalid request body."""
response = client.post(
"/api/query",
json={} # Missing required 'question' field
)
assert response.status_code == 422 # Validation error
def test_query_empty_question(self, client):
"""Test query with empty question."""
response = client.post(
"/api/query",
json={"question": ""}
)
# Should return 422 or handle gracefully
assert response.status_code in [200, 422]
class TestIngestEndpoints:
"""Tests for ingestion endpoints."""
@patch("src.api.routes.ingest.create_vector_store")
@patch("src.api.routes.ingest.get_embeddings")
@patch("src.api.routes.ingest.DocumentChunker")
def test_ingest_text(self, mock_chunker_class, mock_get_embeddings, mock_create_store, client):
"""Test the /api/ingest/text endpoint."""
# Setup mocks
mock_chunker = MagicMock()
mock_chunker.chunk_documents.return_value = [MagicMock()]
mock_chunker_class.return_value = mock_chunker
mock_embeddings = MagicMock()
mock_get_embeddings.return_value = mock_embeddings
mock_store = MagicMock()
mock_create_store.return_value = mock_store
response = client.post(
"/api/ingest/text",
json={
"text": "This is test content to ingest.",
"collection_name": "test_collection",
}
)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
def test_ingest_files_no_files(self, client):
"""Test file ingestion with no files."""
response = client.post("/api/ingest")
# Should fail without files
assert response.status_code == 422
class TestErrorHandling:
"""Tests for error handling."""
def test_404_endpoint(self, client):
"""Test accessing non-existent endpoint."""
response = client.get("/api/nonexistent")
assert response.status_code == 404
def test_method_not_allowed(self, client):
"""Test using wrong HTTP method."""
response = client.get("/api/query") # Should be POST
assert response.status_code == 405
@patch("src.api.routes.query.get_pipeline")
def test_internal_error_handling(self, mock_get_pipeline, client):
"""Test that internal errors are handled gracefully."""
mock_pipeline = MagicMock()
mock_pipeline.query.side_effect = Exception("Internal error")
mock_get_pipeline.return_value = mock_pipeline
response = client.post(
"/api/query",
json={"question": "Test?"}
)
# Should return 500 with error details
assert response.status_code == 500
data = response.json()
assert "error" in data or "detail" in data
|