From b96d08f480f786e8c1f677072f89b13735260a04 Mon Sep 17 00:00:00 2001 From: Stefano Lottini Date: Thu, 29 Feb 2024 02:40:01 +0100 Subject: [PATCH] rename to delete_one_by_predicate and add specific tests --- astrapy/db.py | 4 ++-- astrapy/idiomatic/collection.py | 6 ++++-- tests/astrapy/test_async_db_dml.py | 22 ++++++++++++++++++++++ tests/astrapy/test_db_dml.py | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/astrapy/db.py b/astrapy/db.py index ca18a233..bd67f7f0 100644 --- a/astrapy/db.py +++ b/astrapy/db.py @@ -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: @@ -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: diff --git a/astrapy/idiomatic/collection.py b/astrapy/idiomatic/collection.py index 14bef0fe..6c7d403d 100644 --- a/astrapy/idiomatic/collection.py +++ b/astrapy/idiomatic/collection.py @@ -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: @@ -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: diff --git a/tests/astrapy/test_async_db_dml.py b/tests/astrapy/test_async_db_dml.py index 803aa7f1..7f459b52 100644 --- a/tests/astrapy/test_async_db_dml.py +++ b/tests/astrapy/test_async_db_dml.py @@ -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, diff --git a/tests/astrapy/test_db_dml.py b/tests/astrapy/test_db_dml.py index 174a0ee2..89d638aa 100644 --- a/tests/astrapy/test_db_dml.py +++ b/tests/astrapy/test_db_dml.py @@ -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(