Skip to content

Commit

Permalink
Fix #179 : alias for upsert calls
Browse files Browse the repository at this point in the history
  • Loading branch information
erichare committed Jan 31, 2024
1 parent fa12397 commit 0d18faa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
20 changes: 18 additions & 2 deletions astrapy/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,14 @@ def delete_subdocument(self, id: str, subdoc: str) -> API_RESPONSE:
return response

def upsert(self, document: API_DOC) -> str:
# Deprecated: call the upsert_one method
DEPRECATION_MESSAGE = "Method 'upsert' of AstraDBCollection is deprecated. Please switch to method 'upsert_one'."

warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)

return self.upsert_one(document)

def upsert_one(self, document: API_DOC) -> str:
"""
Emulate an upsert operation for a single document in the collection.
Expand Down Expand Up @@ -948,7 +956,7 @@ def upsert_many(
if concurrency == 1:
for document in documents:
try:
results.append(self.upsert(document))
results.append(self.upsert_one(document))
except Exception as e:
results.append(e)
return results
Expand Down Expand Up @@ -1753,6 +1761,14 @@ async def delete_subdocument(self, id: str, subdoc: str) -> API_RESPONSE:
return response

async def upsert(self, document: API_DOC) -> str:
# Deprecated: call the upsert_one method
DEPRECATION_MESSAGE = "Method 'upsert' of AstraDBCollection is deprecated. Please switch to method 'upsert_one'."

warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)

return await self.upsert_one(document)

async def upsert_one(self, document: API_DOC) -> str:
"""
Emulate an upsert operation for a single document in the collection.
Expand Down Expand Up @@ -1813,7 +1829,7 @@ async def upsert_many(

async def concurrent_upsert(doc: API_DOC) -> str:
async with sem:
return await self.upsert(document=doc)
return await self.upsert_one(document=doc)

tasks = [asyncio.create_task(concurrent_upsert(doc)) for doc in documents]
results = await asyncio.gather(
Expand Down
14 changes: 7 additions & 7 deletions tests/astrapy/test_async_db_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,8 @@ async def test_upsert_many(
assert response1b["data"]["document"] == documents1[-1]


@pytest.mark.describe("upsert (async)")
async def test_upsert_document(
@pytest.mark.describe("upsert one (async)")
async def test_upsert_one_document(
async_writable_v_collection: AsyncAstraDBCollection,
) -> None:
_id = str(uuid.uuid4())
Expand All @@ -685,7 +685,7 @@ async def test_upsert_document(
},
},
}
upsert_result0 = await async_writable_v_collection.upsert(document0)
upsert_result0 = await async_writable_v_collection.upsert_one(document0)
assert upsert_result0 == _id

response0 = await async_writable_v_collection.find_one(filter={"_id": _id})
Expand All @@ -705,7 +705,7 @@ async def test_upsert_document(
"accounting",
],
}
upsert_result1 = await async_writable_v_collection.upsert(document1)
upsert_result1 = await async_writable_v_collection.upsert_one(document1)
assert upsert_result1 == _id

response1 = await async_writable_v_collection.find_one(filter={"_id": _id})
Expand All @@ -725,7 +725,7 @@ async def test_upsert_api_errors(
"nature": "good vector",
"$vector": [10, 11],
}
upsert_result0 = await async_writable_v_collection.upsert(document0a)
upsert_result0 = await async_writable_v_collection.upsert_one(document0a)
assert upsert_result0 == _id0

# triggering an API error for the already-exists path of the upsert
Expand All @@ -735,7 +735,7 @@ async def test_upsert_api_errors(
"$vector": [10, 11, 999, -153],
}
with pytest.raises(ValueError):
_ = await async_writable_v_collection.upsert(document0b)
_ = await async_writable_v_collection.upsert_one(document0b)

# triggering an API error for the already-exists path of the upsert
document1 = {
Expand All @@ -744,7 +744,7 @@ async def test_upsert_api_errors(
"$vector": [10, 11, 999, -153],
}
with pytest.raises(ValueError):
_ = await async_writable_v_collection.upsert(document1)
_ = await async_writable_v_collection.upsert_one(document1)


@pytest.mark.describe("update_one to create a subdocument, not through vector (async)")
Expand Down
14 changes: 7 additions & 7 deletions tests/astrapy/test_db_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def test_upsert_many(
assert response1b["data"]["document"] == documents1[-1]


@pytest.mark.describe("upsert")
@pytest.mark.describe("upsert_one")
def test_upsert_document(writable_v_collection: AstraDBCollection) -> None:
_id = str(uuid.uuid4())

Expand All @@ -712,7 +712,7 @@ def test_upsert_document(writable_v_collection: AstraDBCollection) -> None:
},
},
}
upsert_result0 = writable_v_collection.upsert(document0)
upsert_result0 = writable_v_collection.upsert_one(document0)
assert upsert_result0 == _id

response0 = writable_v_collection.find_one(filter={"_id": _id})
Expand All @@ -732,15 +732,15 @@ def test_upsert_document(writable_v_collection: AstraDBCollection) -> None:
"accounting",
],
}
upsert_result1 = writable_v_collection.upsert(document1)
upsert_result1 = writable_v_collection.upsert_one(document1)
assert upsert_result1 == _id

response1 = writable_v_collection.find_one(filter={"_id": _id})
assert response1 is not None
assert response1["data"]["document"] == document1


@pytest.mark.describe("upsert should catch general errors from API")
@pytest.mark.describe("upsert_one should catch general errors from API")
def test_upsert_api_errors(writable_v_collection: AstraDBCollection) -> None:
_id0 = str(uuid.uuid4())
_id1 = str(uuid.uuid4())
Expand All @@ -750,7 +750,7 @@ def test_upsert_api_errors(writable_v_collection: AstraDBCollection) -> None:
"nature": "good vector",
"$vector": [10, 11],
}
upsert_result0 = writable_v_collection.upsert(document0a)
upsert_result0 = writable_v_collection.upsert_one(document0a)
assert upsert_result0 == _id0

# triggering an API error for the already-exists path of the upsert
Expand All @@ -760,7 +760,7 @@ def test_upsert_api_errors(writable_v_collection: AstraDBCollection) -> None:
"$vector": [10, 11, 999, -153],
}
with pytest.raises(ValueError):
_ = writable_v_collection.upsert(document0b)
_ = writable_v_collection.upsert_one(document0b)

# triggering an API error for the already-exists path of the upsert
document1 = {
Expand All @@ -769,7 +769,7 @@ def test_upsert_api_errors(writable_v_collection: AstraDBCollection) -> None:
"$vector": [10, 11, 999, -153],
}
with pytest.raises(ValueError):
_ = writable_v_collection.upsert(document1)
_ = writable_v_collection.upsert_one(document1)


@pytest.mark.describe("update_one to create a subdocument, not through vector")
Expand Down

0 comments on commit 0d18faa

Please sign in to comment.