Skip to content

Commit bcf6697

Browse files
committed
add numberMatched and numberReturned for /collections
1 parent d42025a commit bcf6697

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

CHANGES.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased]
44

5+
## [4.0.1] - 2025-02-06
6+
7+
### Added
8+
9+
- add `numberReturned` and `numberMatched` in `/collections` response
10+
511
## [4.0.0] - 2025-02-03
612

713
### Changed
@@ -366,7 +372,8 @@ As a part of this release, this repository was extracted from the main
366372

367373
- First PyPi release!
368374

369-
[Unreleased]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/4.0.0..main>
375+
[Unreleased]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/4.0.1..main>
376+
[4.0.1]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/4.0.0..4.0.1>
370377
[4.0.0]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/3.0.1..4.0.0>
371378
[3.0.1]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/3.0.0..3.0.1>
372379
[3.0.0]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/2.5.0..3.0.0>

docker-compose.nginx.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ services:
1010
command: [ "nginx-debug", "-g", "daemon off;" ]
1111
app:
1212
environment:
13-
- UVICORN_ROOT_PATH=/api/v1/pgstac
13+
- ROOT_PATH=/api/v1/pgstac

stac_fastapi/pgstac/core.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,19 @@ async def all_collections( # noqa: C901
114114
)
115115
collections_result = {"collections": cols, "links": []}
116116

117-
linked_collections: List[Collection] = []
118-
collections = collections_result["collections"]
119-
if collections is not None and len(collections) > 0:
120-
for c in collections:
117+
collections = Collections(
118+
collections=[],
119+
links=[],
120+
numberReturned=collections_result.get(
121+
"numberReturned", len(collections_result["collections"])
122+
),
123+
)
124+
if collections_result.get("numberMatched", None) is not None:
125+
collections["numberMatched"] = collections_result.get("numberMatched")
126+
127+
cols = collections_result["collections"]
128+
if cols is not None and len(cols) > 0:
129+
for c in cols:
121130
coll = Collection(**c)
122131
coll["links"] = await CollectionLinks(
123132
collection_id=coll["id"], request=request
@@ -137,18 +146,15 @@ async def all_collections( # noqa: C901
137146
}
138147
)
139148

140-
linked_collections.append(coll)
149+
collections["collections"].append(coll)
141150

142-
links = await CollectionSearchPagingLinks(
151+
collections["links"] = await CollectionSearchPagingLinks(
143152
request=request,
144153
next=next_link,
145154
prev=prev_link,
146155
).get_links()
147156

148-
return Collections(
149-
collections=linked_collections or [],
150-
links=links,
151-
)
157+
return collections
152158

153159
async def get_collection(
154160
self, collection_id: str, request: Request, **kwargs

tests/resources/test_collection.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ async def test_nocollections(
116116
):
117117
resp = await app_client.get("/collections")
118118
assert resp.status_code == 200
119+
assert resp.json()["numberReturned"] == 0
119120

120121

121122
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):
168169
resp = await app_client.get("/collections")
169170
assert resp.status_code == 200
170171
resp_json = resp.json()
172+
assert resp.json()["numberReturned"]
173+
assert resp.json()["numberMatched"]
174+
171175
collections = resp_json["collections"]
172176
# Find collection in list by ID
173177
single_coll = next(coll for coll in collections if coll["id"] == in_json["id"])
@@ -317,6 +321,8 @@ async def test_collection_search_freetext(
317321
"/collections",
318322
params={"q": "temperature"},
319323
)
324+
assert resp.json()["numberReturned"] == 1
325+
assert resp.json()["numberMatched"] == 1
320326
assert len(resp.json()["collections"]) == 1
321327
assert resp.json()["collections"][0]["id"] == load_test2_collection.id
322328

@@ -341,13 +347,17 @@ async def test_all_collections_with_pagination(app_client, load_test_data):
341347
assert resp.status_code == 201
342348

343349
resp = await app_client.get("/collections")
350+
assert resp.json()["numberReturned"] == 10
351+
assert resp.json()["numberMatched"] == 12
344352
cols = resp.json()["collections"]
345353
assert len(cols) == 10
346354
links = resp.json()["links"]
347355
assert len(links) == 3
348356
assert {"root", "self", "next"} == {link["rel"] for link in links}
349357

350358
resp = await app_client.get("/collections", params={"limit": 12})
359+
assert resp.json()["numberReturned"] == 12
360+
assert resp.json()["numberMatched"] == 12
351361
cols = resp.json()["collections"]
352362
assert len(cols) == 12
353363
links = resp.json()["links"]
@@ -369,6 +379,8 @@ async def test_all_collections_without_pagination(app_client_no_ext, load_test_d
369379
assert resp.status_code == 201
370380

371381
resp = await app_client_no_ext.get("/collections")
382+
assert resp.json()["numberReturned"] == 12
383+
assert "numberMatched" not in resp.json()
372384
cols = resp.json()["collections"]
373385
assert len(cols) == 12
374386
links = resp.json()["links"]
@@ -382,6 +394,8 @@ async def test_get_collections_search_pagination(
382394
app_client, load_test_collection, load_test2_collection
383395
):
384396
resp = await app_client.get("/collections")
397+
assert resp.json()["numberReturned"] == 2
398+
assert resp.json()["numberMatched"] == 2
385399
cols = resp.json()["collections"]
386400
assert len(cols) == 2
387401
links = resp.json()["links"]

0 commit comments

Comments
 (0)