|
| 1 | +import os |
1 | 2 | import uuid |
2 | 3 | from copy import deepcopy |
3 | 4 | from typing import Callable |
|
10 | 11 |
|
11 | 12 | from ..conftest import MockRequest, create_item |
12 | 13 |
|
| 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 | + |
13 | 21 |
|
14 | 22 | @pytest.mark.asyncio |
15 | 23 | 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): |
297 | 305 | # ) |
298 | 306 |
|
299 | 307 |
|
| 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 | + |
300 | 353 | @pytest.mark.asyncio |
301 | 354 | async def test_feature_collection_insert( |
302 | 355 | core_client, |
|
0 commit comments