Skip to content

Commit

Permalink
Replace pytest with unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
uw0s committed Apr 15, 2024
1 parent a88ce78 commit 461898c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 47 deletions.
2 changes: 1 addition & 1 deletion python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tmp:
mkdir $@

tmp/all-done: .dockerignore .gitignore Dockerfile main.py pyproject.toml
docker container run --publish 443:443\
docker container run --publish 8000:8000\
$$(command -v nvidia-container-toolkit > /dev/null && printf '%s' '--gpus all') \
$$(test -t 0 && printf '%s' '--interactive --tty') \
--detach-keys 'ctrl-^,ctrl-^' \
Expand Down
89 changes: 44 additions & 45 deletions python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import subprocess
import sys
import time
import unittest
from functools import lru_cache
from io import BytesIO
from pathlib import Path
Expand All @@ -24,7 +25,6 @@
import numpy as np
import pandas as pd
import pydicom
import pytest
import requests
import torch
from fastapi import Body, FastAPI, Request, UploadFile, status
Expand Down Expand Up @@ -145,50 +145,51 @@ def dcm2dictmetadata(ds: pydicom.dataset.Dataset) -> dict[str, dict[str, str]]:
client = TestClient(app)


def app_url() -> str:
return "http://0.0.0.0:443"


def test_upload_files() -> None:
with Path("./prm/1-1.dcm").open("rb") as file:
files = {"files": ("./prm/1-1.dcm", file, "application/dicom")}
response = client.post(app_url() + "/upload_files", files=files)
class TestEndpoints(unittest.TestCase):
def app_url(self: TestEndpoints) -> str:
return "http://0.0.0.0:8000"

def test_upload_files(self: TestEndpoints) -> None:
with Path("./prm/1-1.dcm").open("rb") as file:
files = {"files": ("./prm/1-1.dcm", file, "application/dicom")}
response = client.post(self.app_url() + "/upload_files", files=files)
if response.status_code != status.HTTP_200_OK:
raise AssertionError
UploadFilesResponse.model_validate(response.json())

def test_submit_button(self: TestEndpoints) -> None:
test_options = {
"skip_deidentification": False,
"clean_image": True,
"annotation": False,
"retain_safe_private": False,
"retain_uids": False,
"retain_device_identity": False,
"retain_patient_characteristics": False,
"date_processing": "remove",
"retain_descriptors": False,
"patient_pseudo_id_prefix": "OrgX - ",
}
response = client.post(self.app_url() + "/submit_button", json=test_options)
if response.status_code != status.HTTP_200_OK:
raise AssertionError
UploadFilesResponse.model_validate(response.json())


def test_submit_button() -> None:
test_options = {
"skip_deidentification": False,
"clean_image": True,
"annotation": False,
"retain_safe_private": False,
"retain_uids": False,
"retain_device_identity": False,
"retain_patient_characteristics": False,
"date_processing": "remove",
"retain_descriptors": False,
"patient_pseudo_id_prefix": "OrgX - ",
}
response = client.post(app_url() + "/submit_button", json=test_options)
if response.status_code != status.HTTP_200_OK:
raise AssertionError
json_response = response.json()
desired_hash = "cd6e8eae4006ca7b150c3217667de6b6f7b435f93961d182e72b4da7773884a9"
hasher = hashlib.sha256()
block_size = 65536
with Path(json_response[0][1]).open("rb") as file:
buf = file.read(block_size)
while len(buf) > 0:
hasher.update(buf)
buf = file.read(block_size)
generated_hash = hasher.hexdigest()
if desired_hash != generated_hash:
msg = "E: Generated hash doesn't match"
raise ValueError(
msg,
json_response = response.json()
desired_hash = (
"cd6e8eae4006ca7b150c3217667de6b6f7b435f93961d182e72b4da7773884a9"
)
hasher = hashlib.sha256()
block_size = 65536
with Path(json_response[0][1]).open("rb") as file:
buf = file.read(block_size)
while len(buf) > 0:
hasher.update(buf)
buf = file.read(block_size)
generated_hash = hasher.hexdigest()
if desired_hash != generated_hash:
msg = "E: Generated hash doesn't match"
raise ValueError(
msg,
)


@app.get("/check_existence_of_clean")
Expand Down Expand Up @@ -1525,9 +1526,7 @@ def meddisc() -> None:
ssl_keyfile="tmp/privkey.pem",
)
else:
results = pytest.main(["-rA", "-o", "cache_dir=tmp", __file__])
if results.value != 0: # type: ignore[attr-defined]
sys.exit(results)
unittest.main()


def main_cli() -> None:
Expand Down
1 change: 0 additions & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dependencies = [
"pydicom==2.4.4",
"pylibjpeg-libjpeg==2.1.0",
"pylibjpeg==2.0.0",
"pytest==8.1.1",
"python-multipart==0.0.9",
"segment-anything@https://api.github.com/repos/facebookresearch/segment-anything/tarball/main",
"tensorflow==2.16.1",
Expand Down

0 comments on commit 461898c

Please sign in to comment.