Skip to content

Remove date type for time-based query parameters. #542

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

Merged
merged 2 commits into from
Jun 12, 2025
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
46 changes: 9 additions & 37 deletions core_backend/app/data_api/routers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""This module contains FastAPI routers for data API endpoints."""

from datetime import date, datetime, timezone
from datetime import datetime, timezone
from typing import Annotated

from fastapi import APIRouter, Depends, Query
Expand Down Expand Up @@ -142,21 +142,15 @@ async def get_urgency_rules(
@router.get("/queries", response_model=list[QueryExtract])
async def get_queries(
start_date: Annotated[
datetime | date,
datetime,
Query(
description=(
"Can be date or UTC datetime. "
"Example: `2021-01-01` or `2021-01-01T00:00:00`"
),
description=("A UTC datetime." "Example: `2021-01-01T00:00:00`"),
),
],
end_date: Annotated[
datetime | date,
datetime,
Query(
description=(
"Can be date or UTC datetime. "
"Example: `2021-01-01` or `2021-01-01T00:00:00`"
),
description=("A UTC datetime." "Example: `2021-01-01T00:00:00`"),
),
],
workspace_db: Annotated[WorkspaceDB, Depends(authenticate_key)],
Expand All @@ -165,9 +159,6 @@ async def get_queries(
"""Get all queries including child records for a workspace between a start and end
date.

Note that the `start_date` and `end_date` can be provided as a date or `datetime`
object.

Parameters
----------
start_date
Expand All @@ -185,11 +176,6 @@ async def get_queries(
A list of QueryExtract objects containing all queries for the user.
"""

if isinstance(start_date, date):
start_date = datetime.combine(start_date, datetime.min.time())
if isinstance(end_date, date):
end_date = datetime.combine(end_date, datetime.max.time())

start_date = start_date.replace(tzinfo=timezone.utc)
end_date = end_date.replace(tzinfo=timezone.utc)

Expand All @@ -214,21 +200,15 @@ async def get_queries(
@router.get("/urgency-queries", response_model=list[UrgencyQueryExtract])
async def get_urgency_queries(
start_date: Annotated[
datetime | date,
datetime,
Query(
description=(
"Can be date or UTC datetime. "
"Example: `2021-01-01` or `2021-01-01T00:00:00`"
),
description=("A UTC datetime." "Example: `2021-01-01T00:00:00`"),
),
],
end_date: Annotated[
datetime | date,
datetime,
Query(
description=(
"Can be date or UTC datetime. "
"Example: `2021-01-01` or `2021-01-01T00:00:00`"
),
description=("A UTC datetime." "Example: `2021-01-01T00:00:00`"),
),
],
workspace_db: Annotated[WorkspaceDB, Depends(authenticate_key)],
Expand All @@ -237,9 +217,6 @@ async def get_urgency_queries(
"""Get all urgency queries including child records for a workspace between a start
and end date.

Note that the `start_date` and `end_date` can be provided as a date or `datetime`
object.

Parameters
----------
start_date
Expand All @@ -258,11 +235,6 @@ async def get_urgency_queries(
workspace.
"""

if isinstance(start_date, date):
start_date = datetime.combine(start_date, datetime.min.time())
if isinstance(end_date, date):
end_date = datetime.combine(end_date, datetime.max.time())

start_date = start_date.replace(tzinfo=timezone.utc)
end_date = end_date.replace(tzinfo=timezone.utc)

Expand Down
58 changes: 53 additions & 5 deletions core_backend/tests/api/test_data_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This module contains tests for the data API endpoints."""

import random
from datetime import datetime, timezone
from datetime import datetime, timedelta, timezone
from typing import Any, AsyncGenerator

import pytest
Expand Down Expand Up @@ -230,7 +230,7 @@ async def workspace_data_api_data_1(
monkeypatch: pytest.MonkeyPatch,
urgency_rules_workspace_data_api_1: int,
workspace_data_api_id_1: int,
) -> AsyncGenerator[None, None]:
) -> AsyncGenerator[list[datetime], None]:
"""Create urgency query data for data API workspace 1.

Parameters
Expand Down Expand Up @@ -283,7 +283,7 @@ async def workspace_data_api_data_1(
)
all_orm_objects.append(urgency_response_db)

yield
yield dates

for orm_object in reversed(all_orm_objects):
await asession.delete(orm_object)
Expand Down Expand Up @@ -336,7 +336,7 @@ def test_urgency_query_data_api(
self,
api_key_workspace_data_api_1: str,
client: TestClient,
workspace_data_api_data_1: pytest.FixtureRequest,
workspace_data_api_data_1: list[datetime],
) -> None:
"""Test the urgency query data API.

Expand Down Expand Up @@ -367,7 +367,7 @@ def test_urgency_query_data_api_date_filter(
days_ago_end: int,
api_key_workspace_data_api_1: str,
client: TestClient,
workspace_data_api_data_1: pytest.FixtureRequest,
workspace_data_api_data_1: list[datetime],
) -> None:
"""Test the urgency query data API with date filtering.

Expand Down Expand Up @@ -428,6 +428,54 @@ def test_urgency_query_data_api_date_filter(

assert len(response.json()) == n_records

@pytest.mark.parametrize(
"days_ago_start, days_ago_end",
[[0.5, 0], [1.5, 0.5], [5.5, 3.5], [3.2, 1.5], [8.5, 4], [0.5, 0.2]],
)
def test_urgency_query_data_api_datetime_filter(
self,
days_ago_start: float,
days_ago_end: float,
api_key_workspace_data_api_1: str,
client: TestClient,
workspace_data_api_data_1: list[datetime],
) -> None:
"""Test the urgency query data API with date filtering.

Parameters
----------
days_ago_start
The fraction of days ago to start.
days_ago_end
The fraction of days ago to end.
api_key_workspace_data_api_1
The API key of data API workspace 1.
client
The test client.
workspace_data_api_data_1
The data of data API workspace 1.
"""

start_date = datetime.now(timezone.utc) - timedelta(days=days_ago_start)
end_date = datetime.now(timezone.utc) - timedelta(days=days_ago_end)

date_format = "%Y-%m-%dT%H:%M:%S.%f%z"

response = client.get(
"/data-api/urgency-queries",
headers={"Authorization": f"Bearer {api_key_workspace_data_api_1}"},
params={
"start_date": start_date.strftime(date_format),
"end_date": end_date.strftime(date_format),
},
)
assert response.status_code == status.HTTP_200_OK

n_records = len(
[d for d in workspace_data_api_data_1 if start_date <= d <= end_date]
)
assert len(response.json()) == n_records

@pytest.mark.parametrize(
"days_ago_start, days_ago_end",
[[5, 1], [6, 4], [5, 5], [0, 0], [20, 14], [11, 0], [2, 5]],
Expand Down