From 7392a67821fbbc70f8f1a1942001bda9145f8cc8 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Thu, 6 Feb 2025 16:24:28 +0100 Subject: [PATCH] add numberMatched and numberReturned for /collections (#197) --- CHANGES.md | 9 ++++++++- docker-compose.nginx.yml | 2 +- stac_fastapi/pgstac/core.py | 6 ++++++ tests/resources/test_collection.py | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 56cf572..211e815 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,12 @@ ## [Unreleased] +## [4.0.1] - 2025-02-06 + +### Added + +- add `numberReturned` and `numberMatched` in `/collections` response + ## [4.0.0] - 2025-02-03 ### Changed @@ -366,7 +372,8 @@ As a part of this release, this repository was extracted from the main - First PyPi release! -[Unreleased]: +[Unreleased]: +[4.0.1]: [4.0.0]: [3.0.1]: [3.0.0]: diff --git a/docker-compose.nginx.yml b/docker-compose.nginx.yml index 637a8c6..1bab43c 100644 --- a/docker-compose.nginx.yml +++ b/docker-compose.nginx.yml @@ -10,4 +10,4 @@ services: command: [ "nginx-debug", "-g", "daemon off;" ] app: environment: - - UVICORN_ROOT_PATH=/api/v1/pgstac + - ROOT_PATH=/api/v1/pgstac diff --git a/stac_fastapi/pgstac/core.py b/stac_fastapi/pgstac/core.py index a1bb629..559d8f0 100644 --- a/stac_fastapi/pgstac/core.py +++ b/stac_fastapi/pgstac/core.py @@ -148,6 +148,12 @@ async def all_collections( # noqa: C901 return Collections( collections=linked_collections or [], links=links, + numberMatched=collections_result.get( + "numberMatched", len(linked_collections) + ), + numberReturned=collections_result.get( + "numberReturned", len(linked_collections) + ), ) async def get_collection( diff --git a/tests/resources/test_collection.py b/tests/resources/test_collection.py index d25ed63..92c5943 100644 --- a/tests/resources/test_collection.py +++ b/tests/resources/test_collection.py @@ -116,6 +116,7 @@ async def test_nocollections( ): resp = await app_client.get("/collections") assert resp.status_code == 200 + assert resp.json()["numberReturned"] == 0 async def test_returns_valid_collection(app_client, load_test_data): @@ -168,6 +169,9 @@ async def test_returns_valid_links_in_collections(app_client, load_test_data): resp = await app_client.get("/collections") assert resp.status_code == 200 resp_json = resp.json() + assert resp.json()["numberReturned"] + assert resp.json()["numberMatched"] + collections = resp_json["collections"] # Find collection in list by ID single_coll = next(coll for coll in collections if coll["id"] == in_json["id"]) @@ -317,6 +321,8 @@ async def test_collection_search_freetext( "/collections", params={"q": "temperature"}, ) + assert resp.json()["numberReturned"] == 1 + assert resp.json()["numberMatched"] == 1 assert len(resp.json()["collections"]) == 1 assert resp.json()["collections"][0]["id"] == load_test2_collection.id @@ -341,6 +347,8 @@ async def test_all_collections_with_pagination(app_client, load_test_data): assert resp.status_code == 201 resp = await app_client.get("/collections") + assert resp.json()["numberReturned"] == 10 + assert resp.json()["numberMatched"] == 12 cols = resp.json()["collections"] assert len(cols) == 10 links = resp.json()["links"] @@ -348,6 +356,8 @@ async def test_all_collections_with_pagination(app_client, load_test_data): assert {"root", "self", "next"} == {link["rel"] for link in links} resp = await app_client.get("/collections", params={"limit": 12}) + assert resp.json()["numberReturned"] == 12 + assert resp.json()["numberMatched"] == 12 cols = resp.json()["collections"] assert len(cols) == 12 links = resp.json()["links"] @@ -369,6 +379,8 @@ async def test_all_collections_without_pagination(app_client_no_ext, load_test_d assert resp.status_code == 201 resp = await app_client_no_ext.get("/collections") + assert resp.json()["numberReturned"] == 12 + assert resp.json()["numberMatched"] == 12 cols = resp.json()["collections"] assert len(cols) == 12 links = resp.json()["links"] @@ -382,6 +394,8 @@ async def test_get_collections_search_pagination( app_client, load_test_collection, load_test2_collection ): resp = await app_client.get("/collections") + assert resp.json()["numberReturned"] == 2 + assert resp.json()["numberMatched"] == 2 cols = resp.json()["collections"] assert len(cols) == 2 links = resp.json()["links"]