Skip to content

Commit

Permalink
Bugfix: return empty but don't 500 if we hit the TB test summary API …
Browse files Browse the repository at this point in the history
…for a patient with no TB Episode
  • Loading branch information
davidmiller committed Jun 20, 2023
1 parent f82d079 commit fc9d74d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions plugins/tb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class TbTests(LoginRequiredViewset):

@patient_from_pk
def retrieve(self, request, patient):
tb_patient = patient.episode_set.filter(category_name=TBEpisode.display_name).exists()
# There is nowhere in the UI where these results are displayed if no TB episode
# exists, but due to questionable implementation details of episode detail views,
# sometimes this can be called for non-tb patients.
# If they have old -ve TB screening tests this raises an exception - prevent that
# by simply returning fast.
if not tb_patient:
return json_response({})

# observations of a singl test are split across models, fetch them
# and group them back by lab number
smears = list(models.AFBSmear.objects.filter(
Expand Down
22 changes: 22 additions & 0 deletions plugins/tb/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,25 @@ def test_get_lab_tests(self):
]
}
self.assertEqual(result.json(), expected)


class TestTBTests(OpalTestCase):

def setUp(self):
self.request = self.rf.get("/")
self.patient, _ = self.new_patient_and_episode_please()
self.url = reverse(
'tb_tests-detail',
kwargs={"pk": self.patient.id},
request=self.request
)
self.assertTrue(
self.client.login(
username=self.user.username, password=self.PASSWORD
)
)

def test_no_TB_epispde(self):
result = self.client.get(self.url)
expected = {}
self.assertEqual(result.json(), expected)

0 comments on commit fc9d74d

Please sign in to comment.