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

Add non interval date time query to sqlalchemy backend #262

Merged
merged 13 commits into from
Sep 21, 2021
9 changes: 6 additions & 3 deletions stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,16 @@ def post_search(
if search_request.datetime:
# Two tailed query (between)
dts = search_request.datetime.split("/")
if ".." not in search_request.datetime:
# Non-interval date ex. "2000-02-02T00:00:00.00Z"
if len(dts) == 1:
query = query.filter(self.item_table.datetime == dts[0])
elif ".." not in search_request.datetime:
query = query.filter(self.item_table.datetime.between(*dts))
# All items after the start date
if dts[0] != "..":
elif dts[0] != "..":
query = query.filter(self.item_table.datetime >= dts[0])
# All items before the end date
if dts[1] != "..":
elif dts[1] != "..":
query = query.filter(self.item_table.datetime <= dts[1])

# Query fields
Expand Down
22 changes: 22 additions & 0 deletions stac_fastapi/sqlalchemy/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ def test_search_invalid_date(load_test_data, app_client, postgres_transactions):
assert resp.status_code == 400


def test_datetime_non_interval(load_test_data, app_client, postgres_transactions):
item = load_test_data("test_item.json")
postgres_transactions.create_item(item, request=MockStarletteRequest)
alternate_formats = [
"2020-02-12T12:30:22+00:00",
"2020-02-12T12:30:22.00Z",
"2020-02-12T12:30:22Z",
"2020-02-12T12:30:22.00+00:00",
]
for date in alternate_formats:
params = {
"datetime": date,
"collections": [item["collection"]],
}

resp = app_client.post("/search", json=params)
assert resp.status_code == 200
resp_json = resp.json()
# datetime is returned in this format "2020-02-12T12:30:22+00:00"
assert resp_json["features"][0]["properties"]["datetime"][0:19] == date[0:19]


def test_bbox_3d(load_test_data, app_client, postgres_transactions):
item = load_test_data("test_item.json")
postgres_transactions.create_item(item, request=MockStarletteRequest)
Expand Down