Skip to content

Commit cdd673f

Browse files
committed
update fields extension test
1 parent 6c54a09 commit cdd673f

File tree

1 file changed

+71
-48
lines changed

1 file changed

+71
-48
lines changed

stac_fastapi/tests/api/test_api_search_collections.py

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ async def test_collections_sort_id_desc(app_client, txn_client, ctx):
8585

8686

8787
@pytest.mark.asyncio
88-
async def test_collections_fields(app_client, txn_client, ctx):
89-
"""Verify GET /collections honors the fields parameter."""
88+
async def test_collections_fields_all_endpoints(app_client, txn_client, ctx):
89+
"""Verify GET /collections, GET /collections-search, and POST /collections-search honor the fields parameter."""
9090
# Create multiple collections with different ids
9191
base_collection = ctx.collection
9292

@@ -104,61 +104,84 @@ async def test_collections_fields(app_client, txn_client, ctx):
104104

105105
await refresh_indices(txn_client)
106106

107-
# Test include fields parameter
108-
resp = await app_client.get(
109-
"/collections",
110-
params=[("fields", "id"), ("fields", "title")],
111-
)
112-
assert resp.status_code == 200
113-
resp_json = resp.json()
107+
# Define endpoints to test
108+
endpoints = [
109+
{"method": "GET", "path": "/collections", "params": [("fields", "id,title")]},
110+
{
111+
"method": "GET",
112+
"path": "/collections-search",
113+
"params": [("fields", "id,title")],
114+
},
115+
{
116+
"method": "POST",
117+
"path": "/collections-search",
118+
"body": {"fields": {"include": ["id", "title"]}},
119+
},
120+
]
114121

115-
# Check if collections exist in the response
116-
assert "collections" in resp_json, "No collections in response"
122+
for endpoint in endpoints:
123+
if endpoint["method"] == "GET":
124+
resp = await app_client.get(endpoint["path"], params=endpoint["params"])
125+
else: # POST
126+
resp = await app_client.post(endpoint["path"], json=endpoint["body"])
117127

118-
# Filter collections to only include the ones we created for this test
119-
test_collections = []
120-
for c in resp_json["collections"]:
121-
if "id" in c and c["id"].startswith(test_prefix):
122-
test_collections.append(c)
128+
assert resp.status_code == 200
129+
resp_json = resp.json()
123130

124-
# Filter collections to only include the ones we created for this test
125-
test_collections = []
126-
for c in resp_json["collections"]:
127-
if "id" in c and c["id"].startswith(test_prefix):
128-
test_collections.append(c)
131+
collections_list = resp_json["collections"]
129132

130-
# Collections should only have id and title fields
131-
for collection in test_collections:
132-
assert "id" in collection
133-
assert "title" in collection
134-
assert "description" not in collection
135-
assert "links" in collection # links are always included
133+
# Filter collections to only include the ones we created for this test
134+
test_collections = [
135+
c for c in collections_list if c["id"].startswith(test_prefix)
136+
]
137+
138+
# Collections should only have id and title fields
139+
for collection in test_collections:
140+
assert "id" in collection
141+
assert "title" in collection
142+
assert "description" not in collection
136143

137144
# Test exclude fields parameter
138-
resp = await app_client.get(
139-
"/collections",
140-
params=[("fields", "-description")],
141-
)
142-
assert resp.status_code == 200
143-
resp_json = resp.json()
145+
endpoints = [
146+
{
147+
"method": "GET",
148+
"path": "/collections",
149+
"params": [("fields", "-description")],
150+
},
151+
{
152+
"method": "GET",
153+
"path": "/collections-search",
154+
"params": [("fields", "-description")],
155+
},
156+
{
157+
"method": "POST",
158+
"path": "/collections-search",
159+
"body": {"fields": {"exclude": ["description"]}},
160+
},
161+
]
144162

145-
# Check if collections exist in the response
146-
assert (
147-
"collections" in resp_json
148-
), "No collections in response for exclude fields test"
163+
for endpoint in endpoints:
164+
if endpoint["method"] == "GET":
165+
resp = await app_client.get(endpoint["path"], params=endpoint["params"])
166+
else: # POST
167+
resp = await app_client.post(endpoint["path"], json=endpoint["body"])
149168

150-
# Filter collections to only include the ones we created for this test
151-
test_collections = []
152-
for c in resp_json["collections"]:
153-
if "id" in c and c["id"].startswith(test_prefix):
154-
test_collections.append(c)
169+
assert resp.status_code == 200
170+
resp_json = resp.json()
155171

156-
# Collections should have all fields except description
157-
for collection in test_collections:
158-
assert "id" in collection
159-
assert "title" in collection
160-
assert "description" not in collection
161-
assert "links" in collection
172+
collections_list = resp_json["collections"]
173+
174+
# Filter collections to only include the ones we created for this test
175+
test_collections = [
176+
c for c in collections_list if c["id"].startswith(test_prefix)
177+
]
178+
179+
# Collections should have all fields except description
180+
for collection in test_collections:
181+
assert "id" in collection
182+
assert "title" in collection
183+
assert "description" not in collection
184+
assert "links" in collection # links are always included
162185

163186

164187
@pytest.mark.asyncio

0 commit comments

Comments
 (0)