Skip to content

Commit 0593895

Browse files
authored
Merge pull request #1121 from cmu-delphi/geo_mapper_state_crutch
get_geo_values() doesnt know about a plain "state" type
2 parents 4e8b410 + 923ac4c commit 0593895

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

integrations/client/test_delphi_epidata.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def test_geo_value(self):
218218
# insert placeholder data: three counties, three MSAs
219219
N = 3
220220
rows = [
221-
CovidcastTestRow.make_default_row(geo_type="fips", geo_value=FIPS[i], value=i)
221+
CovidcastTestRow.make_default_row(geo_type="county", geo_value=FIPS[i], value=i)
222222
for i in range(N)
223223
] + [
224224
CovidcastTestRow.make_default_row(geo_type="msa", geo_value=MSA[i], value=i*10)
@@ -245,7 +245,7 @@ def fetch(geo):
245245
self.assertEqual(request['epidata'], [counties[0]])
246246
# test fetch a specific yet not existing region
247247
request = fetch('55555')
248-
self.assertEqual(request['message'], 'Invalid geo_value(s) 55555 for the requested geo_type fips')
248+
self.assertEqual(request['message'], 'Invalid geo_value(s) 55555 for the requested geo_type county')
249249
# test fetch a multiple regions
250250
request = fetch([FIPS[0], FIPS[1]])
251251
self.assertEqual(request['message'], 'success')
@@ -256,10 +256,10 @@ def fetch(geo):
256256
self.assertEqual(request['epidata'], [counties[0], counties[2]])
257257
# test fetch a multiple regions but one is not existing
258258
request = fetch([FIPS[0], '55555'])
259-
self.assertEqual(request['message'], 'Invalid geo_value(s) 55555 for the requested geo_type fips')
259+
self.assertEqual(request['message'], 'Invalid geo_value(s) 55555 for the requested geo_type county')
260260
# test fetch a multiple regions but specify no region
261261
request = fetch([])
262-
self.assertEqual(request['message'], 'geo_value is empty for the requested geo_type fips!')
262+
self.assertEqual(request['message'], 'geo_value is empty for the requested geo_type county!')
263263
# test fetch a region with no results
264264
request = fetch([FIPS[3]])
265265
self.assertEqual(request['message'], 'no results')
@@ -326,7 +326,7 @@ def test_async_epidata(self):
326326
# insert placeholder data: three counties, three MSAs
327327
N = 3
328328
rows = [
329-
CovidcastTestRow.make_default_row(geo_type="fips", geo_value=FIPS[i-1], value=i)
329+
CovidcastTestRow.make_default_row(geo_type="county", geo_value=FIPS[i-1], value=i)
330330
for i in range(N)
331331
] + [
332332
CovidcastTestRow.make_default_row(geo_type="msa", geo_value=MSA[i-1], value=i*10)

src/server/_params.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,21 @@ def __init__(self, geo_type: str, geo_values: Union[bool, Sequence[str]]):
5858
if not isinstance(geo_values, bool):
5959
if geo_values == ['']:
6060
raise ValidationFailedException(f"geo_value is empty for the requested geo_type {geo_type}!")
61-
allowed_values = delphi_utils.geomap.GeoMapper().get_geo_values(geo_type)
62-
invalid_values = set(geo_values) - set(allowed_values)
63-
if invalid_values:
64-
raise ValidationFailedException(f"Invalid geo_value(s) {', '.join(invalid_values)} for the requested geo_type {geo_type}")
61+
# TODO: keep this translator in sync with CsvImporter.GEOGRAPHIC_RESOLUTIONS in acquisition/covidcast/ and with GeoMapper
62+
geo_type_translator = {
63+
"county": "fips",
64+
"state": "state_id",
65+
"zip": "zip",
66+
"hrr": "hrr",
67+
"hhs": "hhs",
68+
"msa": "msa",
69+
"nation": "nation"
70+
}
71+
if geo_type in geo_type_translator: # else geo_type is unknown to GeoMapper
72+
allowed_values = delphi_utils.geomap.GeoMapper().get_geo_values(geo_type_translator[geo_type])
73+
invalid_values = set(geo_values) - set(allowed_values)
74+
if invalid_values:
75+
raise ValidationFailedException(f"Invalid geo_value(s) {', '.join(invalid_values)} for the requested geo_type {geo_type}")
6576
self.geo_type = geo_type
6677
self.geo_values = geo_values
6778

tests/server/test_params.py

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def test_parse_geo_arg(self):
9494
self.assertEqual(parse_geo_arg(), [GeoSet("fips", True)])
9595
with app.test_request_context(f"/?geo=fips:{FIPS[0]}"):
9696
self.assertEqual(parse_geo_arg(), [GeoSet("fips", [FIPS[0]])])
97+
with self.subTest("covidcast"):
98+
for geo_type in "county dma hhs hrr msa nation state".split():
99+
with app.test_request_context(f"/?geo={geo_type}:*"):
100+
self.assertEqual(parse_geo_arg(), [GeoSet(geo_type, True)])
97101
with self.subTest("single list"):
98102
with app.test_request_context(f"/?geo=fips:{FIPS[0]},{FIPS[1]}"):
99103
self.assertEqual(parse_geo_arg(), [GeoSet("fips", [FIPS[0], FIPS[1]])])

0 commit comments

Comments
 (0)