Skip to content

Commit

Permalink
fixes and test
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-cf committed Jul 22, 2024
1 parent f2b39d6 commit f74306d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 6 additions & 2 deletions stac_fastapi/mongo/database_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,9 @@ async def prep_create_item(
mongo_item = self.item_serializer.stac_to_db(item, base_url)

if not exist_ok:
existing_item = await items_collection.find_one({"id": mongo_item["id"]})
existing_item = await items_collection.find_one(
{"id": mongo_item["id"], "collection": mongo_item["collection"]}
)
if existing_item:
raise ConflictError(
f"Item {mongo_item['id']} in collection {mongo_item['collection']} already exists"
Expand Down Expand Up @@ -733,7 +735,9 @@ def sync_prep_create_item(
mongo_item = self.item_serializer.stac_to_db(item, base_url)

if not exist_ok:
existing_item = items_collection.find_one({"id": mongo_item["id"]})
existing_item = items_collection.find_one(
{"id": mongo_item["id"], "collection": mongo_item["collection"]}
)
if existing_item:
raise ConflictError(
f"Item {mongo_item['id']} in collection {mongo_item['collection']} already exists"
Expand Down
30 changes: 29 additions & 1 deletion stac_fastapi/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def rfc3339_str_to_datetime(s: str) -> datetime:


@pytest.mark.asyncio
async def test_create_and_delete_item(app_client, ctx, txn_client):
async def test_create_item_indices(app_client, ctx, txn_client):
"""Test creation and deletion of a single item (transactions extension)"""

test_item = ctx.item
Expand Down Expand Up @@ -890,3 +890,31 @@ async def test_search_datetime_validation_errors(app_client):
# resp = await app_client.get("/search?datetime={}".format(dt))
# assert resp.status_code == 400
# updated for same reason as sfeos


@pytest.mark.asyncio
async def test_create_same_item_in_different_collections(
app_client, ctx, load_test_data
):
"""Test creation of items and indices"""

test_item = load_test_data("test_item.json")
test_collection = load_test_data("test_collection.json")

# create item in collection where an item with same id already exists
resp = await app_client.post(
f"/collections/{test_collection['id']}/items", json=test_item
)
assert resp.status_code == 409, resp.json()

# prep second collection
test_collection["id"] = "test_collection2"
resp = await app_client.post("/collections", json=test_collection)
assert resp.status_code == 201, resp.json()

# create item with same id in second collection
test_item["collection"] = test_collection["id"]
resp = await app_client.post(
f"/collections/{test_collection['id']}/items", json=test_item
)
assert resp.status_code == 201, resp.json()

0 comments on commit f74306d

Please sign in to comment.