Skip to content

Commit

Permalink
adds an additional unit test, removes a duplicate call to get episode…
Browse files Browse the repository at this point in the history
… count in the patient summaries as we get this already, makes sure we call so that the prefetch_related works refs opal #1565 and opal #1557
  • Loading branch information
fredkingham committed Jun 28, 2018
1 parent 8c9ee6b commit 8a6482a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
12 changes: 8 additions & 4 deletions search/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,17 @@ def sort_patients(self, patients):

def get_patient_summary(self, patient):
result = dict()
demographics = patient.demographics_set.first()
for i in ["first_name", "surname", "hospital_number", "date_of_birth"]:
result[i] = getattr(demographics, i)

# prefetch queries only work with sad times, even though
# demographics are a one per patient thing.
for demographics in patient.demographics_set.all():
for i in [
"first_name", "surname", "hospital_number", "date_of_birth"
]:
result[i] = getattr(demographics, i)
result["start"] = patient.min_start
result["end"] = patient.max_end
result["count"] = patient.episode_count
result["count"] = patient.episode_set.count()
result["patient_id"] = patient.id
result["categories"] = list(patient.episode_set.order_by(
"category_name"
Expand Down
26 changes: 25 additions & 1 deletion search/tests/test_search_query_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from opal.tests.episodes import RestrictedEpisodeCategory

from search.search_rules import SearchRule
from opal.models import Synonym, Gender, Patient
from opal.models import Synonym, Gender, Patient, Episode

from opal.core.test import OpalTestCase

Expand Down Expand Up @@ -365,6 +365,30 @@ def test_fuzzy_query(self):
[patient_2, patient_3, patient_1]
)

def test_get_patients(self):
patient_1, episode_1 = self.new_patient_and_episode_please()
patient_2, episode_2 = self.new_patient_and_episode_please()
episode_3 = patient_1.create_episode()

# these will not be used
self.new_patient_and_episode_please()

query = queries.DatabaseQuery(self.user, [self.name_criteria])
with patch.object(query, "get_episodes") as get_episodes:
get_episodes.return_value = Episode.objects.filter(
id__in=[episode_1.id, episode_2.id, episode_3.id]
)
found = query.get_patients().values_list("id", flat=True)
self.assertEqual(
2, found.count()
)
self.assertEqual(
found[0], patient_1.id
)
self.assertEqual(
found[1], patient_2.id
)

def test_distinct_episodes_for_m2m_fields_containing_synonsyms_and_names(
self
):
Expand Down

0 comments on commit 8a6482a

Please sign in to comment.