forked from Project-MONAI/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
123 lines (91 loc) · 4.06 KB
/
test_api.py
File metadata and controls
123 lines (91 loc) · 4.06 KB
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
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
API Endpoint Tests
Tests for FastAPI endpoints including health checks and prediction.
"""
import pytest
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
class TestRootEndpoint:
"""Tests for root endpoint."""
def test_root_returns_200(self):
"""Test that root endpoint returns 200 OK."""
response = client.get("/")
assert response.status_code == 200
def test_root_returns_api_info(self):
"""Test that root endpoint returns API information."""
response = client.get("/")
data = response.json()
assert "name" in data
assert "version" in data
assert "endpoints" in data
assert data["name"] == "MONAI Inference API"
class TestHealthEndpoint:
"""Tests for health check endpoint."""
def test_health_returns_200(self):
"""Test that health endpoint returns 200 OK."""
response = client.get("/health")
assert response.status_code == 200
def test_health_returns_status(self):
"""Test that health endpoint returns status information."""
response = client.get("/health")
data = response.json()
assert "status" in data
assert "model_loaded" in data
assert "device" in data
def test_health_status_format(self):
"""Test that health response has expected format."""
response = client.get("/health")
data = response.json()
assert isinstance(data["model_loaded"], bool)
assert isinstance(data["device"], str)
assert data["status"] in ["healthy", "model_not_loaded"]
class TestPredictEndpoint:
"""Tests for prediction endpoint."""
def test_predict_requires_file(self):
"""Test that predict endpoint requires a file."""
response = client.post("/predict")
assert response.status_code == 422 # Unprocessable Entity
def test_predict_rejects_invalid_format(self):
"""Test that predict endpoint rejects non-NIfTI files."""
# Create a fake file with wrong extension
files = {"file": ("test.txt", b"fake content", "text/plain")}
response = client.post("/predict", files=files)
assert response.status_code == 400
assert "Invalid file format" in response.json()["detail"]
def test_predict_accepts_nifti_extension(self):
"""Test that predict endpoint accepts .nii files."""
# Note: This will fail inference if model not loaded,
# but should pass file validation
files = {"file": ("test.nii", b"fake nifti data", "application/octet-stream")}
response = client.post("/predict", files=files)
# Should get past file validation (not 400)
# May get 503 (model not loaded) or 500 (invalid data)
assert response.status_code in [500, 503]
class TestDocumentation:
"""Tests for API documentation endpoints."""
def test_docs_available(self):
"""Test that Swagger UI docs are available."""
response = client.get("/docs")
assert response.status_code == 200
def test_redoc_available(self):
"""Test that ReDoc documentation is available."""
response = client.get("/redoc")
assert response.status_code == 200
class TestCORS:
"""Tests for CORS middleware."""
def test_cors_headers_present(self):
"""Test that CORS headers are present in responses."""
response = client.get("/health")
# CORS headers should be present
assert "access-control-allow-origin" in response.headers