def test_it(self): from gcloud.datastore._generated import datastore_pb2 from gcloud.datastore._generated 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._callFUT(response) self.assertEqual(result, (index_updates, keys))
def test_prepare_project_valid(self): from gcloud.datastore._generated import entity_pb2 key = entity_pb2.Key() key.partition_id.dataset_id = 'foo' new_key = self._callFUT(key) self.assertFalse(new_key is key) key_without = entity_pb2.Key() new_key.ClearField('partition_id') self.assertEqual(new_key, key_without)
def _prepare_key_for_request(key_pb): """Add protobuf keys to a request object. :type key_pb: :class:`gcloud.datastore._generated.entity_pb2.Key` :param key_pb: A key to be added to a request. :rtype: :class:`gcloud.datastore._generated.entity_pb2.Key` :returns: A key which will be added to a request. It will be the original if nothing needs to be changed. """ if _has_field(key_pb.partition_id, 'dataset_id'): # We remove the dataset_id from the protobuf. This is because # the backend fails a request if the key contains un-prefixed # project. The backend fails because requests to # /datastore/.../datasets/foo/... # and # /datastore/.../datasets/s~foo/... # both go to the datastore given by 's~foo'. So if the key # protobuf in the request body has dataset_id='foo', the # backend will reject since 'foo' != 's~foo'. new_key_pb = _entity_pb2.Key() new_key_pb.CopyFrom(key_pb) new_key_pb.partition_id.ClearField('dataset_id') key_pb = new_key_pb return key_pb
def _make_key(kind, id_, project): from gcloud.datastore._generated import entity_pb2 key = entity_pb2.Key() key.partition_id.project_id = project elem = key.path.add() elem.kind = kind elem.id = id_ return key
def _make_key(kind, id, dataset_id): from gcloud.datastore._generated import entity_pb2 key = entity_pb2.Key() key.partition_id.dataset_id = dataset_id elem = key.path_element.add() elem.kind = kind elem.id = id return key
def to_protobuf(self): from gcloud.datastore._generated import 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.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 gcloud.datastore._generated import entity_pb2 pb = entity_pb2.Key() if project is not None: pb.partition_id.dataset_id = project if namespace is not None: pb.partition_id.namespace = namespace for elem in path: added = pb.path_element.add() added.kind = elem['kind'] if 'id' in elem: added.id = elem['id'] if 'name' in elem: added.name = elem['name'] return pb
def _prepare_key_for_request(key_pb): # pragma: NO COVER copied from helpers """Add protobuf keys to a request object. .. note:: This is copied from `helpers` to avoid a cycle: _implicit_environ -> connection -> helpers -> key -> _implicit_environ :type key_pb: :class:`gcloud.datastore._generated.entity_pb2.Key` :param key_pb: A key to be added to a request. :rtype: :class:`gcloud.datastore._generated.entity_pb2.Key` :returns: A key which will be added to a request. It will be the original if nothing needs to be changed. """ if key_pb.partition_id.HasField('dataset_id'): new_key_pb = _entity_pb2.Key() new_key_pb.CopyFrom(key_pb) new_key_pb.partition_id.ClearField('dataset_id') key_pb = new_key_pb return key_pb
def to_protobuf(self): """Return a protobuf corresponding to the key. :rtype: :class:`gcloud.datastore._generated.entity_pb2.Key` :returns: The protobuf representing the key. """ key = _entity_pb2.Key() key.partition_id.dataset_id = self.dataset_id if self.namespace: key.partition_id.namespace = self.namespace for item in self.path: element = key.path_element.add() if 'kind' in item: element.kind = item['kind'] if 'id' in item: element.id = item['id'] if 'name' in item: element.name = item['name'] return key
def test_prepare_project_unset(self): from gcloud.datastore._generated import entity_pb2 key = entity_pb2.Key() new_key = self._callFUT(key) self.assertTrue(new_key is key)