|
| 1 | +import os |
| 2 | +import uuid |
| 3 | +from copy import deepcopy |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic import ValidationError |
| 7 | + |
| 8 | +from stac_fastapi.extensions.third_party.bulk_transactions import Items |
| 9 | +from stac_fastapi.types.errors import ConflictError |
| 10 | + |
| 11 | +from ..conftest import MockRequest, create_item |
| 12 | + |
| 13 | +if os.getenv("BACKEND", "elasticsearch").lower() == "opensearch": |
| 14 | + from stac_fastapi.opensearch.config import OpensearchSettings as SearchSettings |
| 15 | +else: |
| 16 | + from stac_fastapi.elasticsearch.config import ( |
| 17 | + ElasticsearchSettings as SearchSettings, |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.asyncio |
| 22 | +async def test_bulk_item_insert(ctx, core_client, txn_client, bulk_txn_client): |
| 23 | + items = {} |
| 24 | + for _ in range(10): |
| 25 | + _item = deepcopy(ctx.item) |
| 26 | + _item["id"] = str(uuid.uuid4()) |
| 27 | + items[_item["id"]] = _item |
| 28 | + |
| 29 | + # fc = es_core.item_collection(coll["id"], request=MockStarletteRequest) |
| 30 | + # assert len(fc["features"]) == 0 |
| 31 | + |
| 32 | + bulk_txn_client.bulk_item_insert(Items(items=items), refresh=True) |
| 33 | + |
| 34 | + fc = await core_client.item_collection(ctx.collection["id"], request=MockRequest()) |
| 35 | + assert len(fc["features"]) >= 10 |
| 36 | + |
| 37 | + # for item in items: |
| 38 | + # es_transactions.delete_item( |
| 39 | + # item["id"], item["collection"], request=MockStarletteRequest |
| 40 | + # ) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.asyncio |
| 44 | +async def test_bulk_item_insert_with_raise_on_error( |
| 45 | + ctx, core_client, txn_client, bulk_txn_client |
| 46 | +): |
| 47 | + """ |
| 48 | + Test bulk_item_insert behavior with RAISE_ON_BULK_ERROR set to true and false. |
| 49 | +
|
| 50 | + This test verifies that when RAISE_ON_BULK_ERROR is set to true, a ConflictError |
| 51 | + is raised for conflicting items. When set to false, the operation logs errors |
| 52 | + and continues gracefully. |
| 53 | + """ |
| 54 | + |
| 55 | + # Insert an initial item to set up a conflict |
| 56 | + initial_item = deepcopy(ctx.item) |
| 57 | + initial_item["id"] = str(uuid.uuid4()) |
| 58 | + await create_item(txn_client, initial_item) |
| 59 | + |
| 60 | + # Verify the initial item is inserted |
| 61 | + fc = await core_client.item_collection(ctx.collection["id"], request=MockRequest()) |
| 62 | + assert len(fc["features"]) >= 1 |
| 63 | + |
| 64 | + # Create conflicting items (same ID as the initial item) |
| 65 | + conflicting_items = {initial_item["id"]: deepcopy(initial_item)} |
| 66 | + |
| 67 | + # Test with RAISE_ON_BULK_ERROR set to true |
| 68 | + os.environ["RAISE_ON_BULK_ERROR"] = "true" |
| 69 | + bulk_txn_client.database.sync_settings = SearchSettings() |
| 70 | + |
| 71 | + with pytest.raises(ConflictError): |
| 72 | + bulk_txn_client.bulk_item_insert(Items(items=conflicting_items), refresh=True) |
| 73 | + |
| 74 | + # Test with RAISE_ON_BULK_ERROR set to false |
| 75 | + os.environ["RAISE_ON_BULK_ERROR"] = "false" |
| 76 | + bulk_txn_client.database.sync_settings = SearchSettings() # Reinitialize settings |
| 77 | + result = bulk_txn_client.bulk_item_insert( |
| 78 | + Items(items=conflicting_items), refresh=True |
| 79 | + ) |
| 80 | + |
| 81 | + # Validate the results |
| 82 | + assert "Successfully added/updated 1 Items" in result |
| 83 | + |
| 84 | + # Clean up the inserted item |
| 85 | + await txn_client.delete_item(initial_item["id"], ctx.item["collection"]) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_feature_collection_insert( |
| 90 | + core_client, |
| 91 | + txn_client, |
| 92 | + ctx, |
| 93 | +): |
| 94 | + features = [] |
| 95 | + for _ in range(10): |
| 96 | + _item = deepcopy(ctx.item) |
| 97 | + _item["id"] = str(uuid.uuid4()) |
| 98 | + features.append(_item) |
| 99 | + |
| 100 | + feature_collection = {"type": "FeatureCollection", "features": features} |
| 101 | + |
| 102 | + await create_item(txn_client, feature_collection) |
| 103 | + |
| 104 | + fc = await core_client.item_collection(ctx.collection["id"], request=MockRequest()) |
| 105 | + assert len(fc["features"]) >= 10 |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio |
| 109 | +async def test_bulk_item_insert_validation_error(ctx, core_client, bulk_txn_client): |
| 110 | + items = {} |
| 111 | + # Add 9 valid items |
| 112 | + for _ in range(9): |
| 113 | + _item = deepcopy(ctx.item) |
| 114 | + _item["id"] = str(uuid.uuid4()) |
| 115 | + items[_item["id"]] = _item |
| 116 | + |
| 117 | + # Add 1 invalid item (e.g., missing "datetime") |
| 118 | + invalid_item = deepcopy(ctx.item) |
| 119 | + invalid_item["id"] = str(uuid.uuid4()) |
| 120 | + invalid_item["properties"].pop( |
| 121 | + "datetime", None |
| 122 | + ) # Remove datetime to make it invalid |
| 123 | + items[invalid_item["id"]] = invalid_item |
| 124 | + |
| 125 | + # The bulk insert should raise a ValidationError due to the invalid item |
| 126 | + with pytest.raises(ValidationError): |
| 127 | + bulk_txn_client.bulk_item_insert(Items(items=items), refresh=True) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.asyncio |
| 131 | +async def test_feature_collection_insert_validation_error( |
| 132 | + core_client, |
| 133 | + txn_client, |
| 134 | + ctx, |
| 135 | +): |
| 136 | + features = [] |
| 137 | + # Add 9 valid items |
| 138 | + for _ in range(9): |
| 139 | + _item = deepcopy(ctx.item) |
| 140 | + _item["id"] = str(uuid.uuid4()) |
| 141 | + features.append(_item) |
| 142 | + |
| 143 | + # Add 1 invalid item (e.g., missing "datetime") |
| 144 | + invalid_item = deepcopy(ctx.item) |
| 145 | + invalid_item["id"] = str(uuid.uuid4()) |
| 146 | + invalid_item["properties"].pop( |
| 147 | + "datetime", None |
| 148 | + ) # Remove datetime to make it invalid |
| 149 | + features.append(invalid_item) |
| 150 | + |
| 151 | + feature_collection = {"type": "FeatureCollection", "features": features} |
| 152 | + |
| 153 | + # Assert that a ValidationError is raised due to the invalid item |
| 154 | + with pytest.raises(ValidationError): |
| 155 | + await create_item(txn_client, feature_collection) |
0 commit comments