From 0d18faa0d9fe7aa82d2c6bdf18ae4760e37965db Mon Sep 17 00:00:00 2001 From: Eric Hare Date: Tue, 30 Jan 2024 18:06:57 -0700 Subject: [PATCH] Fix #179 : alias for upsert calls --- astrapy/db.py | 20 ++++++++++++++++++-- tests/astrapy/test_async_db_dml.py | 14 +++++++------- tests/astrapy/test_db_dml.py | 14 +++++++------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/astrapy/db.py b/astrapy/db.py index eb003069..f2a7614d 100644 --- a/astrapy/db.py +++ b/astrapy/db.py @@ -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. @@ -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 @@ -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. @@ -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( diff --git a/tests/astrapy/test_async_db_dml.py b/tests/astrapy/test_async_db_dml.py index 23a5b702..0280095a 100644 --- a/tests/astrapy/test_async_db_dml.py +++ b/tests/astrapy/test_async_db_dml.py @@ -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()) @@ -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}) @@ -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}) @@ -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 @@ -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 = { @@ -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)") diff --git a/tests/astrapy/test_db_dml.py b/tests/astrapy/test_db_dml.py index c2719b39..18c21e5b 100644 --- a/tests/astrapy/test_db_dml.py +++ b/tests/astrapy/test_db_dml.py @@ -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()) @@ -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}) @@ -732,7 +732,7 @@ 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}) @@ -740,7 +740,7 @@ def test_upsert_document(writable_v_collection: AstraDBCollection) -> 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()) @@ -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 @@ -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 = { @@ -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")