Skip to content

Commit 0d44b2c

Browse files
committed
update prev logic
1 parent de4da9f commit 0d44b2c

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

stac_fastapi/pgstac/models/links.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,23 @@ def link_prev(self):
206206
if self.prev is not None:
207207
method = self.request.method
208208
if method == "GET":
209-
# if offset is equal to default value (0), drop it
210-
if self.prev["body"].get("offset", -1) == 0:
211-
_ = self.prev["body"].pop("offset")
209+
u = urlparse(self.url)
210+
params = parse_qs(u.query)
211+
params.update(self.prev["body"])
212212

213-
href = merge_params(self.url, self.prev["body"])
213+
# if offset is equal to default value (0), drop it
214+
if params.get("offset", -1) == 0:
215+
_ = params.pop("offset")
216+
217+
param_string = unquote(urlencode(params, True))
218+
href = ParseResult(
219+
scheme=u.scheme,
220+
netloc=u.netloc,
221+
path=u.path,
222+
params=u.params,
223+
query=param_string,
224+
fragment=u.fragment,
225+
).geturl()
214226

215227
# if prev link is equal to this link, skip it
216228
if href == self.url:

tests/resources/test_collection.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,8 @@ async def test_get_collections_search_limit_offset(
347347
assert len(cols) == 2
348348
assert cols[0]["id"] == load_test_collection["id"]
349349
assert cols[1]["id"] == load_test2_collection.id
350-
# TODO: check with pgstac
351-
# assert len(links) == 2
352-
# assert {"root", "self"} == {link["rel"] for link in links}
350+
assert len(links) == 2
351+
assert {"root", "self"} == {link["rel"] for link in links}
353352

354353
###################
355354
# limit=3, there should not be a next/previous link
@@ -406,20 +405,36 @@ async def test_get_collections_search_limit_offset(
406405
assert prev_link["href"].endswith("?limit=2&offset=1")
407406

408407
###################
409-
# offset=1, should have a `previous` link
408+
# offset=1,limit=1 should have a `previous` link
410409
resp = await app_client.get(
411410
"/collections",
412-
params={"offset": 1},
411+
params={"offset": 1, "limit": 1},
413412
)
414413
cols = resp.json()["collections"]
415414
links = resp.json()["links"]
416415
assert len(cols) == 1
417416
assert cols[0]["id"] == load_test2_collection.id
418-
# TODO: Check with pgstac
417+
assert len(links) == 3
418+
assert {"root", "self", "previous"} == {link["rel"] for link in links}
419+
prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0]
420+
assert "offset" not in prev_link["href"]
421+
422+
###################
423+
# BUG: pgstac doesn't return a `prev` link when limit is not set
424+
# offset=1, should have a `previous` link
425+
# resp = await app_client.get(
426+
# "/collections",
427+
# params={"offset": 1},
428+
# )
429+
# cols = resp.json()["collections"]
430+
# links = resp.json()["links"]
431+
# assert len(cols) == 1
432+
# assert cols[0]["id"] == load_test2_collection.id
419433
# assert len(links) == 3
420434
# assert {"root", "self", "previous"} == {link["rel"] for link in links}
421435
# prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0]
422-
# assert prev_link["href"].endswith("?offset=0")
436+
# # offset=0 should not be in the previous link (because it's useless)
437+
# assert "offset" not in prev_link["href"]
423438

424439
###################
425440
# offset=0, should not have next/previous link
@@ -430,6 +445,5 @@ async def test_get_collections_search_limit_offset(
430445
cols = resp.json()["collections"]
431446
links = resp.json()["links"]
432447
assert len(cols) == 2
433-
# TODO: Check with pgstac
434-
# assert len(links) == 2
435-
# assert {"root", "self"} == {link["rel"] for link in links}
448+
assert len(links) == 2
449+
assert {"root", "self"} == {link["rel"] for link in links}

0 commit comments

Comments
 (0)