diff --git a/presidio-image-redactor/presidio_image_redactor/dicom_image_redactor_engine.py b/presidio-image-redactor/presidio_image_redactor/dicom_image_redactor_engine.py index 0788f195..801b1c95 100644 --- a/presidio-image-redactor/presidio_image_redactor/dicom_image_redactor_engine.py +++ b/presidio-image-redactor/presidio_image_redactor/dicom_image_redactor_engine.py @@ -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 diff --git a/presidio-image-redactor/tests/test_dicom_image_redactor_engine.py b/presidio-image-redactor/tests/test_dicom_image_redactor_engine.py index 390d474b..7bea8fcb 100644 --- a/presidio-image-redactor/tests/test_dicom_image_redactor_engine.py +++ b/presidio-image-redactor/tests/test_dicom_image_redactor_engine.py @@ -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() # ------------------------------------------------------