Skip to content
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

Fix ISO range parsing #1284

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
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