From e57cebcd1a0e1fcc82d396db61f95e07e430879b Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Tue, 22 Oct 2024 15:41:12 -0700 Subject: [PATCH 1/3] fix: populate transaction attributes after commit --- google/cloud/firestore_v1/async_transaction.py | 4 +++- google/cloud/firestore_v1/transaction.py | 4 +++- tests/unit/v1/test_async_transaction.py | 10 +++++++++- tests/unit/v1/test_transaction.py | 12 +++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/google/cloud/firestore_v1/async_transaction.py b/google/cloud/firestore_v1/async_transaction.py index 559bea96f..8691e508d 100644 --- a/google/cloud/firestore_v1/async_transaction.py +++ b/google/cloud/firestore_v1/async_transaction.py @@ -145,7 +145,9 @@ async def _commit(self) -> list: ) self._clean_up() - return list(commit_response.write_results) + self.write_results = list(commit_response.write_results) + self.commit_time = commit_response.commit_time + return self.write_results async def get_all( self, diff --git a/google/cloud/firestore_v1/transaction.py b/google/cloud/firestore_v1/transaction.py index a01c1ed53..1820cd1a4 100644 --- a/google/cloud/firestore_v1/transaction.py +++ b/google/cloud/firestore_v1/transaction.py @@ -145,7 +145,9 @@ def _commit(self) -> list: ) self._clean_up() - return list(commit_response.write_results) + self.write_results = list(commit_response.write_results) + self.commit_time = commit_response.commit_time + return self.write_results def get_all( self, diff --git a/tests/unit/v1/test_async_transaction.py b/tests/unit/v1/test_async_transaction.py index 766c0637e..8b8876a5e 100644 --- a/tests/unit/v1/test_async_transaction.py +++ b/tests/unit/v1/test_async_transaction.py @@ -198,10 +198,15 @@ async def test_asynctransaction__rollback_failure(): @pytest.mark.asyncio async def test_asynctransaction__commit(): from google.cloud.firestore_v1.types import firestore, write + from google.protobuf.timestamp_pb2 import Timestamp + import datetime # Create a minimal fake GAPIC with a dummy result. firestore_api = AsyncMock() - commit_response = firestore.CommitResponse(write_results=[write.WriteResult()]) + commit_time = Timestamp() + commit_time.FromDatetime(datetime.datetime.now()) + results = [write.WriteResult(update_time=commit_time)] + commit_response = firestore.CommitResponse(write_results=results, commit_time=commit_time) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. @@ -221,6 +226,9 @@ async def test_asynctransaction__commit(): # Make sure transaction has no more "changes". assert transaction._id is None assert transaction._write_pbs == [] + # ensure write_results and commit_time were set + assert transaction.write_results == results + assert transaction.commit_time.timestamp_pb() == commit_time # Verify the mocks. firestore_api.commit.assert_called_once_with( diff --git a/tests/unit/v1/test_transaction.py b/tests/unit/v1/test_transaction.py index b5beef6c2..743d9e4ca 100644 --- a/tests/unit/v1/test_transaction.py +++ b/tests/unit/v1/test_transaction.py @@ -33,6 +33,8 @@ def test_transaction_constructor_defaults(): assert transaction._max_attempts == MAX_ATTEMPTS assert not transaction._read_only assert transaction._id is None + assert transaction.write_results is None + assert transaction.commit_time is None def test_transaction_constructor_explicit(): @@ -209,12 +211,17 @@ def test_transaction__rollback_failure(database): def test_transaction__commit(database): from google.cloud.firestore_v1.services.firestore import client as firestore_client from google.cloud.firestore_v1.types import firestore, write + from google.protobuf.timestamp_pb2 import Timestamp + import datetime # Create a minimal fake GAPIC with a dummy result. firestore_api = mock.create_autospec( firestore_client.FirestoreClient, instance=True ) - commit_response = firestore.CommitResponse(write_results=[write.WriteResult()]) + commit_time = Timestamp() + commit_time.FromDatetime(datetime.datetime.now()) + results = [write.WriteResult(update_time=commit_time)] + commit_response = firestore.CommitResponse(write_results=results, commit_time=commit_time) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. @@ -234,6 +241,9 @@ def test_transaction__commit(database): # Make sure transaction has no more "changes". assert transaction._id is None assert transaction._write_pbs == [] + # ensure write_results and commit_time were set + assert transaction.write_results == results + assert transaction.commit_time.timestamp_pb() == commit_time # Verify the mocks. firestore_api.commit.assert_called_once_with( From 17ef7d9fa5adf89af7f42faaa9b9bb67473e9f55 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Tue, 22 Oct 2024 15:44:49 -0700 Subject: [PATCH 2/3] fixed lint --- tests/unit/v1/test_async_transaction.py | 4 +++- tests/unit/v1/test_transaction.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/unit/v1/test_async_transaction.py b/tests/unit/v1/test_async_transaction.py index 8b8876a5e..253650ba1 100644 --- a/tests/unit/v1/test_async_transaction.py +++ b/tests/unit/v1/test_async_transaction.py @@ -206,7 +206,9 @@ async def test_asynctransaction__commit(): commit_time = Timestamp() commit_time.FromDatetime(datetime.datetime.now()) results = [write.WriteResult(update_time=commit_time)] - commit_response = firestore.CommitResponse(write_results=results, commit_time=commit_time) + commit_response = firestore.CommitResponse( + write_results=results, commit_time=commit_time + ) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. diff --git a/tests/unit/v1/test_transaction.py b/tests/unit/v1/test_transaction.py index 743d9e4ca..941e294db 100644 --- a/tests/unit/v1/test_transaction.py +++ b/tests/unit/v1/test_transaction.py @@ -221,7 +221,9 @@ def test_transaction__commit(database): commit_time = Timestamp() commit_time.FromDatetime(datetime.datetime.now()) results = [write.WriteResult(update_time=commit_time)] - commit_response = firestore.CommitResponse(write_results=results, commit_time=commit_time) + commit_response = firestore.CommitResponse( + write_results=results, commit_time=commit_time + ) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. From 0c946f11c390a097e2dbbf3a04d049ad92ae7c5b Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Tue, 22 Oct 2024 22:45:19 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- tests/unit/v1/test_async_transaction.py | 4 +++- tests/unit/v1/test_transaction.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/unit/v1/test_async_transaction.py b/tests/unit/v1/test_async_transaction.py index 8b8876a5e..253650ba1 100644 --- a/tests/unit/v1/test_async_transaction.py +++ b/tests/unit/v1/test_async_transaction.py @@ -206,7 +206,9 @@ async def test_asynctransaction__commit(): commit_time = Timestamp() commit_time.FromDatetime(datetime.datetime.now()) results = [write.WriteResult(update_time=commit_time)] - commit_response = firestore.CommitResponse(write_results=results, commit_time=commit_time) + commit_response = firestore.CommitResponse( + write_results=results, commit_time=commit_time + ) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client. diff --git a/tests/unit/v1/test_transaction.py b/tests/unit/v1/test_transaction.py index 743d9e4ca..941e294db 100644 --- a/tests/unit/v1/test_transaction.py +++ b/tests/unit/v1/test_transaction.py @@ -221,7 +221,9 @@ def test_transaction__commit(database): commit_time = Timestamp() commit_time.FromDatetime(datetime.datetime.now()) results = [write.WriteResult(update_time=commit_time)] - commit_response = firestore.CommitResponse(write_results=results, commit_time=commit_time) + commit_response = firestore.CommitResponse( + write_results=results, commit_time=commit_time + ) firestore_api.commit.return_value = commit_response # Attach the fake GAPIC to a real client.