Skip to content

Commit

Permalink
fix: gracefully handle contained Medications
Browse files Browse the repository at this point in the history
Previously, if a MedicationRequest.medicationReference pointed at a
contained Medication, we would raise an exception trying to handle it.

This commit makes sure that we gracefully ignore it and pass it through
to the output table.
  • Loading branch information
mikix committed Jun 30, 2023
1 parent 5ea30b7 commit c814291
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
17 changes: 9 additions & 8 deletions cumulus_etl/etl/tasks/basic_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def __init__(self, *args, **kwargs):
# But let's try initially with keeping it around for the whole task.
self.medication_ids = set()

def scrub_medication(self, medication: dict) -> bool:
def scrub_medication(self, medication: dict | None) -> bool:
"""Scrub incoming medication resources, returns False if it should be skipped"""
if not self.scrubber.scrub_resource(medication): # standard scrubbing
if not medication or not self.scrubber.scrub_resource(medication): # standard scrubbing
return False

# Normally the above is all we'd need to do.
Expand All @@ -75,12 +75,13 @@ async def fetch_medication(self, resource: dict) -> dict | None:
if not reference:
return None

# Don't duplicate medications we've already seen this run.
# This will still duplicate medications from previous runs, but avoiding that feels like more work than it's
# worth - just download em again and push em through (there might be updates to the resources, too!)
if reference in self.medication_ids:
return None
self.medication_ids.add(reference)
if not reference.startswith("#"):
# Don't duplicate medications we've already seen this run.
# This will still duplicate medications from previous runs, but avoiding that feels like more work than it's
# worth - just download em again and push em through (there might be updates to the resources, too!)
if reference in self.medication_ids:
return None
self.medication_ids.add(reference)

try:
medication = await fhir.download_reference(self.task_config.client, reference)
Expand Down
16 changes: 16 additions & 0 deletions tests/etl/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ async def test_inline_codes(self):
df2 = self.format2.write_records.call_args[0][0]
self.assertTrue(df2.empty)

async def test_contained_medications(self):
"""Verify that we pass it through and don't blow up"""
self.make_json("MedicationRequest.1", "A", medicationReference={"reference": "#123"})

await basic_tasks.MedicationRequestTask(self.job_config, self.scrubber).run()

# Confirm we wrote the basic MedicationRequest
self.assertEqual(1, self.format.write_records.call_count)
df = self.format.write_records.call_args[0][0]
self.assertEqual(f'#{self.codebook.db.resource_hash("123")}', df.iloc[0].medicationReference["reference"])

# Confirm we wrote an empty dataframe to the medication table
self.assertEqual(1, self.format2.write_records.call_count)
df2 = self.format2.write_records.call_args[0][0]
self.assertTrue(df2.empty)

@mock.patch("cumulus_etl.fhir.download_reference")
async def test_external_medications(self, mock_download):
"""Verify that we download referenced medications"""
Expand Down

0 comments on commit c814291

Please sign in to comment.