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

Making datastore Connection.begin_transaction() return low-level protobuf. #3091

Merged
merged 1 commit into from
Mar 3, 2017
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
5 changes: 2 additions & 3 deletions datastore/google/cloud/datastore/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,11 @@ def begin_transaction(self, project):
:type project: str
:param project: The project to which the transaction applies.

:rtype: bytes
:rtype: :class:`.datastore_pb2.BeginTransactionResponse`
:returns: The serialized transaction that was begun.
"""
request = _datastore_pb2.BeginTransactionRequest()
response = self._datastore_api.begin_transaction(project, request)
return response.transaction
return self._datastore_api.begin_transaction(project, request)

def commit(self, project, request, transaction_id):
"""Commit mutations in context of current transaction (if any).
Expand Down
3 changes: 2 additions & 1 deletion datastore/google/cloud/datastore/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ def begin(self):
"""
super(Transaction, self).begin()
try:
self._id = self._client._connection.begin_transaction(
response_pb = self._client._connection.begin_transaction(
self.project)
self._id = response_pb.transaction
except: # noqa: E722 do not use bare except, specify exception instead
self._status = self._ABORTED
raise
Expand Down
26 changes: 17 additions & 9 deletions datastore/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,25 +672,33 @@ def test_run_query_w_namespace_nonempty_result(self):
def test_begin_transaction(self):
from google.cloud.proto.datastore.v1 import datastore_pb2

PROJECT = 'PROJECT'
TRANSACTION = b'TRANSACTION'
project = 'PROJECT'
transaction = b'TRANSACTION'
rsp_pb = datastore_pb2.BeginTransactionResponse()
rsp_pb.transaction = TRANSACTION
rsp_pb.transaction = transaction

# Create mock HTTP and client with response.
http = Http({'status': '200'}, rsp_pb.SerializeToString())
client = mock.Mock(_http=http, spec=['_http'])

# Make request.
conn = self._make_one(client)
URI = '/'.join([
response = conn.begin_transaction(project)

# Check the result and verify the callers.
self.assertEqual(response, rsp_pb)
uri = '/'.join([
conn.api_base_url,
conn.API_VERSION,
'projects',
PROJECT + ':beginTransaction',
project + ':beginTransaction',
])
self.assertEqual(conn.begin_transaction(PROJECT), TRANSACTION)
cw = http._called_with
self._verify_protobuf_call(cw, URI, conn)
rq_class = datastore_pb2.BeginTransactionRequest
request = rq_class()
self._verify_protobuf_call(cw, uri, conn)
request = datastore_pb2.BeginTransactionRequest()
request.ParseFromString(cw['body'])
# The RPC-over-HTTP request does not set the project in the request.
self.assertEqual(request.project_id, u'')

def test_commit_wo_transaction(self):
from google.cloud.proto.datastore.v1 import datastore_pb2
Expand Down
5 changes: 4 additions & 1 deletion datastore/unit_tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,12 @@ def __init__(self, xact_id=123, keys=()):
mutation_results=mutation_results)

def begin_transaction(self, project):
import mock

This comment was marked as spam.

This comment was marked as spam.


self._begun = project
if self._side_effect is None:
return self._xact_id
return mock.Mock(
transaction=self._xact_id, spec=['transaction'])
else:
raise self._side_effect

Expand Down