Spaces:
Running
Running
File size: 2,649 Bytes
eb89f47 5959a78 eb89f47 5959a78 eb89f47 5959a78 eb89f47 | 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 | """
DocuMind AI — API Integration Tests
Run: pytest tests/ -v
"""
import os
from fastapi.testclient import TestClient
# Set test environment
os.environ["GROQ_API_KEY"] = "test_key"
os.environ["VECTORSTORE_PATH"] = "./data/test_vectorstore"
os.environ["UPLOAD_FOLDER"] = "./data/test_uploads"
from main import app
client = TestClient(app)
class TestHealth:
def test_health_returns_200(self):
res = client.get("/api/health")
assert res.status_code == 200
def test_health_fields(self):
res = client.get("/api/health")
data = res.json()
assert "status" in data
assert "app_name" in data
assert "version" in data
assert data["status"] == "healthy"
class TestDocuments:
def test_list_documents_empty(self):
res = client.get("/api/documents")
assert res.status_code == 200
data = res.json()
assert "documents" in data
assert "total" in data
assert isinstance(data["documents"], list)
def test_upload_non_pdf_rejected(self):
res = client.post(
"/api/upload",
files={"file": ("test.txt", b"hello world", "text/plain")}
)
assert res.status_code == 400
assert "PDF" in res.json()["detail"]
def test_upload_empty_pdf_handled(self):
# Minimal valid PDF bytes
minimal_pdf = b"%PDF-1.4\n1 0 obj\n<< /Type /Catalog >>\nendobj\n%%EOF"
res = client.post(
"/api/upload",
files={"file": ("empty.pdf", minimal_pdf, "application/pdf")}
)
assert res.status_code == 422
def test_delete_nonexistent_document(self):
res = client.delete("/api/documents/nonexistent_id")
assert res.status_code == 404
class TestQuery:
def test_query_no_documents(self):
res = client.post(
"/api/query",
json={"question": "What is this about?"}
)
assert res.status_code == 404
def test_query_short_question_rejected(self):
res = client.post(
"/api/query",
json={"question": "hi"}
)
assert res.status_code == 422 # Pydantic min_length validation
def test_query_schema_validation(self):
res = client.post(
"/api/query",
json={"question": "Valid question here", "top_k": 999}
)
# top_k max is 20
assert res.status_code == 422
class TestFrontend:
def test_root_returns_html(self):
res = client.get("/")
assert res.status_code == 200
assert "text/html" in res.headers.get("content-type", "")
|