Skip to content

Commit

Permalink
rename to delete_one_by_predicate and add specific tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hemidactylus committed Feb 29, 2024
1 parent 7ec1a7e commit b96d08f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
4 changes: 2 additions & 2 deletions astrapy/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ def delete_one(self, id: str) -> API_RESPONSE:

return response

def delete_one_filter(self, filter: Dict[str, Any]) -> API_RESPONSE:
def delete_one_by_predicate(self, filter: Dict[str, Any]) -> API_RESPONSE:
"""
Delete a single document from the collection based on a filter clause
Args:
Expand Down Expand Up @@ -1929,7 +1929,7 @@ async def delete_one(self, id: str) -> API_RESPONSE:

return response

async def delete_one_filter(self, filter: Dict[str, Any]) -> API_RESPONSE:
async def delete_one_by_predicate(self, filter: Dict[str, Any]) -> API_RESPONSE:
"""
Delete a single document from the collection based on a filter clause
Args:
Expand Down
6 changes: 4 additions & 2 deletions astrapy/idiomatic/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def delete_one(
method_name="delete_one",
parameter_name="let",
)
do_response = self._astra_db_collection.delete_one_filter(filter=filter)
do_response = self._astra_db_collection.delete_one_by_predicate(filter=filter)
if "deletedCount" in do_response.get("status", {}):
deleted_count = do_response["status"]["deletedCount"]
if deleted_count == -1:
Expand Down Expand Up @@ -409,7 +409,9 @@ async def delete_one(
method_name="delete_one",
parameter_name="let",
)
do_response = await self._astra_db_collection.delete_one_filter(filter=filter)
do_response = await self._astra_db_collection.delete_one_by_predicate(
filter=filter
)
if "deletedCount" in do_response.get("status", {}):
deleted_count = do_response["status"]["deletedCount"]
if deleted_count == -1:
Expand Down
22 changes: 22 additions & 0 deletions tests/astrapy/test_async_db_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,28 @@ async def test_delete_one_novector(
assert delete_response_no["status"]["deletedCount"] == 0


@pytest.mark.describe("delete_one_by_predicate, not through vector (async)")
async def test_delete_one_by_predicate_novector(
async_empty_v_collection: AsyncAstraDBCollection,
) -> None:
await async_empty_v_collection.insert_one({"k": "v1"})
await async_empty_v_collection.insert_one({"k": "v1"})
await async_empty_v_collection.insert_one({"k": "v1"})
await async_empty_v_collection.insert_one({"k": "v2"})
assert (await async_empty_v_collection.count_documents())["status"]["count"] == 4
assert (await async_empty_v_collection.count_documents({"k": "v1"}))["status"][
"count"
] == 3

await async_empty_v_collection.delete_one_by_predicate({"k": "v1"})
await async_empty_v_collection.delete_one_by_predicate({"k": "zz"})

assert (await async_empty_v_collection.count_documents())["status"]["count"] == 3
assert (await async_empty_v_collection.count_documents({"k": "v1"}))["status"][
"count"
] == 2


@pytest.mark.describe("delete_many, not through vector (async)")
async def test_delete_many_novector(
async_disposable_v_collection: AsyncAstraDBCollection,
Expand Down
18 changes: 18 additions & 0 deletions tests/astrapy/test_db_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,24 @@ def test_delete_one_novector(disposable_v_collection: AstraDBCollection) -> None
assert delete_response_no["status"]["deletedCount"] == 0


@pytest.mark.describe("delete_one_by_predicate, not through vector")
def test_delete_one_by_predicate_novector(
empty_v_collection: AstraDBCollection,
) -> None:
empty_v_collection.insert_one({"k": "v1"})
empty_v_collection.insert_one({"k": "v1"})
empty_v_collection.insert_one({"k": "v1"})
empty_v_collection.insert_one({"k": "v2"})
assert empty_v_collection.count_documents()["status"]["count"] == 4
assert empty_v_collection.count_documents({"k": "v1"})["status"]["count"] == 3

empty_v_collection.delete_one_by_predicate({"k": "v1"})
empty_v_collection.delete_one_by_predicate({"k": "zz"})

assert empty_v_collection.count_documents()["status"]["count"] == 3
assert empty_v_collection.count_documents({"k": "v1"})["status"]["count"] == 2


@pytest.mark.describe("delete_many, not through vector")
def test_delete_many_novector(disposable_v_collection: AstraDBCollection) -> None:
delete_response = disposable_v_collection.delete_many(
Expand Down

0 comments on commit b96d08f

Please sign in to comment.