-
Notifications
You must be signed in to change notification settings - Fork 27
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
update pagination for collection-search #155
base: main
Are you sure you want to change the base?
Changes from all commits
456c515
a9dce7e
66a540e
8862c93
576a9dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
from stac_fastapi.pgstac.config import Settings | ||
from stac_fastapi.pgstac.models.links import ( | ||
CollectionLinks, | ||
CollectionSearchPagingLinks, | ||
ItemCollectionLinks, | ||
ItemLinks, | ||
PagingLinks, | ||
|
@@ -46,8 +47,8 @@ async def all_collections( # noqa: C901 | |
bbox: Optional[BBox] = None, | ||
datetime: Optional[DateTimeType] = None, | ||
limit: Optional[int] = None, | ||
offset: Optional[int] = None, | ||
query: Optional[str] = None, | ||
token: Optional[str] = None, | ||
fields: Optional[List[str]] = None, | ||
sortby: Optional[str] = None, | ||
filter: Optional[str] = None, | ||
|
@@ -68,7 +69,7 @@ async def all_collections( # noqa: C901 | |
base_args = { | ||
"bbox": bbox, | ||
"limit": limit, | ||
"token": token, | ||
"offset": offset, | ||
"query": orjson.loads(unquote_plus(query)) if query else query, | ||
} | ||
|
||
|
@@ -90,12 +91,16 @@ async def all_collections( # noqa: C901 | |
) | ||
collections_result: Collections = await conn.fetchval(q, *p) | ||
|
||
next: Optional[str] = None | ||
prev: Optional[str] = None | ||
|
||
next_link: Optional[Dict[str, Any]] = None | ||
prev_link: Optional[Dict[str, Any]] = None | ||
if links := collections_result.get("links"): | ||
next = collections_result["links"].pop("next") | ||
prev = collections_result["links"].pop("prev") | ||
next_link = None | ||
prev_link = None | ||
for link in links: | ||
if link["rel"] == "next": | ||
next_link = link | ||
elif link["rel"] == "prev": | ||
prev_link = link | ||
|
||
linked_collections: List[Collection] = [] | ||
collections = collections_result["collections"] | ||
|
@@ -120,10 +125,13 @@ async def all_collections( # noqa: C901 | |
|
||
linked_collections.append(coll) | ||
|
||
links = await PagingLinks( | ||
if not collections: | ||
next_link = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pgstac seems to return a next link even if returned collection is empty cc @bitner |
||
|
||
links = await CollectionSearchPagingLinks( | ||
request=request, | ||
next=next, | ||
prev=prev, | ||
next=next_link, | ||
prev=prev_link, | ||
).get_links() | ||
|
||
return Collections( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,3 +303,133 @@ async def test_get_collections_search( | |
"/collections", | ||
) | ||
assert len(resp.json()["collections"]) == 2 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_collections_search_limit_offset( | ||
app_client, load_test_collection, load_test2_collection | ||
): | ||
resp = await app_client.get("/collections") | ||
cols = resp.json()["collections"] | ||
assert len(cols) == 2 | ||
links = resp.json()["links"] | ||
assert len(links) == 2 | ||
assert {"root", "self"} == {link["rel"] for link in links} | ||
|
||
################### | ||
# limit should be positive | ||
resp = await app_client.get("/collections", params={"limit": 0}) | ||
assert resp.status_code == 400 | ||
|
||
################### | ||
# limit=1, should have a `next` link | ||
resp = await app_client.get( | ||
"/collections", | ||
params={"limit": 1}, | ||
) | ||
cols = resp.json()["collections"] | ||
links = resp.json()["links"] | ||
assert len(cols) == 1 | ||
assert cols[0]["id"] == load_test_collection["id"] | ||
assert len(links) == 3 | ||
assert {"root", "self", "next"} == {link["rel"] for link in links} | ||
next_link = list(filter(lambda link: link["rel"] == "next", links))[0] | ||
assert next_link["href"].endswith("?limit=1&offset=1") | ||
|
||
################### | ||
# limit=2, there should not be a next link | ||
resp = await app_client.get( | ||
"/collections", | ||
params={"limit": 2}, | ||
) | ||
cols = resp.json()["collections"] | ||
links = resp.json()["links"] | ||
assert len(cols) == 2 | ||
assert cols[0]["id"] == load_test_collection["id"] | ||
assert cols[1]["id"] == load_test2_collection.id | ||
# TODO: check with pgstac | ||
# assert len(links) == 2 | ||
# assert {"root", "self"} == {link["rel"] for link in links} | ||
|
||
################### | ||
# limit=3, there should not be a next/previous link | ||
resp = await app_client.get( | ||
"/collections", | ||
params={"limit": 3}, | ||
) | ||
cols = resp.json()["collections"] | ||
links = resp.json()["links"] | ||
assert len(cols) == 2 | ||
assert cols[0]["id"] == load_test_collection["id"] | ||
assert cols[1]["id"] == load_test2_collection.id | ||
assert len(links) == 2 | ||
assert {"root", "self"} == {link["rel"] for link in links} | ||
|
||
################### | ||
# offset=3, because there are 2 collections, we should not have `next` or `prev` links | ||
resp = await app_client.get( | ||
"/collections", | ||
params={"offset": 3}, | ||
) | ||
cols = resp.json()["collections"] | ||
links = resp.json()["links"] | ||
assert len(cols) == 0 | ||
assert len(links) == 2 | ||
assert {"root", "self"} == {link["rel"] for link in links} | ||
|
||
################### | ||
# offset=3,limit=1 | ||
resp = await app_client.get( | ||
"/collections", | ||
params={"limit": 1, "offset": 3}, | ||
) | ||
cols = resp.json()["collections"] | ||
links = resp.json()["links"] | ||
assert len(cols) == 0 | ||
assert len(links) == 3 | ||
assert {"root", "self", "previous"} == {link["rel"] for link in links} | ||
prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0] | ||
assert prev_link["href"].endswith("?limit=1&offset=2") | ||
|
||
################### | ||
# limit=2, offset=3, there should not be a next link | ||
resp = await app_client.get( | ||
"/collections", | ||
params={"limit": 2, "offset": 3}, | ||
) | ||
cols = resp.json()["collections"] | ||
links = resp.json()["links"] | ||
assert len(cols) == 0 | ||
assert len(links) == 3 | ||
assert {"root", "self", "previous"} == {link["rel"] for link in links} | ||
prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0] | ||
assert prev_link["href"].endswith("?limit=2&offset=1") | ||
|
||
################### | ||
# offset=1, should have a `previous` link | ||
resp = await app_client.get( | ||
"/collections", | ||
params={"offset": 1}, | ||
) | ||
cols = resp.json()["collections"] | ||
links = resp.json()["links"] | ||
assert len(cols) == 1 | ||
assert cols[0]["id"] == load_test2_collection.id | ||
# TODO: Check with pgstac | ||
# assert len(links) == 3 | ||
# assert {"root", "self", "previous"} == {link["rel"] for link in links} | ||
# prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0] | ||
# assert prev_link["href"].endswith("?offset=0") | ||
|
||
################### | ||
# offset=0, should not have next/previous link | ||
resp = await app_client.get( | ||
"/collections", | ||
params={"offset": 0}, | ||
) | ||
cols = resp.json()["collections"] | ||
links = resp.json()["links"] | ||
assert len(cols) == 2 | ||
# TODO: Check with pgstac | ||
# assert len(links) == 2 | ||
# assert {"root", "self"} == {link["rel"] for link in links} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there seems to be bugs in pgstac about how next/previous links are created should open an issue in pgstac repo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref stac-api-extensions/collection-search#19