Skip to content

Commit 59014f5

Browse files
committed
test: add sorting test
Confirming that #514 is fixed with pgstac v0.7.0.
1 parent d468373 commit 59014f5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

stac_fastapi/pgstac/tests/api/test_api.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, timedelta
2+
from typing import Any, Dict, List
23
from urllib.parse import quote_plus
34

45
import orjson
@@ -577,3 +578,47 @@ async def test_deleting_items_with_identical_ids(app_client):
577578
response = await app_client.get(f"/collections/{collection.id}/items")
578579
assert response.status_code == 200, response.json()
579580
assert not response.json()["features"]
581+
582+
583+
@pytest.mark.parametrize("direction", ("asc", "desc"))
584+
async def test_sorting_and_paging(app_client, load_test_collection, direction: str):
585+
collection_id = load_test_collection.id
586+
for i in range(10):
587+
item = Item(
588+
id=f"item-{i}",
589+
geometry={"type": "Point", "coordinates": [-105.1019, 40.1672]},
590+
bbox=[-105.1019, 40.1672, -105.1019, 40.1672],
591+
datetime=datetime.now(),
592+
properties={
593+
"eo:cloud_cover": 42 + i if i % 3 != 0 else None,
594+
},
595+
)
596+
item.collection_id = collection_id
597+
response = await app_client.post(
598+
f"/collections/{collection_id}/items",
599+
json=item.to_dict(include_self_link=False, transform_hrefs=False),
600+
)
601+
assert response.status_code == 200
602+
603+
async def search(query: Dict[str, Any]) -> List[Item]:
604+
items: List[Item] = list()
605+
while True:
606+
response = await app_client.post("/search", json=query)
607+
json = response.json()
608+
assert response.status_code == 200, json
609+
items.extend((Item.from_dict(d) for d in json["features"]))
610+
next_link = next(
611+
(link for link in json["links"] if link["rel"] == "next"), None
612+
)
613+
if next_link is None:
614+
return items
615+
else:
616+
query = next_link["body"]
617+
618+
query = {
619+
"collections": [collection_id],
620+
"sortby": [{"field": "properties.eo:cloud_cover", "direction": direction}],
621+
"limit": 5,
622+
}
623+
items = await search(query)
624+
assert len(items) == 10, items

0 commit comments

Comments
 (0)