Skip to content

Commit 6c54a09

Browse files
committed
combine, improve free-text tests
1 parent 170a0a2 commit 6c54a09

File tree

1 file changed

+41
-139
lines changed

1 file changed

+41
-139
lines changed

stac_fastapi/tests/api/test_api_search_collections.py

Lines changed: 41 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -162,78 +162,58 @@ async def test_collections_fields(app_client, txn_client, ctx):
162162

163163

164164
@pytest.mark.asyncio
165-
async def test_collections_free_text_search_get(app_client, txn_client, ctx):
166-
"""Verify GET /collections honors the q parameter for free text search."""
167-
# Create multiple collections with different content
165+
async def test_collections_free_text_all_endpoints(app_client, txn_client, ctx):
166+
"""Test free text search across all collection endpoints."""
167+
# Create test data
168+
test_prefix = f"free-text-{uuid.uuid4().hex[:8]}"
168169
base_collection = ctx.collection
170+
search_term = "SEARCHABLETERM"
169171

170-
# Use unique prefixes to avoid conflicts between tests
171-
test_prefix = f"q-get-{uuid.uuid4().hex[:8]}"
172-
173-
test_collections = [
174-
{
175-
"id": f"{test_prefix}-sentinel",
176-
"title": "Sentinel-2 Collection",
177-
"description": "Collection of Sentinel-2 data",
178-
"summaries": {"platform": ["sentinel-2a", "sentinel-2b"]},
179-
},
180-
{
181-
"id": f"{test_prefix}-landsat",
182-
"title": "Landsat Collection",
183-
"description": "Collection of Landsat data",
184-
"summaries": {"platform": ["landsat-8", "landsat-9"]},
185-
},
186-
{
187-
"id": f"{test_prefix}-modis",
188-
"title": "MODIS Collection",
189-
"description": "Collection of MODIS data",
190-
"summaries": {"platform": ["terra", "aqua"]},
191-
},
192-
]
172+
# Create test collections
173+
target_collection = base_collection.copy()
174+
target_collection["id"] = f"{test_prefix}-target"
175+
target_collection["title"] = f"Collection with {search_term} in title"
176+
await create_collection(txn_client, target_collection)
193177

194-
for i, coll in enumerate(test_collections):
195-
test_collection = base_collection.copy()
196-
test_collection["id"] = coll["id"]
197-
test_collection["title"] = coll["title"]
198-
test_collection["description"] = coll["description"]
199-
test_collection["summaries"] = coll["summaries"]
200-
await create_collection(txn_client, test_collection)
178+
decoy_collection = base_collection.copy()
179+
decoy_collection["id"] = f"{test_prefix}-decoy"
180+
decoy_collection["title"] = "Collection without the term"
181+
await create_collection(txn_client, decoy_collection)
201182

202183
await refresh_indices(txn_client)
203184

204-
# Test free text search for "sentinel"
205-
resp = await app_client.get(
206-
"/collections",
207-
params=[("q", "sentinel")],
208-
)
209-
assert resp.status_code == 200
210-
resp_json = resp.json()
211-
212-
# Filter collections to only include the ones we created for this test
213-
found_collections = [
214-
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
185+
# Define endpoints to test
186+
endpoints = [
187+
{"method": "GET", "path": "/collections", "param": "q"},
188+
{"method": "GET", "path": "/collections-search", "param": "q"},
189+
{"method": "POST", "path": "/collections-search", "body_key": "q"},
215190
]
216191

217-
# Should only find the sentinel collection
218-
assert len(found_collections) == 1
219-
assert found_collections[0]["id"] == f"{test_prefix}-sentinel"
192+
for endpoint in endpoints:
193+
print(f"Testing free text search on {endpoint['method']} {endpoint['path']}")
220194

221-
# Test free text search for "landsat"
222-
resp = await app_client.get(
223-
"/collections",
224-
params=[("q", "modis")],
225-
)
226-
assert resp.status_code == 200
227-
resp_json = resp.json()
195+
if endpoint["method"] == "GET":
196+
params = [(endpoint["param"], search_term)]
197+
resp = await app_client.get(endpoint["path"], params=params)
198+
else: # POST
199+
body = {endpoint["body_key"]: search_term}
200+
resp = await app_client.post(endpoint["path"], json=body)
228201

229-
# Filter collections to only include the ones we created for this test
230-
found_collections = [
231-
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
232-
]
202+
assert (
203+
resp.status_code == 200
204+
), f"Failed for {endpoint['method']} {endpoint['path']} with status {resp.status_code}"
205+
resp_json = resp.json()
233206

234-
# Should only find the landsat collection
235-
assert len(found_collections) == 1
236-
assert found_collections[0]["id"] == f"{test_prefix}-modis"
207+
collections = resp_json["collections"]
208+
209+
# Filter to our test collections
210+
found = [c for c in collections if c["id"].startswith(test_prefix)]
211+
assert (
212+
len(found) == 1
213+
), f"Expected 1 collection, found {len(found)} for {endpoint['method']} {endpoint['path']}"
214+
assert (
215+
found[0]["id"] == target_collection["id"]
216+
), f"Expected {target_collection['id']}, found {found[0]['id']} for {endpoint['method']} {endpoint['path']}"
237217

238218

239219
@pytest.mark.asyncio
@@ -719,84 +699,6 @@ async def test_collections_search_cql2_text(app_client, txn_client, ctx):
719699
assert filtered_collections[0]["id"] == collection_id
720700

721701

722-
@pytest.mark.asyncio
723-
async def test_collections_search_free_text(app_client, txn_client, ctx):
724-
"""Test collections search with free text search (q parameter)."""
725-
# Create a unique prefix for test collections
726-
test_prefix = f"test-{uuid.uuid4()}"
727-
728-
# Create a collection with a simple, searchable title
729-
searchable_term = "SEARCHABLETERM"
730-
target_collection = ctx.collection.copy()
731-
target_collection["id"] = f"{test_prefix}-target"
732-
target_collection["title"] = f"Collection with {searchable_term} in the title"
733-
target_collection["description"] = "This is the collection we want to find"
734-
await create_collection(txn_client, target_collection)
735-
736-
# Collection 2: Similar but without the searchable term
737-
decoy_collection = ctx.collection.copy()
738-
decoy_collection["id"] = f"{test_prefix}-decoy"
739-
decoy_collection["title"] = "Collection with similar words in the title"
740-
decoy_collection["description"] = "This is a decoy collection"
741-
await create_collection(txn_client, decoy_collection)
742-
743-
# Make sure to refresh indices and wait a moment
744-
await refresh_indices(txn_client)
745-
746-
# First, verify that our collections are actually in the database
747-
resp = await app_client.get("/collections")
748-
assert resp.status_code == 200
749-
resp_json = resp.json()
750-
751-
# Get all collections from the response
752-
all_collections = resp_json["collections"]
753-
754-
# Check that our test collections are present
755-
test_collections = [c for c in all_collections if c["id"].startswith(test_prefix)]
756-
assert (
757-
len(test_collections) >= 2
758-
), f"Expected at least 2 test collections, got {len(test_collections)}"
759-
760-
# Verify our target collection is present and has the searchable term
761-
target_collections = [
762-
c for c in test_collections if c["id"] == target_collection["id"]
763-
]
764-
assert (
765-
len(target_collections) == 1
766-
), f"Target collection not found: {target_collection['id']}"
767-
assert searchable_term in target_collections[0]["title"]
768-
769-
# Now test the free text search
770-
resp = await app_client.get(f"/collections-search?q={searchable_term}")
771-
assert resp.status_code == 200
772-
resp_json = resp.json()
773-
774-
# Get all collections with our test prefix
775-
found_collections = [
776-
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
777-
]
778-
779-
# Verify that our target collection is returned
780-
assert target_collection["id"] in [
781-
c["id"] for c in found_collections
782-
], f"Target collection {target_collection['id']} not within search results"
783-
784-
# Test POST search with free text search
785-
resp = await app_client.post("/collections-search", json={"q": searchable_term})
786-
assert resp.status_code == 200
787-
resp_json = resp.json()
788-
789-
# Get all collections with our test prefix
790-
found_collections = [
791-
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
792-
]
793-
794-
# Verify that our target collection is returned
795-
assert target_collection["id"] in [
796-
c["id"] for c in found_collections
797-
], f"Target collection {target_collection['id']} not found within POST search results"
798-
799-
800702
@pytest.mark.asyncio
801703
async def test_collections_pagination_all_endpoints(app_client, txn_client, ctx):
802704
"""Test pagination works correctly across all collection endpoints."""

0 commit comments

Comments
 (0)