From 42084eeee6d086aaa999f6a0f4bef6c4b7c05604 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 6 May 2024 12:48:52 +0800 Subject: [PATCH 1/2] add pagination test --- CHANGELOG.md | 4 +++ stac_fastapi/tests/resources/test_item.py | 42 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46a95128..efcff7c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 [#241](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/241) + ### Fixed - Fixed issue where searches return an empty `links` array [#241](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/241) diff --git a/stac_fastapi/tests/resources/test_item.py b/stac_fastapi/tests/resources/test_item.py index f84d9759..336becdc 100644 --- a/stac_fastapi/tests/resources/test_item.py +++ b/stac_fastapi/tests/resources/test_item.py @@ -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)""" From e17162baecb92aaff70310c6f280119a2cd3e60d Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 6 May 2024 12:55:31 +0800 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efcff7c2..c9ad4307 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### 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 [#241](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/241) +- 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