Skip to content

Commit e7edc9e

Browse files
committed
q test
1 parent 4454b7e commit e7edc9e

File tree

2 files changed

+170
-2
lines changed

2 files changed

+170
-2
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ async def all_collections(
228228
self,
229229
fields: Optional[List[str]] = None,
230230
sortby: Optional[str] = None,
231-
q: Optional[List[str]] = None,
231+
q: Optional[Union[str, List[str]]] = None,
232232
**kwargs,
233233
) -> stac_types.Collections:
234234
"""Read all collections from the database.
@@ -247,6 +247,8 @@ async def all_collections(
247247
limit = int(request.query_params.get("limit", os.getenv("STAC_ITEM_LIMIT", 10)))
248248
token = request.query_params.get("token")
249249

250+
print("q: ", q)
251+
250252
# Process fields parameter for filtering collection properties
251253
includes, excludes = set(), set()
252254
if fields and self.extension_is_enabled("FieldsExtension"):
@@ -271,8 +273,13 @@ async def all_collections(
271273
if parsed_sort:
272274
sort = parsed_sort
273275

276+
# Convert q to a list if it's a string
277+
q_list = None
278+
if q is not None:
279+
q_list = [q] if isinstance(q, str) else q
280+
274281
collections, next_token = await self.database.get_all_collections(
275-
token=token, limit=limit, request=request, sort=sort, q=q
282+
token=token, limit=limit, request=request, sort=sort, q=q_list
276283
)
277284

278285
# Apply field filtering if fields parameter was provided

stac_fastapi/tests/api/test_api_search_collections.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,164 @@ async def test_collections_fields(app_client, txn_client, load_test_data):
152152
assert "title" in collection
153153
assert "description" not in collection
154154
assert "links" in collection
155+
156+
157+
@pytest.mark.asyncio
158+
async def test_collections_free_text_search_get(app_client, txn_client, load_test_data):
159+
"""Verify GET /collections honors the q parameter for free text search."""
160+
# Create multiple collections with different content
161+
base_collection = load_test_data("test_collection.json")
162+
163+
# Use unique prefixes to avoid conflicts between tests
164+
test_prefix = f"q-get-{uuid.uuid4().hex[:8]}"
165+
166+
# Create collections with different content to test free text search
167+
test_collections = [
168+
{
169+
"id": f"{test_prefix}-sentinel",
170+
"title": "Sentinel-2 Collection",
171+
"description": "Collection of Sentinel-2 data",
172+
"summaries": {"platform": ["sentinel-2a", "sentinel-2b"]},
173+
},
174+
{
175+
"id": f"{test_prefix}-landsat",
176+
"title": "Landsat Collection",
177+
"description": "Collection of Landsat data",
178+
"summaries": {"platform": ["landsat-8", "landsat-9"]},
179+
},
180+
{
181+
"id": f"{test_prefix}-modis",
182+
"title": "MODIS Collection",
183+
"description": "Collection of MODIS data",
184+
"summaries": {"platform": ["terra", "aqua"]},
185+
},
186+
]
187+
188+
for i, coll in enumerate(test_collections):
189+
test_collection = base_collection.copy()
190+
test_collection["id"] = coll["id"]
191+
test_collection["title"] = coll["title"]
192+
test_collection["description"] = coll["description"]
193+
test_collection["summaries"] = coll["summaries"]
194+
await create_collection(txn_client, test_collection)
195+
196+
# Test free text search for "sentinel"
197+
resp = await app_client.get(
198+
"/collections",
199+
params=[("q", "sentinel")],
200+
)
201+
assert resp.status_code == 200
202+
resp_json = resp.json()
203+
204+
# Filter collections to only include the ones we created for this test
205+
found_collections = [
206+
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
207+
]
208+
209+
# Should only find the sentinel collection
210+
assert len(found_collections) == 1
211+
assert found_collections[0]["id"] == f"{test_prefix}-sentinel"
212+
213+
# Test free text search for "landsat"
214+
resp = await app_client.get(
215+
"/collections",
216+
params=[("q", "modis")],
217+
)
218+
assert resp.status_code == 200
219+
resp_json = resp.json()
220+
221+
# Filter collections to only include the ones we created for this test
222+
found_collections = [
223+
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
224+
]
225+
226+
# Should only find the landsat collection
227+
assert len(found_collections) == 1
228+
assert found_collections[0]["id"] == f"{test_prefix}-modis"
229+
230+
231+
# @pytest.mark.asyncio
232+
# async def test_collections_free_text_search_post(app_client, txn_client, load_test_data):
233+
# """Verify POST /collections-search honors the q parameter for free text search."""
234+
# # Create multiple collections with different content
235+
# base_collection = load_test_data("test_collection.json")
236+
237+
# # Use unique prefixes to avoid conflicts between tests
238+
# test_prefix = f"q-post-{uuid.uuid4().hex[:8]}"
239+
240+
# # Create collections with different content to test free text search
241+
# test_collections = [
242+
# {
243+
# "id": f"{test_prefix}-sentinel",
244+
# "title": "Sentinel-2 Collection",
245+
# "description": "Collection of Sentinel-2 data",
246+
# "summaries": {"platform": ["sentinel-2a", "sentinel-2b"]}
247+
# },
248+
# {
249+
# "id": f"{test_prefix}-landsat",
250+
# "title": "Landsat Collection",
251+
# "description": "Collection of Landsat data",
252+
# "summaries": {"platform": ["landsat-8", "landsat-9"]}
253+
# },
254+
# {
255+
# "id": f"{test_prefix}-modis",
256+
# "title": "MODIS Collection",
257+
# "description": "Collection of MODIS data",
258+
# "summaries": {"platform": ["terra", "aqua"]}
259+
# }
260+
# ]
261+
262+
# for i, coll in enumerate(test_collections):
263+
# test_collection = base_collection.copy()
264+
# test_collection["id"] = coll["id"]
265+
# test_collection["title"] = coll["title"]
266+
# test_collection["description"] = coll["description"]
267+
# test_collection["summaries"] = coll["summaries"]
268+
# await create_collection(txn_client, test_collection)
269+
270+
# # Test free text search for "sentinel" via POST
271+
# resp = await app_client.post(
272+
# "/collections-search",
273+
# json={"q": ["sentinel"]},
274+
# )
275+
# assert resp.status_code == 200
276+
# resp_json = resp.json()
277+
278+
# # Filter collections to only include the ones we created for this test
279+
# found_collections = [c for c in resp_json["collections"] if c["id"].startswith(test_prefix)]
280+
281+
# # Should only find the sentinel collection
282+
# assert len(found_collections) == 1
283+
# assert found_collections[0]["id"] == f"{test_prefix}-sentinel"
284+
285+
# # Test free text search for "landsat" via POST
286+
# resp = await app_client.post(
287+
# "/collections-search",
288+
# json={"q": ["landsat"]},
289+
# )
290+
# assert resp.status_code == 200
291+
# resp_json = resp.json()
292+
293+
# # Filter collections to only include the ones we created for this test
294+
# found_collections = [c for c in resp_json["collections"] if c["id"].startswith(test_prefix)]
295+
296+
# # Should only find the landsat collection
297+
# assert len(found_collections) == 1
298+
# assert found_collections[0]["id"] == f"{test_prefix}-landsat"
299+
300+
# # Test free text search with multiple terms
301+
# resp = await app_client.post(
302+
# "/collections-search",
303+
# json={"q": ["sentinel", "modis"]},
304+
# )
305+
# assert resp.status_code == 200
306+
# resp_json = resp.json()
307+
308+
# # Filter collections to only include the ones we created for this test
309+
# found_collections = [c for c in resp_json["collections"] if c["id"].startswith(test_prefix)]
310+
# found_ids = [c["id"] for c in found_collections]
311+
312+
# # Should find both sentinel and modis collections
313+
# assert len(found_collections) == 2
314+
# assert f"{test_prefix}-sentinel" in found_ids
315+
# assert f"{test_prefix}-modis" in found_ids

0 commit comments

Comments
 (0)