Skip to content

Commit fee3f2f

Browse files
authored
Merge pull request #3134 from dhermes/ds-move-begin_transaction-to-GAPIC
Using GAPIC datastore object (and an HTTP equivalent) for begin_transaction.
2 parents 44b4d50 + 3daaf06 commit fee3f2f

File tree

6 files changed

+91
-145
lines changed

6 files changed

+91
-145
lines changed

datastore/google/cloud/datastore/_gax.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,24 +152,6 @@ def run_query(self, project, request_pb):
152152
with _grpc_catch_rendezvous():
153153
return self._stub.RunQuery(request_pb)
154154

155-
def begin_transaction(self, project, request_pb):
156-
"""Perform a ``beginTransaction`` request.
157-
158-
:type project: str
159-
:param project: The project to connect to. This is
160-
usually your project name in the cloud console.
161-
162-
:type request_pb:
163-
:class:`.datastore_pb2.BeginTransactionRequest`
164-
:param request_pb: The request protobuf object.
165-
166-
:rtype: :class:`.datastore_pb2.BeginTransactionResponse`
167-
:returns: The returned protobuf response object.
168-
"""
169-
request_pb.project_id = project
170-
with _grpc_catch_rendezvous():
171-
return self._stub.BeginTransaction(request_pb)
172-
173155

174156
class GAPICDatastoreAPI(datastore_client.DatastoreClient):
175157
"""An API object that sends proto-over-gRPC requests.

datastore/google/cloud/datastore/_http.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,6 @@ def run_query(self, project, request_pb):
198198
self.connection.api_base_url,
199199
request_pb, _datastore_pb2.RunQueryResponse)
200200

201-
def begin_transaction(self, project, request_pb):
202-
"""Perform a ``beginTransaction`` request.
203-
204-
:type project: str
205-
:param project: The project to connect to. This is
206-
usually your project name in the cloud console.
207-
208-
:type request_pb:
209-
:class:`.datastore_pb2.BeginTransactionRequest`
210-
:param request_pb: The request protobuf object.
211-
212-
:rtype: :class:`.datastore_pb2.BeginTransactionResponse`
213-
:returns: The returned protobuf response object.
214-
"""
215-
return _rpc(self.connection.http, project, 'beginTransaction',
216-
self.connection.api_base_url,
217-
request_pb, _datastore_pb2.BeginTransactionResponse)
218-
219201

220202
class Connection(connection_module.Connection):
221203
"""A connection to the Google Cloud Datastore via the Protobuf API.
@@ -335,20 +317,6 @@ def run_query(self, project, query_pb, namespace=None,
335317
request.query.CopyFrom(query_pb)
336318
return self._datastore_api.run_query(project, request)
337319

338-
def begin_transaction(self, project):
339-
"""Begin a transaction.
340-
341-
Maps the ``DatastoreService.BeginTransaction`` protobuf RPC.
342-
343-
:type project: str
344-
:param project: The project to which the transaction applies.
345-
346-
:rtype: :class:`.datastore_pb2.BeginTransactionResponse`
347-
:returns: The serialized transaction that was begun.
348-
"""
349-
request = _datastore_pb2.BeginTransactionRequest()
350-
return self._datastore_api.begin_transaction(project, request)
351-
352320

353321
class HTTPDatastoreAPI(object):
354322
"""An API object that sends proto-over-HTTP requests.
@@ -362,6 +330,21 @@ class HTTPDatastoreAPI(object):
362330
def __init__(self, client):
363331
self.client = client
364332

333+
def begin_transaction(self, project):
334+
"""Perform a ``beginTransaction`` request.
335+
336+
:type project: str
337+
:param project: The project to connect to. This is
338+
usually your project name in the cloud console.
339+
340+
:rtype: :class:`.datastore_pb2.BeginTransactionResponse`
341+
:returns: The returned protobuf response object.
342+
"""
343+
request_pb = _datastore_pb2.BeginTransactionRequest()
344+
return _rpc(self.client._http, project, 'beginTransaction',
345+
self.client._base_url,
346+
request_pb, _datastore_pb2.BeginTransactionResponse)
347+
365348
def commit(self, project, mode, mutations, transaction=None):
366349
"""Perform a ``commit`` request.
367350

datastore/google/cloud/datastore/transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def begin(self):
184184
"""
185185
super(Transaction, self).begin()
186186
try:
187-
response_pb = self._client._connection.begin_transaction(
187+
response_pb = self._client._datastore_api.begin_transaction(
188188
self.project)
189189
self._id = response_pb.transaction
190190
except: # noqa: E722 do not use bare except, specify exception instead

datastore/unit_tests/test__gax.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,6 @@ def test_run_query_invalid_argument(self):
240240
exc = GrpcRendezvous(exc_state, None, None, None)
241241
self._run_query_failure_helper(exc, BadRequest)
242242

243-
def test_begin_transaction(self):
244-
return_val = object()
245-
stub = _GRPCStub(return_val)
246-
datastore_api, _ = self._make_one(stub=stub)
247-
248-
request_pb = mock.Mock(project_id=None, spec=['project_id'])
249-
project = 'PROJECT'
250-
result = datastore_api.begin_transaction(project, request_pb)
251-
self.assertIs(result, return_val)
252-
self.assertEqual(request_pb.project_id, project)
253-
self.assertEqual(
254-
stub.method_calls,
255-
[(request_pb, 'BeginTransaction')])
256-
257243

258244
@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
259245
class TestGAPICDatastoreAPI(unittest.TestCase):
@@ -338,6 +324,3 @@ def Lookup(self, request_pb):
338324

339325
def RunQuery(self, request_pb):
340326
return self._method(request_pb, 'RunQuery')
341-
342-
def BeginTransaction(self, request_pb):
343-
return self._method(request_pb, 'BeginTransaction')

datastore/unit_tests/test__http.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,23 @@ def test_run_query_w_namespace_nonempty_result(self):
634634
self.assertEqual(request.partition_id.namespace_id, namespace)
635635
self.assertEqual(request.query, q_pb)
636636

637+
638+
class TestHTTPDatastoreAPI(unittest.TestCase):
639+
640+
@staticmethod
641+
def _get_target_class():
642+
from google.cloud.datastore._http import HTTPDatastoreAPI
643+
644+
return HTTPDatastoreAPI
645+
646+
def _make_one(self, *args, **kwargs):
647+
return self._get_target_class()(*args, **kwargs)
648+
649+
def test_constructor(self):
650+
client = object()
651+
ds_api = self._make_one(client)
652+
self.assertIs(ds_api.client, client)
653+
637654
def test_begin_transaction(self):
638655
from google.cloud.proto.datastore.v1 import datastore_pb2
639656

@@ -648,37 +665,20 @@ def test_begin_transaction(self):
648665
_http=http, _base_url='test.invalid', spec=['_http', '_base_url'])
649666

650667
# Make request.
651-
conn = self._make_one(client)
652-
response = conn.begin_transaction(project)
668+
ds_api = self._make_one(client)
669+
response = ds_api.begin_transaction(project)
653670

654671
# Check the result and verify the callers.
655672
self.assertEqual(response, rsp_pb)
656673
uri = _build_expected_url(
657-
conn.api_base_url, project, 'beginTransaction')
674+
client._base_url, project, 'beginTransaction')
658675
cw = http._called_with
659676
_verify_protobuf_call(self, cw, uri)
660677
request = datastore_pb2.BeginTransactionRequest()
661678
request.ParseFromString(cw['body'])
662679
# The RPC-over-HTTP request does not set the project in the request.
663680
self.assertEqual(request.project_id, u'')
664681

665-
666-
class TestHTTPDatastoreAPI(unittest.TestCase):
667-
668-
@staticmethod
669-
def _get_target_class():
670-
from google.cloud.datastore._http import HTTPDatastoreAPI
671-
672-
return HTTPDatastoreAPI
673-
674-
def _make_one(self, *args, **kwargs):
675-
return self._get_target_class()(*args, **kwargs)
676-
677-
def test_constructor(self):
678-
client = object()
679-
ds_api = self._make_one(client)
680-
self.assertIs(ds_api.client, client)
681-
682682
def test_commit_wo_transaction(self):
683683
from google.cloud.proto.datastore.v1 import datastore_pb2
684684
from google.cloud.datastore.helpers import _new_value_pb

0 commit comments

Comments
 (0)