learning_ai_common_plat/services/extraction-service/python/tests/test_app.py
saravanakumardb1 c9d5c0caed feat(extraction): integration tests + Python tests + fix langextract API
- 6 route integration tests (mock sidecar via vitest vi.mock)
- 12 task CRUD route tests (mock repository)
- 29 Python tests: 10 extractor, 12 models, 7 app endpoints
- Fix extractor.py: correct lx.extract() API (text_or_documents positional, prompt_description)
- Mock fallback when no GEMINI_API_KEY or USE_MOCK_EXTRACTOR=true
- 46 TS tests + 29 Python tests = 75 total
2026-02-14 13:49:18 -08:00

82 lines
2.1 KiB
Python

"""
Unit tests for FastAPI app endpoints.
Uses TestClient to test endpoints without starting the server.
"""
import pytest
from fastapi.testclient import TestClient
from src.app import app
client = TestClient(app)
def test_health_endpoint():
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
assert data["version"] == "0.1.0"
def test_extract_endpoint_minimal():
response = client.post(
"/extract",
json={"text": "We had a meeting today to discuss deadlines"},
)
assert response.status_code == 200
data = response.json()
assert "extractions" in data
assert "metadata" in data
assert data["metadata"]["char_count"] == len("We had a meeting today to discuss deadlines")
def test_extract_endpoint_with_task_id():
response = client.post(
"/extract",
json={
"text": "John decided to ship by Friday",
"task_id": "transcript-extraction",
"model_id": "gemini-2.5-flash",
},
)
assert response.status_code == 200
data = response.json()
assert isinstance(data["extractions"], list)
def test_extract_endpoint_rejects_empty_text():
response = client.post("/extract", json={"text": ""})
assert response.status_code == 422
def test_extract_endpoint_request_id_forwarding():
response = client.post(
"/extract",
json={"text": "test text for request ID"},
headers={"x-request-id": "test-req-123"},
)
assert response.status_code == 200
def test_extract_batch_endpoint():
response = client.post(
"/extract/batch",
json={
"requests": [
{"text": "First document about a meeting"},
{"text": "Second document with action items to do"},
],
},
)
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert len(data) == 2
def test_extract_batch_rejects_empty():
response = client.post("/extract/batch", json={"requests": []})
assert response.status_code == 422