Adding type check for image passed to redact (#1020)

This commit is contained in:
Nile Wilson 2023-01-25 09:22:47 -05:00 коммит произвёл GitHub
Родитель eecbdef23e
Коммит 9671db36d3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 36 добавлений и 2 удалений

Просмотреть файл

@ -48,6 +48,9 @@ class DicomImageRedactorEngine(ImageRedactorEngine):
:return: DICOM instance with redacted pixel data.
"""
# Check input
if type(image) != pydicom.dataset.FileDataset:
raise TypeError("The provided image must be a loaded DICOM instance.")
instance = deepcopy(image)
# Load image for processing

Просмотреть файл

@ -5,10 +5,12 @@ import os
import numpy as np
from PIL import Image
import pydicom
from presidio_image_redactor import DicomImageRedactorEngine
from typing import Union, Tuple
from presidio_image_redactor.dicom_image_redactor_engine import DicomImageRedactorEngine
from typing import Union, Tuple, TypeVar
import pytest
T = TypeVar('T')
SCRIPT_DIR = os.path.dirname(__file__)
TEST_DICOM_PARENT_DIR = f"{SCRIPT_DIR}/test_data"
TEST_DICOM_DIR_1 = f"{SCRIPT_DIR}/test_data/dicom_dir_1"
@ -1112,6 +1114,35 @@ def test_DicomImageRedactorEngine_redact_happy_path(
assert mock_add_redact_box.call_count == 1
@pytest.mark.parametrize(
"image, expected_error_type",
[
(Path(TEST_DICOM_PARENT_DIR), "TypeError"),
("path_here", "TypeError"),
(np.random.randint(255, size=(64, 64)), "TypeError"),
(Image.fromarray(np.random.randint(255, size=(400, 400),dtype=np.uint8)), "TypeError")
],
)
def test_DicomImageRedactorEngine_redact_exceptions(
mock_engine: DicomImageRedactorEngine,
image: T,
expected_error_type: str,
):
"""Test error handling of DicomImageRedactorEngine _redact_single_dicom_image()
Args:
mock_engine (DicomImageRedactorEngine): DicomImageRedactorEngine object.
image (any): Input "image".
expected_error_type (str): Type of error we expect to be raised.
"""
with pytest.raises(Exception) as exc_info:
# Act
mock_engine.redact(image, "contrast", 25, False, "."
)
# Assert
assert expected_error_type == exc_info.typename
# ------------------------------------------------------
# DicomImageRedactorEngine _redact_single_dicom_image()
# ------------------------------------------------------