def test_it(self): from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore_v1.types import entity as 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, [i._pb for i in keys]))
def _make_entity(kind, id_, project): from google.cloud.datastore_v1.types import entity as entity_pb2 key = entity_pb2.Key() key.partition_id.project_id = project elem = key.path._pb.add() elem.kind = kind elem.id = id_ return entity_pb2.Entity(key=key)
def _make_mutation(id_): from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore_v1.types import entity as entity_pb2 key = entity_pb2.Key() key.partition_id.project_id = "PROJECT" elem = key._pb.path.add() elem.kind = "Kind" elem.id = id_ return datastore_pb2.MutationResult(key=key)
def to_protobuf(self): from google.cloud.datastore_v1.types import entity as entity_pb2 key = self._key = entity_pb2.Key() # Don't assign it, because it will just get ripped out # key.partition_id.project_id = self.project element = key._pb.path.add() element.kind = self._kind if self._id is not None: element.id = self._id return key
def _makePB(self, project=None, namespace=None, path=()): from google.cloud.datastore_v1.types import entity as entity_pb2 pb = entity_pb2.Key() if project is not None: pb.partition_id.project_id = project if namespace is not None: pb.partition_id.namespace_id = namespace for elem in path: added = pb._pb.path.add() added.kind = elem["kind"] if "id" in elem: added.id = elem["id"] if "name" in elem: added.name = elem["name"] return pb
def to_protobuf(self): """Return a protobuf corresponding to the key. :rtype: :class:`.entity_pb2.Key` :returns: The protobuf representing the key. """ key = _entity_pb2.Key() key.partition_id.project_id = self.project if self.namespace: key.partition_id.namespace_id = self.namespace for item in self.path: element = key.PathElement() if "kind" in item: element.kind = item["kind"] if "id" in item: element.id = item["id"] if "name" in item: element.name = item["name"] key.path.append(element) return key