File size: 1,796 Bytes
b6f9fa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from fastapi.testclient import TestClient
from src.api.main import app

client = TestClient(app)

def test_health_endpoint():
    """Test that the /health endpoint correctly reports system status."""
    response = client.get("/health")
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "ok"
    assert "ollama_available" in data

def test_evaluate_endpoint():
    """Test the /evaluate endpoint with mock claims."""
    payload = {
        "question": "Is Metformin safe?",
        "answer": "Metformin is a safe and effective drug. It is recommended.",
        "context_chunks": [
            {
                "chunk_id": "mock-1",
                "text": "Metformin is a first-line medication for the treatment of type 2 diabetes. It is safe.",
                "source": "mock_db",
                "pub_type": "research_abstract",
                "pub_year": 2024,
                "title": "Study on Metformin safety"
            }
        ],
        "run_ragas": False
    }

    # Since the evaluation modules load heavy ML models, 
    # the first test call might take 10-15s to run.
    response = client.post("/evaluate", json=payload)
    assert response.status_code == 200
    
    data = response.json()
    assert "composite_score" in data
    assert "hrs" in data
    assert data["risk_band"] in ["LOW", "MODERATE", "HIGH", "CRITICAL"]
    assert "faithfulness" in data["module_results"]

def test_query_invalid_params():
    """Test the /query validation rules."""
    payload = {
        "question": "Hi",  # 2 chars — below min_length=5, triggers 422
        "top_k": 5
    }
    response = client.post("/query", json=payload)
    assert response.status_code == 422  # Unprocessable Entity (Pydantic validation error)