def test_from_key(self):
   key = Key(dataset=Dataset('test-dataset')).kind('TestKind').id(1234)
   entity = Entity.from_key(key)
   self.assertEqual('test-dataset', entity.dataset().id())
   self.assertEqual('TestKind', entity.key().kind())
   self.assertEqual(entity.key().kind(), entity.kind())
   self.assertEqual(1234, entity.key().id())
Exemple #2
0
  def get_entities(self, keys):
    # This import is here to avoid circular references.
    from gclouddatastore.entity import Entity

    entity_pbs = self.connection().lookup(dataset_id=self.id(),
        key_pbs=[k.to_protobuf() for k in keys])

    entities = []
    for entity_pb in entity_pbs:
      entities.append(Entity.from_protobuf(entity_pb, dataset=self))
    return entities
Exemple #3
0
  def fetch(self, limit=None, connection=None):
    clone = self

    if limit:
      clone = self.limit(limit)

    connection = connection or clone._connection
    if not connection:
      raise ValueError

    response = connection._run_query(clone._pb, namespace=clone.namespace())
    return [Entity.from_protobuf(e.entity) for e in response.batch.entity_result]
  def get_entities(self, keys):
    lookup_request = datastore_pb.LookupRequest()

    for key in keys:
      if not key.dataset_id():
        key.dataset_id(self._dataset_id)
      lookup_request.key.add().CopyFrom(key.to_protobuf())

    lookup_response = self._rpc(
        'lookup', lookup_request, datastore_pb.LookupResponse)

    return [Entity.from_protobuf(result.entity)
            for result in lookup_response.found]
Exemple #5
0
  def fetch(self, limit=None):
    """Executes the Query and returns all matching entities.

    This makes an API call to the Cloud Datastore,
    sends the Query as a protobuf,
    parses the responses to Entity protobufs,
    and then converts them to :class:`gclouddatastore.entity.Entity` objects.

    For example::

      >>> import gclouddatastore
      >>> dataset = gclouddatastore.get_dataset('dataset-id', email, key_path)
      >>> query = dataset.query('Person').filter('name =', 'Sally')
      >>> query.fetch()
      [<Entity object>, <Entity object>, ...]
      >>> query.fetch(1)
      [<Entity object>]
      >>> query.limit()
      None

    :type limit: integer
    :param limit: An optional limit to apply temporarily to this query.
                  That is, the Query itself won't be altered,
                  but the limit will be applied to the query
                  before it is executed.

    :rtype: list of :class:`gclouddatastore.entity.Entity`'s
    :returns: The list of entities matching this query's criteria.
    """
    clone = self

    if limit:
      clone = self.limit(limit)

    entity_pbs = self.dataset().connection().run_query(
        query_pb=clone.to_protobuf(), dataset_id=self.dataset().id())

    return [Entity.from_protobuf(entity) for entity in entity_pbs]
 def test_init_sets_proper_values(self):
   dataset = Dataset(id='test-dataset')
   entity = Entity(dataset, 'TestKind')
   self.assertEqual('test-dataset', entity.dataset().id())
   self.assertEqual('TestKind', entity.kind())
 def test_key(self):
   dataset = Dataset(id='test-dataset')
   entity = Entity(dataset, 'TestKind')
   self.assertIsInstance(entity.key(), Key)