Skip to content

Commit

Permalink
Add tests to confirm that upsert method results in success when posti…
Browse files Browse the repository at this point in the history
…ng the same items twice
  • Loading branch information
Edward Keeble committed Nov 30, 2023
1 parent 933d700 commit 0f45da7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
4 changes: 3 additions & 1 deletion stac_fastapi/pgstac/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ async def bulk_item_insert(self, items: Items, request: Request, **kwargs) -> st

async with request.app.state.get_connection(request, "w") as conn:
if items.method == BulkTransactionMethod.INSERT:
method_verb = "added"
await dbfunc(conn, "create_items", items_to_insert)
elif items.method == BulkTransactionMethod.UPSERT:
method_verb = "upserted"
await dbfunc(conn, "upsert_items", items_to_insert)

return_msg = f"Successfully added {len(items_to_insert)} items."
return_msg = f"Successfully {method_verb} {len(items_to_insert)} items."
return return_msg
70 changes: 70 additions & 0 deletions tests/clients/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,76 @@ async def test_create_bulk_items(
assert resp.status_code == 200


async def test_create_bulk_items_already_exist_insert(
app_client, load_test_data: Callable, load_test_collection
):
coll = load_test_collection
item = load_test_data("test_item.json")

items = {}
for _ in range(2):
_item = deepcopy(item)
_item["id"] = str(uuid.uuid4())
items[_item["id"]] = _item

payload = {"items": items, "method": "insert"}

resp = await app_client.post(
f"/collections/{coll.id}/bulk_items",
json=payload,
)
assert resp.status_code == 200
assert resp.text == '"Successfully added 2 items."'

for item_id in items.keys():
resp = await app_client.get(f"/collections/{coll.id}/items/{item_id}")
assert resp.status_code == 200

# Try creating the same items again.
# This should fail with the default insert behavior.
resp = await app_client.post(
f"/collections/{coll.id}/bulk_items",
json=payload,
)
assert resp.status_code == 409


async def test_create_bulk_items_already_exist_upsert(
app_client, load_test_data: Callable, load_test_collection
):
coll = load_test_collection
item = load_test_data("test_item.json")

items = {}
for _ in range(2):
_item = deepcopy(item)
_item["id"] = str(uuid.uuid4())
items[_item["id"]] = _item

payload = {"items": items, "method": "insert"}

resp = await app_client.post(
f"/collections/{coll.id}/bulk_items",
json=payload,
)
assert resp.status_code == 200
assert resp.text == '"Successfully added 2 items."'

for item_id in items.keys():
resp = await app_client.get(f"/collections/{coll.id}/items/{item_id}")
assert resp.status_code == 200

# Try creating the same items again, but using upsert.
# This should succeed.
payload["method"] = "upsert"
resp = await app_client.post(
f"/collections/{coll.id}/bulk_items",
json=payload,
)
assert resp.status_code == 200
assert resp.text == '"Successfully upserted 2 items."'


# TODO since right now puts implement upsert
# test_create_collection_already_exists
# test create_item_already_exists
Expand Down

0 comments on commit 0f45da7

Please sign in to comment.