Skip to content

Commit

Permalink
Merge pull request #244 from stac-utils/pag-links-test
Browse files Browse the repository at this point in the history
Add pagination test to verify the absence of a 'next' link on the last page of results
  • Loading branch information
jonhealy1 authored May 6, 2024
2 parents 8660a13 + e17162b commit b0362b7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- A test to ensure that pagination correctly returns expected links, particularly verifying the absence of a 'next' link on the last page of results [#244](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/244)

### Fixed

- Fixed issue where searches return an empty `links` array [#241](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/241)
Expand Down
42 changes: 42 additions & 0 deletions stac_fastapi/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,48 @@ async def test_pagination_base_links(app_client, ctx):
assert {"self", "root"}.issubset({link["rel"] for link in page_data["links"]})


@pytest.mark.asyncio
async def test_pagination_links_behavior(app_client, ctx, txn_client):
"""Test the links in pagination specifically look for last page behavior."""

# Ingest 5 items
for _ in range(5):
ctx.item["id"] = str(uuid.uuid4())
await create_item(txn_client, item=ctx.item)

# Setting a limit to ensure the creation of multiple pages
limit = 1
first_page = await app_client.get(
f"/collections/{ctx.item['collection']}/items?limit={limit}"
)
first_page_data = first_page.json()

# Test for 'next' link in the first page
next_link = next(
(link for link in first_page_data["links"] if link["rel"] == "next"), None
)
assert next_link, "Missing 'next' link on the first page"

# Follow to the last page using 'next' links
current_page_data = first_page_data
while "next" in {link["rel"] for link in current_page_data["links"]}:
next_page_url = next(
(
link["href"]
for link in current_page_data["links"]
if link["rel"] == "next"
),
None,
)
next_page = await app_client.get(next_page_url)
current_page_data = next_page.json()

# Verify the last page does not have a 'next' link
assert "next" not in {
link["rel"] for link in current_page_data["links"]
}, "Unexpected 'next' link on the last page"


@pytest.mark.asyncio
async def test_pagination_item_collection(app_client, ctx, txn_client):
"""Test item collection pagination links (paging extension)"""
Expand Down

0 comments on commit b0362b7

Please sign in to comment.