|
11 | 11 | import subprocess
|
12 | 12 | import sys
|
13 | 13 | import time
|
| 14 | +import unittest |
14 | 15 | from functools import lru_cache
|
15 | 16 | from io import BytesIO
|
16 | 17 | from pathlib import Path
|
|
24 | 25 | import numpy as np
|
25 | 26 | import pandas as pd
|
26 | 27 | import pydicom
|
27 |
| -import pytest |
28 | 28 | import requests
|
29 | 29 | import torch
|
30 | 30 | from fastapi import Body, FastAPI, Request, UploadFile, status
|
@@ -145,50 +145,51 @@ def dcm2dictmetadata(ds: pydicom.dataset.Dataset) -> dict[str, dict[str, str]]:
|
145 | 145 | client = TestClient(app)
|
146 | 146 |
|
147 | 147 |
|
148 |
| -def app_url() -> str: |
149 |
| - return "http://0.0.0.0:443" |
150 |
| - |
151 |
| - |
152 |
| -def test_upload_files() -> None: |
153 |
| - with Path("./prm/1-1.dcm").open("rb") as file: |
154 |
| - files = {"files": ("./prm/1-1.dcm", file, "application/dicom")} |
155 |
| - response = client.post(app_url() + "/upload_files", files=files) |
| 148 | +class TestEndpoints(unittest.TestCase): |
| 149 | + def app_url(self: TestEndpoints) -> str: |
| 150 | + return "http://0.0.0.0:8000" |
| 151 | + |
| 152 | + def test_upload_files(self: TestEndpoints) -> None: |
| 153 | + with Path("./prm/1-1.dcm").open("rb") as file: |
| 154 | + files = {"files": ("./prm/1-1.dcm", file, "application/dicom")} |
| 155 | + response = client.post(self.app_url() + "/upload_files", files=files) |
| 156 | + if response.status_code != status.HTTP_200_OK: |
| 157 | + raise AssertionError |
| 158 | + UploadFilesResponse.model_validate(response.json()) |
| 159 | + |
| 160 | + def test_submit_button(self: TestEndpoints) -> None: |
| 161 | + test_options = { |
| 162 | + "skip_deidentification": False, |
| 163 | + "clean_image": True, |
| 164 | + "annotation": False, |
| 165 | + "retain_safe_private": False, |
| 166 | + "retain_uids": False, |
| 167 | + "retain_device_identity": False, |
| 168 | + "retain_patient_characteristics": False, |
| 169 | + "date_processing": "remove", |
| 170 | + "retain_descriptors": False, |
| 171 | + "patient_pseudo_id_prefix": "OrgX - ", |
| 172 | + } |
| 173 | + response = client.post(self.app_url() + "/submit_button", json=test_options) |
156 | 174 | if response.status_code != status.HTTP_200_OK:
|
157 | 175 | raise AssertionError
|
158 |
| - UploadFilesResponse.model_validate(response.json()) |
159 |
| - |
160 |
| - |
161 |
| -def test_submit_button() -> None: |
162 |
| - test_options = { |
163 |
| - "skip_deidentification": False, |
164 |
| - "clean_image": True, |
165 |
| - "annotation": False, |
166 |
| - "retain_safe_private": False, |
167 |
| - "retain_uids": False, |
168 |
| - "retain_device_identity": False, |
169 |
| - "retain_patient_characteristics": False, |
170 |
| - "date_processing": "remove", |
171 |
| - "retain_descriptors": False, |
172 |
| - "patient_pseudo_id_prefix": "OrgX - ", |
173 |
| - } |
174 |
| - response = client.post(app_url() + "/submit_button", json=test_options) |
175 |
| - if response.status_code != status.HTTP_200_OK: |
176 |
| - raise AssertionError |
177 |
| - json_response = response.json() |
178 |
| - desired_hash = "cd6e8eae4006ca7b150c3217667de6b6f7b435f93961d182e72b4da7773884a9" |
179 |
| - hasher = hashlib.sha256() |
180 |
| - block_size = 65536 |
181 |
| - with Path(json_response[0][1]).open("rb") as file: |
182 |
| - buf = file.read(block_size) |
183 |
| - while len(buf) > 0: |
184 |
| - hasher.update(buf) |
185 |
| - buf = file.read(block_size) |
186 |
| - generated_hash = hasher.hexdigest() |
187 |
| - if desired_hash != generated_hash: |
188 |
| - msg = "E: Generated hash doesn't match" |
189 |
| - raise ValueError( |
190 |
| - msg, |
| 176 | + json_response = response.json() |
| 177 | + desired_hash = ( |
| 178 | + "cd6e8eae4006ca7b150c3217667de6b6f7b435f93961d182e72b4da7773884a9" |
191 | 179 | )
|
| 180 | + hasher = hashlib.sha256() |
| 181 | + block_size = 65536 |
| 182 | + with Path(json_response[0][1]).open("rb") as file: |
| 183 | + buf = file.read(block_size) |
| 184 | + while len(buf) > 0: |
| 185 | + hasher.update(buf) |
| 186 | + buf = file.read(block_size) |
| 187 | + generated_hash = hasher.hexdigest() |
| 188 | + if desired_hash != generated_hash: |
| 189 | + msg = "E: Generated hash doesn't match" |
| 190 | + raise ValueError( |
| 191 | + msg, |
| 192 | + ) |
192 | 193 |
|
193 | 194 |
|
194 | 195 | @app.get("/check_existence_of_clean")
|
@@ -1525,9 +1526,7 @@ def meddisc() -> None:
|
1525 | 1526 | ssl_keyfile="tmp/privkey.pem",
|
1526 | 1527 | )
|
1527 | 1528 | else:
|
1528 |
| - results = pytest.main(["-rA", "-o", "cache_dir=tmp", __file__]) |
1529 |
| - if results.value != 0: # type: ignore[attr-defined] |
1530 |
| - sys.exit(results) |
| 1529 | + unittest.main() |
1531 | 1530 |
|
1532 | 1531 |
|
1533 | 1532 | def main_cli() -> None:
|
|
0 commit comments