Skip to content

Commit

Permalink
Merge pull request #530 from bento-platform/fix/sampled-tissue-autoco…
Browse files Browse the repository at this point in the history
…mplete

fix(phenopackets): sampled tissue autocomplete crash w/ None + no q
  • Loading branch information
davidlougheed authored Aug 27, 2024
2 parents 755b39a + 26f0609 commit 0346ef7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
8 changes: 7 additions & 1 deletion chord_metadata_service/phenopackets/autocomplete_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def get_result_label(self, item):
return item.sampled_tissue["label"]

def get_queryset(self):
qs = Biosample.objects.all().distinct("sampled_tissue__label").order_by("sampled_tissue__label")
qs = (
Biosample.objects
.filter(sampled_tissue__isnull=False)
.distinct("sampled_tissue__label")
.order_by("sampled_tissue__label")
)

if self.q:
# looks for a matching string everywhere in sampled_tissue label field
qs = qs.filter(sampled_tissue__label__icontains=self.q)
Expand Down
15 changes: 10 additions & 5 deletions chord_metadata_service/phenopackets/tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,14 +439,19 @@ def valid_biosample_1(individual, procedure=VALID_PROCEDURE_1):
)


def valid_biosample_2(individual, procedure=VALID_PROCEDURE_2):
def valid_biosample_2(individual, procedure=VALID_PROCEDURE_2, **kwargs):
if "sampled_tissue" in kwargs:
sampled_tissue = kwargs["sampled_tissue"]
else:
sampled_tissue = {
"id": "UBERON_0001256",
"label": "urinary bladder"
}

return dict(
id='biosample_id:2',
individual=individual,
sampled_tissue={
"id": "UBERON_0001256",
"label": "urinary bladder"
},
sampled_tissue=sampled_tissue,
description='This is a test biosample.',
taxonomy={
"id": "NCBITaxon:9606",
Expand Down
29 changes: 29 additions & 0 deletions chord_metadata_service/phenopackets/tests/test_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def setUp(self):
self.biosample_1 = m.Biosample.objects.create(**c.valid_biosample_1(self.individual))
self.biosample_2 = m.Biosample.objects.create(**c.valid_biosample_2(self.individual))

def test_autocomplete_response_no_q(self):
response = self.client.get('/api/biosample_sampled_tissue_autocomplete')
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()
self.assertEqual(len(response_data["results"]), 2)

def test_autocomplete_response(self):
response = self.client.get('/api/biosample_sampled_tissue_autocomplete', {'q': 'bladder'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -62,3 +68,26 @@ def test_autocomplete_response_2(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()
self.assertEqual(len(response_data["results"]), 1)


class BiosapleSampledTissueAutocompleteWithNoneTest(APITestCase):
""" Test module for sampled tissue label autocomplete with None present for sampled tissue. """

def setUp(self):
self.individual = m.Individual.objects.create(**c.VALID_INDIVIDUAL_1)
self.biosample_1 = m.Biosample.objects.create(**c.valid_biosample_1(self.individual))
self.biosample_2 = m.Biosample.objects.create(**c.valid_biosample_2(self.individual, sampled_tissue=None))

def test_autocomplete_response_no_q(self):
# no q set used to trigger an error, since it was assumed sampled_tissue would be set; this is a regression test
# for that case.
response = self.client.get('/api/biosample_sampled_tissue_autocomplete')
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()
self.assertEqual(len(response_data["results"]), 1)

def test_autocomplete_response(self):
response = self.client.get('/api/biosample_sampled_tissue_autocomplete', {'q': 'bladder'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()
self.assertEqual(len(response_data["results"]), 1)

0 comments on commit 0346ef7

Please sign in to comment.