Skip to content

Commit a4c796b

Browse files
committed
free text post
1 parent 1a78fa9 commit a4c796b

File tree

1 file changed

+78
-24
lines changed

1 file changed

+78
-24
lines changed

stac_fastapi/tests/api/test_api_search_collections.py

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -409,22 +409,12 @@ async def test_collections_query_extension(app_client, txn_client, ctx):
409409
# Test query extension with not-equal operator on ID
410410
query = {"id": {"neq": f"{test_prefix}-sentinel"}}
411411

412-
print(f"\nTesting neq query: {query}")
413-
print(f"JSON query: {json.dumps(query)}")
414-
415412
resp = await app_client.get(
416413
"/collections",
417414
params=[("query", json.dumps(query))],
418415
)
419-
print(f"Response status: {resp.status_code}")
420416
assert resp.status_code == 200
421417
resp_json = resp.json()
422-
print(f"Response JSON keys: {resp_json.keys()}")
423-
print(f"Number of collections in response: {len(resp_json.get('collections', []))}")
424-
425-
# Print all collection IDs in the response
426-
all_ids = [c["id"] for c in resp_json.get("collections", [])]
427-
print(f"All collection IDs in response: {all_ids}")
428418

429419
# Filter collections to only include the ones we created for this test
430420
found_collections = [
@@ -675,12 +665,6 @@ async def test_collections_post(app_client, txn_client, ctx):
675665
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
676666
]
677667

678-
# Debug print to see what's in the collections
679-
print(
680-
"Collection keys:",
681-
test_collections[0].keys() if test_collections else "No collections found",
682-
)
683-
684668
# Check that stac_version is excluded from the collections
685669
for collection in test_collections:
686670
assert "stac_version" not in collection
@@ -706,9 +690,6 @@ async def test_collections_search_cql2_text(app_client, txn_client, ctx):
706690
assert resp.status_code == 200
707691
resp_json = resp.json()
708692

709-
# Debug print to see what's in the response
710-
print("Collections in response:", [c["id"] for c in resp_json["collections"]])
711-
712693
# Filter collections to only include the ones with our test prefix
713694
filtered_collections = [
714695
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
@@ -726,11 +707,6 @@ async def test_collections_search_cql2_text(app_client, txn_client, ctx):
726707
assert resp.status_code == 200
727708
resp_json = resp.json()
728709

729-
# Debug print to see what's in the response
730-
print(
731-
"Collections in response (LIKE):", [c["id"] for c in resp_json["collections"]]
732-
)
733-
734710
# Filter collections to only include the ones with our test prefix
735711
filtered_collections = [
736712
c for c in resp_json["collections"] if c["id"].startswith(test_prefix)
@@ -741,3 +717,81 @@ async def test_collections_search_cql2_text(app_client, txn_client, ctx):
741717
len(filtered_collections) == 1
742718
) # We only created one collection with this prefix
743719
assert filtered_collections[0]["id"] == collection_id
720+
721+
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"

0 commit comments

Comments
 (0)