Skip to content

Commit

Permalink
Update test_replace_identifiers.py
Browse files Browse the repository at this point in the history
Adding unit tests to validate results of multiple actions on the same field.
  • Loading branch information
wetzelj committed Aug 6, 2020
1 parent 72cd3f6 commit 861e92f
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions deid/tests/test_replace_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,100 @@ def test_strip_sequences(self):
for tag in result[0]:
self.assertFalse(isinstance(tag.value, Sequence))

def test_jitter_compounding(self):
"""
Testing jitter compounding: Checks to ensure that multiple jitter rules applied to the same field result
in both rules being applied. While in practice this may be somewhat of a nonsensical use case when large recipes
exist multiple rules may inadvertently be defined. In prior versions of pydicom/deid rules were additive and
recipes are built in that manner. This test ensures consistency with prior versions.
%header
JITTER StudyDate 1
JITTER StudyDate 2
"""
print("Test jitter compounding")
dicom_file = get_file(self.dataset)

actions = [
{"action": "JITTER", "field": "StudyDate", "value": "1"},
{"action": "JITTER", "field": "StudyDate", "value": "2"}
]
recipe = create_recipe(actions)
result = replace_identifiers(
dicom_files=dicom_file,
deid=recipe,
save=False,
remove_private=False,
strip_sequences=True,
)

self.assertEqual(1, len(result))
self.assertEqual(151, len(result[0]))
self.assertEqual("20230104", result[0]["StudyDate"].value)

def test_addremove_compounding(self):
"""
Testing add/remove compounding: Checks to ensure that multiple rules applied to the same field result
in both rules being applied. While in practice this may be somewhat of a nonsensical use case when large recipes
exist multiple rules may inadvertently be defined. In prior versions of pydicom/deid rules were additive and
recipes are built in that manner. This test ensures consistency with prior versions.
%header
ADD PatientIdentityRemoved Yeppers!
REMOVE PatientIdentityRemoved
"""
print("Test addremove compounding")
dicom_file = get_file(self.dataset)

actions = [
{"action": "ADD", "field": "PatientIdentityRemoved", "value": "Yeppers!"},
{"action": "REMOVE", "field": "PatientIdentityRemoved"}
]
recipe = create_recipe(actions)
result = replace_identifiers(
dicom_files=dicom_file,
deid=recipe,
save=False,
remove_private=False,
strip_sequences=True,
)

self.assertEqual(1, len(result))
self.assertEqual(151, len(result[0]))
with self.assertRaises(KeyError):
willerror = result[0]["PatientIdentityRemoved"].value

def test_removeadd_compounding(self):
"""
Testing remove/add compounding: Checks to ensure that multiple rules applied to the same field result
in both rules being applied. While in practice this may be somewhat of a nonsensical use case when large recipes
exist multiple rules may inadvertently be defined. In prior versions of pydicom/deid rules were additive and
recipes are built in that manner. This test ensures consistency with prior versions.
%header
REMOVE StudyDate
ADD StudyDate 20200805
"""
print("Test remove/add compounding")
dicom_file = get_file(self.dataset)

actions = [
{"action": "REMOVE", "field": "PatientID"},
{"action": "ADD", "field": "PatientID", "value": "123456"}
]
recipe = create_recipe(actions)
result = replace_identifiers(
dicom_files=dicom_file,
deid=recipe,
save=False,
remove_private=False,
strip_sequences=True,
)

self.assertEqual(1, len(result))
self.assertEqual(151, len(result[0]))
self.assertEqual("123456", result[0]["PatientID"].value)

# MORE TESTS NEED TO BE WRITTEN TO TEST SEQUENCES


Expand Down

0 comments on commit 861e92f

Please sign in to comment.