-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_main.py
More file actions
57 lines (46 loc) · 1.89 KB
/
Copy pathtest_main.py
File metadata and controls
57 lines (46 loc) · 1.89 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
import sys
import pytest
from unittest.mock import MagicMock
# --- 1. MOCK WEASYPRINT BEFORE IMPORTING FASTAPI ---
mock_wp = MagicMock()
sys.modules['weasyprint'] = mock_wp
sys.modules['weasyprint.HTML'] = mock_wp
# --- 2. NOW BRING IN THE APP ---
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
# --- CORE APP TESTS ---
def test_read_root():
"""Test the root endpoint (/) for status code and exact JSON structure."""
response = client.get("/")
assert response.status_code == 200
data = response.json()
assert "name" in data
assert "version" in data
assert "organization" in data
assert data["name"] == "ASL Methods Parameter Generator API Service"
def test_cors_middleware():
"""Test that CORS middleware allows external origins."""
headers = {"Origin": "http://localhost:3000"}
response = client.get("/", headers=headers)
assert response.status_code == 200
assert response.headers.get("access-control-allow-origin") == "*"
# --- REPORT ROUTER TESTS ---
def test_process_dicom_no_files():
"""Test that /process/dicom correctly rejects requests missing DICOM files."""
response = client.post("/api/report/process/dicom")
assert response.status_code == 400
assert response.json()["detail"] == "No DICOM files provided"
def test_report_pdf_missing_payload():
"""Test that /report-pdf crashes when expected nested keys are missing."""
# TestClient raises the actual Python KeyError instead of a 500 status code
with pytest.raises(KeyError):
client.post("/api/report/report-pdf", json={})
def test_process_bids_accepts_form_data():
"""Test that /process/bids processes form data."""
# TestClient raises the ValueError from the Enum mismatch
with pytest.raises(ValueError):
client.post(
"/api/report/process/bids",
data={"modality": "asl"}
)