File size: 4,804 Bytes
4a8b134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Comprehensive Test Suite for Drug Repurposing API
Tests both with pytest and standalone requests
"""
import pytest
import requests
import json
import time
from fastapi.testclient import TestClient
from app.main import app

client = TestClient(app)

# ============================================================================
# PYTEST UNIT TESTS - Run with: pytest test_api.py
# ============================================================================

class TestHealthCheck:
    """Test health check endpoint"""
    
    def test_health_check(self):
        """Test health endpoint returns 200"""
        response = client.get("/health")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "healthy"
        assert "version" in data
        assert "service" in data


class TestRootEndpoint:
    """Test root endpoint"""
    
    def test_root_endpoint(self):
        """Test root endpoint returns API info"""
        response = client.get("/")
        assert response.status_code == 200
        data = response.json()
        assert "name" in data
        assert "version" in data
        assert "docs" in data


class TestModelStatus:
    """Test model status endpoint"""
    
    def test_model_status(self):
        """Test model status endpoint"""
        response = client.get("/api/v1/model-status")
        assert response.status_code == 200
        data = response.json()
        assert "model" in data
        assert "device" in data
        assert "gpu_available" in data


class TestDiseaseTargets:
    """Test disease target endpoint"""
    
    def test_disease_targets_valid_disease(self):
        """Test with valid disease name"""
        payload = {
            "disease_name": "Type 2 Diabetes",
            "top_n": 5
        }
        response = client.post("/api/v1/disease-targets", json=payload)
        # Response depends on API availability
        assert response.status_code in [200, 404, 500]
        """Test with invalid disease name"""
        payload = {
            "disease_name": "NonexistentDisease12345XYZ",
            "top_n": 5
        }
        response = client.post("/api/v1/disease-targets", json=payload)
        assert response.status_code in [404, 500]


class TestProteinSequences:
    """Test protein sequence endpoint"""
    
    def test_protein_sequences_empty_list(self):
        """Test with empty target list"""
        response = client.post("/api/v1/protein-sequences", json=[])
        assert response.status_code == 200
        data = response.json()
        assert data["total_requested"] == 0


class TestDrugLibrary:
    """Test drug library endpoint"""
    
    def test_drug_library(self):
        """Test drug library endpoint"""
        response = client.get("/api/v1/drug-library")
        assert response.status_code == 200
        data = response.json()
        assert "total_drugs" in data
        assert "drugs" in data
        assert isinstance(data["drugs"], list)


class TestRequestValidation:
    """Test request validation"""
    
    def test_invalid_top_n_too_large(self):
        """Test validation of top_n parameter"""
        payload = {
            "disease_name": "Type 2 Diabetes",
            "top_n": 101  # Max is 100
        }
        response = client.post("/api/v1/disease-targets", json=payload)
        # Should either accept (clamp) or reject
        # Current implementation clamps to 100
        assert response.status_code in [200, 422]
    
    def test_missing_required_field(self):
        """Test missing required field"""
        payload = {
            "top_n": 10
            # Missing disease_name
        }
        response = client.post("/api/v1/disease-targets", json=payload)
        assert response.status_code == 422


class TestScreeningPipeline:
    """Test complete screening pipeline"""
    
    def test_screening_with_mock_data(self):
        """Test screening endpoint with mock data"""
        payload = {
            "disease_name": "Type 2 Diabetes",
            "min_score": 0.5,
            "top_n_targets": 3,
            "known_drugs": ["Metformin"]
        }
        response = client.post("/api/v1/screen", json=payload)
        # Response depends on external API availability
        assert response.status_code in [200, 404, 500]
        
        if response.status_code == 200:
            data = response.json()
            assert "disease_name" in data
            assert "total_targets_found" in data
            assert "total_drugs_screened" in data
            assert "top_candidates" in data
            assert "warnings" in data


if __name__ == "__main__":
    # Run with: python test_api.py
    # Or pytest: pytest test_api.py -v
    pytest.main([__file__, "-v"])
    pytest.main([__file__, "-v"])