Skip to content
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

Add pagination test to verify the absence of a 'next' link on the last page of results #244

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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