Skip to content

Commit

Permalink
Add non interval date time query to sqlalchemy backend (#262)
Browse files Browse the repository at this point in the history
* add non interval date to searc request

* add datetime test

* check first 18 chanracters of datetime

* add comment about non inerval data

* test multiple datetime formats

* erase duplicate test date

* run pre-commit

* run pre-commit

Co-authored-by: Rob Emanuele <rdemanuele@gmail.com>
Co-authored-by: Nathan Zimmerman <npzimmerman@gmail.com>
  • Loading branch information
3 people authored Sep 21, 2021
1 parent e283d20 commit 27d2d2a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
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 @@ -338,13 +338,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 @@ -174,6 +174,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

0 comments on commit 27d2d2a

Please sign in to comment.