Esempio n. 1
0
def create_entities(count):
    """Creates a list of entities with random keys."""
    entities = []

    for _ in range(count):
        entity_result = query_pb2.EntityResult()
        entity_result.entity.key.path.add().name = str(uuid.uuid4())
        entities.append(entity_result)

    return entities
Esempio n. 2
0
def _make_query_response(entity_pbs, cursor_as_bytes, more_results_enum,
                         skipped_results):
    from google.cloud.proto.datastore.v1 import datastore_pb2
    from google.cloud.proto.datastore.v1 import query_pb2

    return datastore_pb2.RunQueryResponse(batch=query_pb2.QueryResultBatch(
        skipped_results=skipped_results,
        end_cursor=cursor_as_bytes,
        more_results=more_results_enum,
        entity_results=[
            query_pb2.EntityResult(entity=entity) for entity in entity_pbs
        ],
    ), )
Esempio n. 3
0
def create_entities(count, id_or_name=False):
    """Creates a list of entities with random keys."""
    entities = []

    for _ in range(count):
        entity_result = query_pb2.EntityResult()
        if id_or_name:
            entity_result.entity.key.path.add().id = (uuid.uuid4().int &
                                                      ((1 << 63) - 1))
        else:
            entity_result.entity.key.path.add().name = str(uuid.uuid4())
        entities.append(entity_result)

    return entities
Esempio n. 4
0
    def test_run_query_w_namespace_nonempty_result(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2
        from google.cloud.proto.datastore.v1 import entity_pb2
        from google.cloud.proto.datastore.v1 import query_pb2

        project = 'PROJECT'
        kind = 'Kind'
        namespace = 'NS'
        query_pb = self._make_query_pb(kind)
        partition_id = entity_pb2.PartitionId(project_id=project,
                                              namespace_id=namespace)
        read_options = datastore_pb2.ReadOptions()
        rsp_pb = datastore_pb2.RunQueryResponse(
            batch=query_pb2.QueryResultBatch(
                entity_result_type=query_pb2.EntityResult.FULL,
                entity_results=[
                    query_pb2.EntityResult(entity=entity_pb2.Entity()),
                ],
                more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS,
            ))

        # 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.
        ds_api = self._make_one(client)
        response = ds_api.run_query(project,
                                    partition_id,
                                    read_options,
                                    query=query_pb)

        # Check the result and verify the callers.
        self.assertEqual(response, rsp_pb)

        uri = _build_expected_url(client._base_url, project, 'runQuery')
        request = _verify_protobuf_call(http, uri,
                                        datastore_pb2.RunQueryRequest())
        self.assertEqual(request.partition_id, partition_id)
        self.assertEqual(request.query, query_pb)