Skip to content

Commit

Permalink
Refactor: Error handling to get_datetime_range
Browse files Browse the repository at this point in the history
  • Loading branch information
fjugipe committed Feb 7, 2024
1 parent 2fb606a commit 334c49e
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import timedelta
from typing import Tuple

from fastapi import HTTPException
from google.protobuf.timestamp_pb2 import Timestamp
from pydantic import AwareDatetime
from pydantic import TypeAdapter
Expand All @@ -11,23 +12,38 @@ def get_datetime_range(datetime_string: str | None) -> Tuple[Timestamp, Timestam
if not datetime_string:
return None

errors = {}

start_datetime, end_datetime = Timestamp(), Timestamp()
aware_datetime_type_adapter = TypeAdapter(AwareDatetime)
datetimes = tuple(value.strip() for value in datetime_string.split("/"))
if len(datetimes) == 1:
start_datetime.FromDatetime(aware_datetime_type_adapter.validate_python(datetimes[0]))
end_datetime.FromDatetime(
aware_datetime_type_adapter.validate_python(datetimes[0]) + timedelta(seconds=1)
) # HACK: Add one second so we get some data, as the store returns [start, end)
else:
if datetimes[0] != "..":

try:
datetimes = tuple(value.strip() for value in datetime_string.split("/"))
if len(datetimes) == 1:
start_datetime.FromDatetime(aware_datetime_type_adapter.validate_python(datetimes[0]))
end_datetime.FromDatetime(
aware_datetime_type_adapter.validate_python(datetimes[0]) + timedelta(seconds=1)
) # HACK: Add one second so we get some data, as the store returns [start, end)
else:
start_datetime.FromDatetime(datetime.min)
if datetimes[1] != "..":
# HACK add one second so that the end_datetime is included in the interval.
end_datetime.FromDatetime(aware_datetime_type_adapter.validate_python(datetimes[1]) + timedelta(seconds=1))
else:
end_datetime.FromDatetime(datetime.max)
if datetimes[0] != "..":
start_datetime.FromDatetime(aware_datetime_type_adapter.validate_python(datetimes[0]))
else:
start_datetime.FromDatetime(datetime.min)
if datetimes[1] != "..":
# HACK add one second so that the end_datetime is included in the interval.
end_datetime.FromDatetime(
aware_datetime_type_adapter.validate_python(datetimes[1]) + timedelta(seconds=1)
)
else:
end_datetime.FromDatetime(datetime.max)

if start_datetime.seconds > end_datetime.seconds:
errors["datetime"] = f"Invalid range: {datetimes[0]} > {datetimes[1]}"

except ValueError:
errors["datetime"] = f"Invalid format: {datetime_string}"

if errors:
raise HTTPException(status_code=422, detail=errors)

return start_datetime, end_datetime

1 comment on commit 334c49e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Unit Test Coverage Report
FileStmtsMissCoverMissing
\_\_init\_\_.py00100% 
datastore_pb2.py584621%23–68
datastore_pb2_grpc.py43430%2–225
dependencies.py31310%1–49
grpc_getter.py880%1–16
locustfile.py15150%1–31
main.py22220%3–51
metadata_endpoints.py19190%1–70
formatters
   \_\_init\_\_.py12650%16–32
   base_formatter.py7186%18
   covjson.py52198%69
routers
   \_\_init\_\_.py00100% 
   edr.py47470%2–126
   records.py00100% 
test
   test_covjson.py600100% 
TOTAL37423936% 

API Unit Test Coverage Summary

Tests Skipped Failures Errors Time
4 0 💤 0 ❌ 0 🔥 0.983s ⏱️

Please sign in to comment.