Skip to content

Commit 643cc0b

Browse files
committed
add test
1 parent f6c1aa0 commit 643cc0b

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ def bulk_item_insert(
927927
else:
928928
logger.info(f"Bulk sync operation succeeded with {success} actions.")
929929

930-
return f"Successfully added {success} Items. {attempted - success} errors occurred."
930+
return f"Successfully added/updated {success} Items. {attempted - success} errors occurred."
931931

932932

933933
_DEFAULT_QUERYABLES: Dict[str, Dict[str, Any]] = {

stac_fastapi/tests/clients/test_elasticsearch.py renamed to stac_fastapi/tests/clients/test_es_os.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import uuid
23
from copy import deepcopy
34
from typing import Callable
@@ -10,6 +11,13 @@
1011

1112
from ..conftest import MockRequest, create_item
1213

14+
if os.getenv("BACKEND", "elasticsearch").lower() == "opensearch":
15+
from stac_fastapi.opensearch.config import OpensearchSettings as SearchSettings
16+
else:
17+
from stac_fastapi.elasticsearch.config import (
18+
ElasticsearchSettings as SearchSettings,
19+
)
20+
1321

1422
@pytest.mark.asyncio
1523
async def test_create_collection(app_client, ctx, core_client, txn_client):
@@ -297,6 +305,51 @@ async def test_bulk_item_insert(ctx, core_client, txn_client, bulk_txn_client):
297305
# )
298306

299307

308+
@pytest.mark.asyncio
309+
async def test_bulk_item_insert_with_raise_on_error(
310+
ctx, core_client, txn_client, bulk_txn_client
311+
):
312+
"""
313+
Test bulk_item_insert behavior with RAISE_ON_BULK_ERROR set to true and false.
314+
315+
This test verifies that when RAISE_ON_BULK_ERROR is set to true, a ConflictError
316+
is raised for conflicting items. When set to false, the operation logs errors
317+
and continues gracefully.
318+
"""
319+
320+
# Insert an initial item to set up a conflict
321+
initial_item = deepcopy(ctx.item)
322+
initial_item["id"] = str(uuid.uuid4())
323+
await create_item(txn_client, initial_item)
324+
325+
# Verify the initial item is inserted
326+
fc = await core_client.item_collection(ctx.collection["id"], request=MockRequest())
327+
assert len(fc["features"]) >= 1
328+
329+
# Create conflicting items (same ID as the initial item)
330+
conflicting_items = {initial_item["id"]: deepcopy(initial_item)}
331+
332+
# Test with RAISE_ON_BULK_ERROR set to true
333+
os.environ["RAISE_ON_BULK_ERROR"] = "true"
334+
bulk_txn_client.database.sync_settings = SearchSettings()
335+
336+
with pytest.raises(ConflictError):
337+
bulk_txn_client.bulk_item_insert(Items(items=conflicting_items), refresh=True)
338+
339+
# Test with RAISE_ON_BULK_ERROR set to false
340+
os.environ["RAISE_ON_BULK_ERROR"] = "false"
341+
bulk_txn_client.database.sync_settings = SearchSettings() # Reinitialize settings
342+
result = bulk_txn_client.bulk_item_insert(
343+
Items(items=conflicting_items), refresh=True
344+
)
345+
346+
# Validate the results
347+
assert "Successfully added/updated 1 Items" in result
348+
349+
# Clean up the inserted item
350+
await txn_client.delete_item(initial_item["id"], ctx.item["collection"])
351+
352+
300353
@pytest.mark.asyncio
301354
async def test_feature_collection_insert(
302355
core_client,

0 commit comments

Comments
 (0)