From fb293ad0015b9c4e10e708acc535496d24f162b9 Mon Sep 17 00:00:00 2001 From: David Hoese <david.hoese@ssec.wisc.edu> Date: Wed, 4 Oct 2023 13:26:38 -0500 Subject: [PATCH] Add error message specific to begin/end in the future --- metobsapi/data_api.py | 10 ++++++++-- metobsapi/tests/test_data_api.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/metobsapi/data_api.py b/metobsapi/data_api.py index 348e798..edc397b 100644 --- a/metobsapi/data_api.py +++ b/metobsapi/data_api.py @@ -36,11 +36,16 @@ ROUNDING = { def handle_date(date: str) -> datetime: datetime_fmt = "%Y-%m-%d" if len(date) == LENGTH_DATE_STR else "%Y-%m-%dT%H:%M:%S" try: - return datetime.strptime(date, datetime_fmt) + dt = datetime.strptime(date, datetime_fmt) except ValueError as e: msg = f"Malformed date string '{date}'" LOG.warning(msg) raise ValueError(msg) from e + if dt > (datetime.utcnow() + timedelta(minutes=1)): + msg = f"Date/time is in the future: {date}" + LOG.warning(msg) + raise ValueError(msg) + return dt def handle_time_string(date_string): @@ -392,7 +397,8 @@ def _convert_begin_and_end(begin, end) -> tuple[datetime | timedelta, datetime | begin = handle_time_string(begin) end = handle_time_string(end) except (TypeError, ValueError) as e: - raise ValueError("malformed_timestamp") from e + msg = f"malformed_timestamp: {str(e)}" + raise ValueError(msg) from e return begin, end diff --git a/metobsapi/tests/test_data_api.py b/metobsapi/tests/test_data_api.py index e891f87..6a82763 100644 --- a/metobsapi/tests/test_data_api.py +++ b/metobsapi/tests/test_data_api.py @@ -198,13 +198,23 @@ def test_bad_format(client): def test_bad_begin_json(client): - res = client.get("/api/data.json?symbols=air_temp&begin=blah") + res = client.get("/api/data.json?symbols=aoss.tower.air_temp&begin=blah") res = json.loads(res.data.decode()) assert res["code"] == 400 assert res["status"] == "error" assert "timestamp" in res["message"] +def test_future_begin_json(client): + now = datetime.utcnow() + future_begin = now + timedelta(days=1) + res = client.get(f"/api/data.json?symbols=aoss.tower.air_temp&begin={future_begin:%Y-%m-%dT%H:%M:%S}") + res = json.loads(res.data.decode()) + assert res["code"] == 400 + assert res["status"] == "error" + assert "is in the future" in res["message"] + + def test_bad_order(client): res = client.get("/api/data.json?order=blah&symbols=air_temp") res = json.loads(res.data.decode()) -- GitLab