Skip to content

Fix week parsing for date parameters/arguments #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions integrations/server/test_covidcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ def _insert_placeholder_set_five(self):
self._insert_rows(rows)
return rows

def _insert_placeholder_set_with_weeks(self):
rows = [
CovidcastTestRow.make_default_row(
time_value=2021_05+i, time_type="week",
source="nchs-mortality", signal="deaths_covid_incidence_num",
geo_type="state", geo_value="il",
value=i*i)
for i in [0, 1, 2]
]
self._insert_rows(rows)
return rows

def test_round_trip(self):
"""Make a simple round-trip with some sample data."""

Expand Down Expand Up @@ -445,3 +457,22 @@ def test_date_formats(self):

# assert that the right data came back
self.assertEqual(len(response['epidata']), 2 * 3)


def test_week_formats(self):
"""Test different ways to specify week ranges are accepted."""

rows = self._insert_placeholder_set_with_weeks()
expected = {
'result': 1,
'epidata': [r.as_api_row_dict() for r in rows],
'message': 'success',
}

colond = self.request_based_on_row(rows[0], time_values="202105:202107")
dashed = self.request_based_on_row(rows[0], time_values="202105-202107")
enumed = self.request_based_on_row(rows[0], time_values="202105,202106,202107")

self.assertEqual(expected, colond)
self.assertEqual(expected, dashed)
self.assertEqual(expected, enumed)
13 changes: 12 additions & 1 deletion src/server/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,18 @@ def extract_dates(key: Union[str, Sequence[str]]) -> Optional[TimeValues]:
values.append(r)
continue
# parse other date formats
r = parse_day_value(part)
dashless_part = part.replace("-", "")
if len(dashless_part) in (6, 12):
# if there are 6 or 12 (hopefully integer) chars in this, it
# should be a week or week range (YYYYWW or YYYYWWYYYYWW)
r = parse_week_value(part)
elif len(dashless_part) in (8, 16):
# if its 8 or 16, it should be a day or
# day range (YYYYMMDD or YYYYMMDDYYYYMMDD)
r = parse_day_value(part)
else:
# other time types tbd lol
raise ValidationFailedException(f"unrecognized date format: {part}")
values.append(r)
return values

Expand Down