def test_it(self): from google.cloud.grpc.datastore.v1 import datastore_pb2 from google.cloud.grpc.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): import mock from google.cloud.grpc.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' conn = self._make_one() URI = '/'.join([ conn.api_base_url, conn.API_VERSION, 'projects', PROJECT + ':commit', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) # Set up mock for parsing the response. expected_result = object() _parsed = [] def mock_parse(response): _parsed.append(response) return expected_result patch = mock.patch( 'google.cloud.datastore._http._parse_commit_response', new=mock_parse) with patch: result = conn.commit(PROJECT, req_pb, b'xact') self.assertIs(result, expected_result) cw = http._called_with self._verifyProtobufCall(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) self.assertEqual(_parsed, [rsp_pb])