-
Notifications
You must be signed in to change notification settings - Fork 25
Ensure consistent validation #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f4d190
Refactor create_item method to include base_url and exist_ok parameters
jonhealy1 0f85dcf
update changelog
jonhealy1 a6b6e10
test, validate items in bulk transacton client
jonhealy1 d6eda04
update readme, changelog
jonhealy1 b9c88c4
remove commented out loop
jonhealy1 94ad607
remove unnecessary code
jonhealy1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import os | ||
import uuid | ||
from copy import deepcopy | ||
|
||
import pytest | ||
from pydantic import ValidationError | ||
|
||
from stac_fastapi.extensions.third_party.bulk_transactions import Items | ||
from stac_fastapi.types.errors import ConflictError | ||
|
||
from ..conftest import MockRequest, create_item | ||
|
||
if os.getenv("BACKEND", "elasticsearch").lower() == "opensearch": | ||
from stac_fastapi.opensearch.config import OpensearchSettings as SearchSettings | ||
else: | ||
from stac_fastapi.elasticsearch.config import ( | ||
ElasticsearchSettings as SearchSettings, | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_bulk_item_insert(ctx, core_client, txn_client, bulk_txn_client): | ||
items = {} | ||
for _ in range(10): | ||
_item = deepcopy(ctx.item) | ||
_item["id"] = str(uuid.uuid4()) | ||
items[_item["id"]] = _item | ||
|
||
# fc = es_core.item_collection(coll["id"], request=MockStarletteRequest) | ||
# assert len(fc["features"]) == 0 | ||
|
||
bulk_txn_client.bulk_item_insert(Items(items=items), refresh=True) | ||
|
||
fc = await core_client.item_collection(ctx.collection["id"], request=MockRequest()) | ||
assert len(fc["features"]) >= 10 | ||
|
||
# for item in items: | ||
jonhealy1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# es_transactions.delete_item( | ||
# item["id"], item["collection"], request=MockStarletteRequest | ||
# ) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_bulk_item_insert_with_raise_on_error( | ||
ctx, core_client, txn_client, bulk_txn_client | ||
): | ||
""" | ||
Test bulk_item_insert behavior with RAISE_ON_BULK_ERROR set to true and false. | ||
|
||
This test verifies that when RAISE_ON_BULK_ERROR is set to true, a ConflictError | ||
is raised for conflicting items. When set to false, the operation logs errors | ||
and continues gracefully. | ||
""" | ||
|
||
# Insert an initial item to set up a conflict | ||
initial_item = deepcopy(ctx.item) | ||
initial_item["id"] = str(uuid.uuid4()) | ||
await create_item(txn_client, initial_item) | ||
|
||
# Verify the initial item is inserted | ||
fc = await core_client.item_collection(ctx.collection["id"], request=MockRequest()) | ||
assert len(fc["features"]) >= 1 | ||
|
||
# Create conflicting items (same ID as the initial item) | ||
conflicting_items = {initial_item["id"]: deepcopy(initial_item)} | ||
|
||
# Test with RAISE_ON_BULK_ERROR set to true | ||
os.environ["RAISE_ON_BULK_ERROR"] = "true" | ||
bulk_txn_client.database.sync_settings = SearchSettings() | ||
|
||
with pytest.raises(ConflictError): | ||
bulk_txn_client.bulk_item_insert(Items(items=conflicting_items), refresh=True) | ||
|
||
# Test with RAISE_ON_BULK_ERROR set to false | ||
os.environ["RAISE_ON_BULK_ERROR"] = "false" | ||
bulk_txn_client.database.sync_settings = SearchSettings() # Reinitialize settings | ||
result = bulk_txn_client.bulk_item_insert( | ||
Items(items=conflicting_items), refresh=True | ||
) | ||
|
||
# Validate the results | ||
assert "Successfully added/updated 1 Items" in result | ||
|
||
# Clean up the inserted item | ||
await txn_client.delete_item(initial_item["id"], ctx.item["collection"]) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_feature_collection_insert( | ||
core_client, | ||
txn_client, | ||
ctx, | ||
): | ||
features = [] | ||
for _ in range(10): | ||
_item = deepcopy(ctx.item) | ||
_item["id"] = str(uuid.uuid4()) | ||
features.append(_item) | ||
|
||
feature_collection = {"type": "FeatureCollection", "features": features} | ||
|
||
await create_item(txn_client, feature_collection) | ||
|
||
fc = await core_client.item_collection(ctx.collection["id"], request=MockRequest()) | ||
assert len(fc["features"]) >= 10 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_bulk_item_insert_validation_error(ctx, core_client, bulk_txn_client): | ||
items = {} | ||
# Add 9 valid items | ||
for _ in range(9): | ||
_item = deepcopy(ctx.item) | ||
_item["id"] = str(uuid.uuid4()) | ||
items[_item["id"]] = _item | ||
|
||
# Add 1 invalid item (e.g., missing "datetime") | ||
invalid_item = deepcopy(ctx.item) | ||
invalid_item["id"] = str(uuid.uuid4()) | ||
invalid_item["properties"].pop( | ||
"datetime", None | ||
) # Remove datetime to make it invalid | ||
items[invalid_item["id"]] = invalid_item | ||
|
||
# The bulk insert should raise a ValidationError due to the invalid item | ||
with pytest.raises(ValidationError): | ||
bulk_txn_client.bulk_item_insert(Items(items=items), refresh=True) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_feature_collection_insert_validation_error( | ||
core_client, | ||
txn_client, | ||
ctx, | ||
): | ||
features = [] | ||
# Add 9 valid items | ||
for _ in range(9): | ||
_item = deepcopy(ctx.item) | ||
_item["id"] = str(uuid.uuid4()) | ||
features.append(_item) | ||
|
||
# Add 1 invalid item (e.g., missing "datetime") | ||
invalid_item = deepcopy(ctx.item) | ||
invalid_item["id"] = str(uuid.uuid4()) | ||
invalid_item["properties"].pop( | ||
"datetime", None | ||
) # Remove datetime to make it invalid | ||
features.append(invalid_item) | ||
|
||
feature_collection = {"type": "FeatureCollection", "features": features} | ||
|
||
# Assert that a ValidationError is raised due to the invalid item | ||
with pytest.raises(ValidationError): | ||
await create_item(txn_client, feature_collection) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.