def test_commit_w_transaction(self): from google.cloud.proto.datastore.v1 import datastore_pb2 from google.cloud.datastore.helpers import _new_value_pb project = 'PROJECT' key_pb = self._make_key_pb(project) rsp_pb = datastore_pb2.CommitResponse() req_pb = datastore_pb2.CommitRequest() mutation = req_pb.mutations.add() insert = mutation.upsert insert.key.CopyFrom(key_pb) value_pb = _new_value_pb(insert, 'foo') value_pb.string_value = u'Foo' http = Http({'status': '200'}, rsp_pb.SerializeToString()) client = mock.Mock(_http=http, spec=['_http']) conn = self._make_one(client) uri = '/'.join([ conn.api_base_url, conn.API_VERSION, 'projects', project + ':commit', ]) result = conn.commit(project, req_pb, b'xact') self.assertEqual(result, rsp_pb) # Verify the caller. cw = http._called_with self._verify_protobuf_call(cw, uri, conn) rq_class = datastore_pb2.CommitRequest request = rq_class() request.ParseFromString(cw['body']) self.assertEqual(request.transaction, b'xact') self.assertEqual(list(request.mutations), [mutation]) self.assertEqual(request.mode, rq_class.TRANSACTIONAL)
def test_it(self): from google.cloud.proto.datastore.v1 import datastore_pb2 from google.cloud.proto.datastore.v1 import entity_pb2 index_updates = 1337 keys = [ entity_pb2.Key( path=[ entity_pb2.Key.PathElement( kind='Foo', id=1234, ), ], ), entity_pb2.Key( path=[ entity_pb2.Key.PathElement( kind='Bar', name='baz', ), ], ), ] response = datastore_pb2.CommitResponse( mutation_results=[ datastore_pb2.MutationResult(key=key) for key in keys ], index_updates=index_updates, ) result = self._call_fut(response) self.assertEqual(result, (index_updates, keys))
def test_commit_w_transaction(self): from google.cloud.proto.datastore.v1 import datastore_pb2 from google.cloud.datastore.helpers import _new_value_pb project = 'PROJECT' key_pb = _make_key_pb(project) rsp_pb = datastore_pb2.CommitResponse() req_pb = datastore_pb2.CommitRequest() mutation = req_pb.mutations.add() insert = mutation.upsert insert.key.CopyFrom(key_pb) value_pb = _new_value_pb(insert, 'foo') value_pb.string_value = u'Foo' # Create mock HTTP and client with response. http = _make_requests_session( [_make_response(content=rsp_pb.SerializeToString())]) client = mock.Mock(_http=http, _base_url='test.invalid', spec=['_http', '_base_url']) # Make request. rq_class = datastore_pb2.CommitRequest ds_api = self._make_one(client) mode = rq_class.TRANSACTIONAL result = ds_api.commit(project, mode, [mutation], transaction=b'xact') # Check the result and verify the callers. self.assertEqual(result, rsp_pb) uri = _build_expected_url(client._base_url, project, 'commit') request = _verify_protobuf_call(http, uri, rq_class()) self.assertEqual(request.transaction, b'xact') self.assertEqual(list(request.mutations), [mutation]) self.assertEqual(request.mode, rq_class.TRANSACTIONAL)
def test_commit(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = datastore_client.DatastoreClient() # Mock request project_id = 'projectId-1969970175' mode = enums.CommitRequest.Mode.MODE_UNSPECIFIED mutations = [] # Mock response index_updates = -1425228195 expected_response = datastore_pb2.CommitResponse( index_updates=index_updates) grpc_stub.Commit.return_value = expected_response response = client.commit(project_id, mode, mutations) self.assertEqual(expected_response, response) grpc_stub.Commit.assert_called_once() args, kwargs = grpc_stub.Commit.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = datastore_pb2.CommitRequest(project_id=project_id, mode=mode, mutations=mutations) self.assertEqual(expected_request, actual_request)
def __init__(self, *new_key_ids): from google.cloud.proto.datastore.v1 import datastore_pb2 self._committed = [] mutation_results = [ _make_mutation(key_id) for key_id in new_key_ids] self._commit_response_pb = datastore_pb2.CommitResponse( mutation_results=mutation_results)
def __init__(self, xact_id=123, keys=()): from google.cloud.proto.datastore.v1 import datastore_pb2 self._xact_id = xact_id mutation_results = [ datastore_pb2.MutationResult(key=key) for key in keys ] self._commit_response_pb = datastore_pb2.CommitResponse( mutation_results=mutation_results)
def commit(self, project, commit_request, transaction_id): from google.cloud.proto.datastore.v1 import datastore_pb2 self._commit_cw.append((project, commit_request, transaction_id)) keys, self._commit = self._commit[0], self._commit[1:] mutation_results = [ datastore_pb2.MutationResult(key=key) for key in keys ] return datastore_pb2.CommitResponse(mutation_results=mutation_results)
def _make_commit_response(*new_key_ids): from google.cloud.proto.datastore.v1 import datastore_pb2 mutation_results = [_make_mutation(key_id) for key_id in new_key_ids] return datastore_pb2.CommitResponse(mutation_results=mutation_results)
def _make_commit_response(*keys): from google.cloud.proto.datastore.v1 import datastore_pb2 mutation_results = [datastore_pb2.MutationResult(key=key) for key in keys] return datastore_pb2.CommitResponse(mutation_results=mutation_results)