Skip to content

Commit 63243a8

Browse files
authored
Merge pull request #1284 from cmu-delphi/1153-covidcast-endpoint-inconsistent-handling-of-iso-format-dates
Fix ISO range parsing
2 parents f7da659 + f590926 commit 63243a8

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

src/server/_params.py

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -425,44 +425,20 @@ def extract_dates(key: Union[str, Sequence[str]]) -> Optional[TimeValues]:
425425
return None
426426
values: TimeValues = []
427427

428-
def push_range(first: str, last: str):
429-
first_d = parse_date(first)
430-
last_d = parse_date(last)
431-
if first_d == last_d:
432-
# the first and last numbers are the same, just treat it as a singe value
433-
return first_d
434-
if last_d > first_d:
435-
# add the range as an array
436-
return (first_d, last_d)
437-
# the range is inverted, this is an error
438-
raise ValidationFailedException(f"{key}: the given range is inverted")
439-
440428
for part in parts:
441-
if "-" not in part and ":" not in part:
442-
# YYYYMMDD
443-
values.append(parse_date(part))
444-
continue
429+
if part == "*":
430+
return ["*"]
445431
if ":" in part:
446432
# YYYY-MM-DD:YYYY-MM-DD
447-
range_part = part.split(":", 2)
448-
r = push_range(range_part[0], range_part[1])
449-
if r is None:
450-
return None
451-
values.append(r)
452-
continue
453-
# YYYY-MM-DD or YYYYMMDD-YYYYMMDD
454-
# split on the dash
455-
range_part = part.split("-")
456-
if len(range_part) == 2:
457-
# YYYYMMDD-YYYYMMDD
458-
r = push_range(range_part[0], range_part[1])
433+
range_part = part.split(":", 1)
434+
r = _verify_range(parse_date(range_part[0]), parse_date(range_part[1]))
459435
if r is None:
460436
return None
461437
values.append(r)
462438
continue
463-
# YYYY-MM-DD
464-
values.append(parse_date(part))
465-
# success, return the list
439+
# parse other date formats
440+
r = parse_day_value(part)
441+
values.append(r)
466442
return values
467443

468444
def parse_source_signal_sets() -> List[SourceSignalSet]:

tests/server/test_params.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,12 @@ def test_extract_dates(self):
477477
with self.subTest("multiple param mixed iso"):
478478
with app.test_request_context("/?s=2020-01-01&s=2020-01-02,2020-01-03"):
479479
self.assertEqual(extract_dates("s"), [20200101, 20200102, 20200103])
480+
with self.subTest("iso range"):
481+
with app.test_request_context("/?s=2020-01-01--2020-01-30"):
482+
self.assertEqual(extract_dates("s"), [(20200101, 20200130)])
483+
with self.subTest("wildcard"):
484+
with app.test_request_context("/?s=*"):
485+
self.assertEqual(extract_dates("s"), ["*"])
480486

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

0 commit comments

Comments
 (0)