Skip to content

Commit

Permalink
fix: populate transaction attributes after commit (#977)
Browse files Browse the repository at this point in the history
* fix: populate transaction attributes after commit

* fixed lint

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
daniel-sanche and gcf-owl-bot[bot] authored Oct 25, 2024
1 parent 3fe7271 commit aa3c0a3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
4 changes: 3 additions & 1 deletion google/cloud/firestore_v1/async_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion google/cloud/firestore_v1/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/v1/test_async_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,17 @@ 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.
Expand All @@ -221,6 +228,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(
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/v1/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -209,12 +211,19 @@ 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.
Expand All @@ -234,6 +243,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(
Expand Down

0 comments on commit aa3c0a3

Please sign in to comment.