Skip to content

Commit

Permalink
Merge pull request #1284 from cmu-delphi/1153-covidcast-endpoint-inco…
Browse files Browse the repository at this point in the history
…nsistent-handling-of-iso-format-dates

Fix ISO range parsing
  • Loading branch information
dmytrotsko authored Sep 21, 2023
2 parents f7da659 + f590926 commit 63243a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
38 changes: 7 additions & 31 deletions src/server/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,44 +425,20 @@ def extract_dates(key: Union[str, Sequence[str]]) -> Optional[TimeValues]:
return None
values: TimeValues = []

def push_range(first: str, last: str):
first_d = parse_date(first)
last_d = parse_date(last)
if first_d == last_d:
# the first and last numbers are the same, just treat it as a singe value
return first_d
if last_d > first_d:
# add the range as an array
return (first_d, last_d)
# the range is inverted, this is an error
raise ValidationFailedException(f"{key}: the given range is inverted")

for part in parts:
if "-" not in part and ":" not in part:
# YYYYMMDD
values.append(parse_date(part))
continue
if part == "*":
return ["*"]
if ":" in part:
# YYYY-MM-DD:YYYY-MM-DD
range_part = part.split(":", 2)
r = push_range(range_part[0], range_part[1])
if r is None:
return None
values.append(r)
continue
# YYYY-MM-DD or YYYYMMDD-YYYYMMDD
# split on the dash
range_part = part.split("-")
if len(range_part) == 2:
# YYYYMMDD-YYYYMMDD
r = push_range(range_part[0], range_part[1])
range_part = part.split(":", 1)
r = _verify_range(parse_date(range_part[0]), parse_date(range_part[1]))
if r is None:
return None
values.append(r)
continue
# YYYY-MM-DD
values.append(parse_date(part))
# success, return the list
# parse other date formats
r = parse_day_value(part)
values.append(r)
return values

def parse_source_signal_sets() -> List[SourceSignalSet]:
Expand Down
6 changes: 6 additions & 0 deletions tests/server/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ def test_extract_dates(self):
with self.subTest("multiple param mixed iso"):
with app.test_request_context("/?s=2020-01-01&s=2020-01-02,2020-01-03"):
self.assertEqual(extract_dates("s"), [20200101, 20200102, 20200103])
with self.subTest("iso range"):
with app.test_request_context("/?s=2020-01-01--2020-01-30"):
self.assertEqual(extract_dates("s"), [(20200101, 20200130)])
with self.subTest("wildcard"):
with app.test_request_context("/?s=*"):
self.assertEqual(extract_dates("s"), ["*"])

with self.subTest("not a date"):
with app.test_request_context("/?s=a"):
Expand Down

0 comments on commit 63243a8

Please sign in to comment.