Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: populate transaction attributes after commit #977

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading