Skip to content

Commit

Permalink
Merge pull request #361 from bento-platform/utilspy-cleanup
Browse files Browse the repository at this point in the history
fix issue with 0-age individuals, use interval notation for public endpoints, and clean up restapi.utils
  • Loading branch information
davidlougheed committed Nov 22, 2022
2 parents 86748f8 + 87b6f89 commit 4e9d063
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 115 deletions.
2 changes: 1 addition & 1 deletion chord_metadata_service/package.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[package]
name = katsu
version = 2.15.2
version = 2.15.3
authors = Ksenia Zaytseva, David Lougheed, Simon Chénard, Romain Grégoire, Paul Pillot, Son Chau
16 changes: 8 additions & 8 deletions chord_metadata_service/patients/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def test_public_filtering_extra_properties_invalid_3(self):
def test_public_filtering_extra_properties_range_1(self):
# extra_properties range search (both min and max ranges, single value)
response = self.client.get(
'/api/public?lab_test_result_value=200-300'
'/api/public?lab_test_result_value=[200, 300)'
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_obj = response.json()
Expand All @@ -433,7 +433,7 @@ def test_public_filtering_extra_properties_range_1(self):
if db_count <= self.response_threshold:
self.assertEqual(response_obj, settings.INSUFFICIENT_DATA_AVAILABLE)
else:
self.assertEqual(db_count, response_obj['count'])
self.assertEqual(db_count, response_obj["count"])

@override_settings(CONFIG_PUBLIC=CONFIG_PUBLIC_TEST)
def test_public_filtering_extra_properties_range_2(self):
Expand Down Expand Up @@ -475,7 +475,7 @@ def test_public_filtering_extra_properties_range_3(self):
def test_public_filtering_extra_properties_wrong_range(self):
# extra_properties range search, unauthorized range
response = self.client.get(
'/api/public?lab_test_result_value=100-200'
'/api/public?lab_test_result_value=[100, 200)'
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_obj = response.json()
Expand Down Expand Up @@ -525,7 +525,7 @@ def test_public_filtering_extra_properties_range_string_2(self):
def test_public_filtering_extra_properties_multiple_ranges_1(self):
# extra_properties range search (both min and max range, multiple values)
response = self.client.get(
'/api/public?lab_test_result_value=< 200&baseline_creatinine=100-150'
'/api/public?lab_test_result_value=< 200&baseline_creatinine=[100, 150)'
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_obj = response.json()
Expand Down Expand Up @@ -615,7 +615,7 @@ def setUp(self):
@override_settings(CONFIG_PUBLIC=CONFIG_PUBLIC_TEST)
def test_public_filtering_age_range(self):
# age valid range search
response = self.client.get('/api/public?age=20-30')
response = self.client.get('/api/public?age=[20, 30)')
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_obj = response.json()
db_count = Individual.objects.filter(age_numeric__gte=20, age_numeric__lt=30).count()
Expand All @@ -628,23 +628,23 @@ def test_public_filtering_age_range(self):
@override_settings(CONFIG_PUBLIC=CONFIG_PUBLIC_TEST)
def test_public_filtering_age_invalid_range(self):
# age invalid range max search
response = self.client.get('/api/public?age=10-50')
response = self.client.get('/api/public?age=[10, 50)')
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_obj = response.json()
self.assertEqual(response_obj["code"], 400)

@override_settings(CONFIG_PUBLIC=CONFIG_PUBLIC_TEST_SEARCH_SEX_ONLY)
def test_public_filtering_age_range_min_and_max_no_age_in_config(self):
# test with config without age field, returns error
response = self.client.get('/api/public?age=20-30')
response = self.client.get('/api/public?age=[20, 30)')
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_obj = response.json()
self.assertEqual(response_obj["code"], 400)

@override_settings(CONFIG_PUBLIC={})
def test_public_filtering_age_range_min_and_max_no_config(self):
# test when config is not provided, returns NO_PUBLIC_DATA_AVAILABLE
response = self.client.get('/api/public?age=20-30')
response = self.client.get('/api/public?age=[20, 30)')
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_obj = response.json()
self.assertIsInstance(response_obj, dict)
Expand Down
2 changes: 2 additions & 0 deletions chord_metadata_service/restapi/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ def public_overview(_request):
stats = get_range_stats(field_props)
elif field_props["datatype"] == "date":
stats = get_date_stats(field_props)
else:
raise NotImplementedError()

response["fields"][field] = {
**field_props,
Expand Down
14 changes: 7 additions & 7 deletions chord_metadata_service/restapi/tests/test_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def setUp(self):
}
}

def test_config_with_tappers(self):
def test_config_with_tapers(self):
rg = list(labelled_range_generator(self.fp))
c = self.fp["config"]
self.assertEqual(rg[0], (c["minimum"], c["taper_left"], f"< {c['taper_left']}"))
self.assertEqual(rg[-1], (c["taper_right"], c["maximum"], f"≥ {c['taper_right']}"))
self.assertEqual(rg[1], (c["taper_left"], c["taper_left"] + c["bin_size"],
f"{c['taper_left']}-{c['taper_left'] + c['bin_size']}"))
f"[{c['taper_left']}, {c['taper_left'] + c['bin_size']})"))

def test_config_without_tappers(self):
self.fp["config"] = {
Expand All @@ -39,8 +39,8 @@ def test_config_without_tappers(self):
"taper_right": 1000
}
rg = list(labelled_range_generator(self.fp))
self.assertIn("-", rg[0][2])
self.assertIn("-", rg[-1][2])
self.assertIn("[", rg[0][2])
self.assertIn("[", rg[-1][2])

def test_wrong_config_min_max(self):
self.fp["config"] = {
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_custom_bins_config(self):
rg = list(labelled_range_generator(self.fp))
self.assertEqual(rg[0], (0, 200, "< 200"))
self.assertEqual(rg[-1], (2000, None, "≥ 2000"))
self.assertEqual(rg[1], (200, 300, "200-300"))
self.assertEqual(rg[1], (200, 300, "[200, 300)"))

def test_custom_bins_config_no_open_ended(self):
c = {
Expand All @@ -92,8 +92,8 @@ def test_custom_bins_config_no_open_ended(self):
}
}
rg = list(labelled_range_generator(c))
self.assertIn("-", rg[0][2])
self.assertIn("-", rg[-1][2])
self.assertIn("[", rg[0][2])
self.assertIn("[", rg[-1][2])

def test_custom_bins_wrong_min(self):
c = {
Expand Down

0 comments on commit 4e9d063

Please sign in to comment.