test that the KeyError is raised for an identifying keyword that has a VR
that is not addressed by the replacement strategy if the element is in the
input dataset
@@ -11,6 +11,7 @@ import pytest
import pydicom
from pymedphys._dicom.anonymise import (
+ anonymise_dataset,
anonymise_file,
is_anonymised_directory,
is_anonymised_file,
@@ -26,16 +27,65 @@ from pymedphys.tests.dicom.test_anonymise import get_test_filepaths
@pytest.mark.pydicom
def test_pseudonymise_file():
+ identifying_keywords_for_pseudo = get_default_pseudonymisation_keywords()
+ logging.info("Using pseudonymisation keywords")
+ replacement_strategy = strategy.pseudonymisation_dispatch
+ logging.info("Using pseudonymisation strategy")
for test_file_path in get_test_filepaths():
- _test_pseudonymise_file_at_path(test_file_path)
+ _test_pseudonymise_file_at_path(
+ test_file_path,
+ keywords=identifying_keywords_for_pseudo,
+ strategy=replacement_strategy,
+ )
+
+
+@pytest.mark.pydicom
+def test_identifier_with_unknown_vr():
+ # The fundamental feature being tested is behaviour in
+ # response to a programmer error.
+ # The programmer error is specification of an identifier that has a VR
+ # that has not been addressed in the strategy.
+ # However, because the strategy is only applied when the identifier is found
+ # in the dataset, the error will only surface in that circumstance
+ replacement_strategy = strategy.pseudonymisation_dispatch
+ logging.info("Using pseudonymisation strategy")
+ identifying_keywords_for_pseudo = ["CodingSchemeURL", "PatientID"]
+ logging.info("Using keyword with VR = UR")
+
+ ds_input = pydicom.Dataset()
+ ds_input.PatientID = "ABC123"
+ # not expected to cause problems if the identifier with unknown VR is not in the data
+ assert (
+ anonymise_dataset(
+ ds_input,
+ replacement_strategy=replacement_strategy,
+ identifying_keywords=identifying_keywords_for_pseudo,
+ )
+ is not None
+ )
+
+ # should raise the error if the identifier with unknown VR is in the data
+ with pytest.raises(KeyError):
+ ds_input.CodingSchemeURL = "https://scheming.coders.co.nz"
+ anonymise_dataset(
+ ds_input,
+ replacement_strategy=replacement_strategy,
+ identifying_keywords=identifying_keywords_for_pseudo,
+ )
-def _test_pseudonymise_file_at_path(test_file_path):
+def _test_pseudonymise_file_at_path(test_file_path, keywords=None, strategy=None):
assert not is_anonymised_file(test_file_path)
+ if keywords is None:
identifying_keywords_for_pseudo = get_default_pseudonymisation_keywords()
logging.info("Using pseudonymisation keywords")
+ else:
+ identifying_keywords_for_pseudo = keywords
+ if strategy is None:
replacement_strategy = strategy.pseudonymisation_dispatch
logging.info("Using pseudonymisation strategy")
+ else:
+ replacement_strategy = strategy
with tempfile.TemporaryDirectory() as output_directory:
pseudonymised_file_path = anonymise_file(
