def test_run_query_wo_namespace_empty_result(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.query import Query DATASET_ID = 'DATASET' KIND = 'Nonesuch' CURSOR = b'\x00' q_pb = Query(KIND, DATASET_ID).to_protobuf() rsp_pb = datastore_pb.RunQueryResponse() rsp_pb.batch.end_cursor = CURSOR no_more = datastore_pb.QueryResultBatch.NO_MORE_RESULTS rsp_pb.batch.more_results = no_more rsp_pb.batch.entity_result_type = datastore_pb.EntityResult.FULL conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'runQuery', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) pbs, end, more, skipped = conn.run_query(DATASET_ID, q_pb) self.assertEqual(pbs, []) self.assertEqual(end, CURSOR) self.assertTrue(more) self.assertEqual(skipped, 0) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.RunQueryRequest request = rq_class() request.ParseFromString(cw['body']) self.assertEqual(request.partition_id.namespace, '') self.assertEqual(request.query, q_pb)
def test_run_query_w_namespace_nonempty_result(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.query import Query DATASET_ID = 'DATASET' KIND = 'Kind' entity = datastore_pb.Entity() q_pb = Query(KIND, DATASET_ID).to_protobuf() rsp_pb = datastore_pb.RunQueryResponse() rsp_pb.batch.entity_result.add(entity=entity) rsp_pb.batch.entity_result_type = 1 # FULL rsp_pb.batch.more_results = 3 # NO_MORE_RESULTS conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'runQuery', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) pbs = conn.run_query(DATASET_ID, q_pb, 'NS')[0] self.assertEqual(len(pbs), 1) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.RunQueryRequest request = rq_class() request.ParseFromString(cw['body']) self.assertEqual(request.partition_id.namespace, 'NS') self.assertEqual(request.query, q_pb)
def test_run_query_wo_namespace_empty_result(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.query import Query DATASET_ID = 'DATASET' KIND = 'Nonesuch' q_pb = Query(KIND, DATASET_ID).to_protobuf() rsp_pb = datastore_pb.RunQueryResponse() conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'runQuery', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) pbs, end, more, skipped = conn.run_query(DATASET_ID, q_pb) self.assertEqual(pbs, []) self.assertEqual(end, '') self.assertTrue(more) self.assertEqual(skipped, 0) cw = http._called_with self.assertEqual(cw['uri'], URI) self.assertEqual(cw['method'], 'POST') self.assertEqual(cw['headers']['Content-Type'], 'application/x-protobuf') self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT) rq_class = datastore_pb.RunQueryRequest request = rq_class() request.ParseFromString(cw['body']) self.assertEqual(request.partition_id.namespace, '') self.assertEqual(request.query, q_pb)
def query(self, **kwargs): """Proxy to :class:`gcloud.datastore.query.Query`. Passes our ``dataset_id``. """ if 'dataset_id' in kwargs: raise TypeError('Cannot pass dataset_id') kwargs['dataset_id'] = self.dataset_id return Query(**kwargs)
def list_reports(employee_id=None, status=None): query = Query(kind='Expense Report') if employee_id is not None: key = Key('Employee', employee_id) query.ancestor = key if status is not None: query.add_filter('status', '=', status) for report in query.fetch(): yield _report_info(report)
def query(self, *args, **kwargs): """Create a query bound to this dataset. :param args: positional arguments, passed through to the Query :param kw: keyword arguments, passed through to the Query :rtype: :class:`gcloud.datastore.query.Query` :returns: a new Query instance, bound to this dataset. """ kwargs['dataset'] = self return Query(*args, **kwargs)
def query(self, **kwargs): """Proxy to :class:`gcloud.datastore.query.Query`. Passes our ``dataset_id``. """ if 'client' in kwargs: raise TypeError('Cannot pass client') if 'dataset_id' in kwargs: raise TypeError('Cannot pass dataset_id') kwargs['dataset_id'] = self.dataset_id if 'namespace' not in kwargs: kwargs['namespace'] = self.namespace return Query(self, **kwargs)
def query(self, **kwargs): """Proxy to :class:`gcloud.datastore.query.Query`. Passes our ``project``. """ if 'client' in kwargs: raise TypeError('Cannot pass client') if 'project' in kwargs: raise TypeError('Cannot pass project') kwargs['project'] = self.project if 'namespace' not in kwargs: kwargs['namespace'] = self.namespace return Query(self, **kwargs)
def test_run_query_w_eventual_and_transaction(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.query import Query DATASET_ID = 'DATASET' KIND = 'Nonesuch' CURSOR = b'\x00' TRANSACTION = 'TRANSACTION' q_pb = Query(KIND, DATASET_ID).to_protobuf() rsp_pb = datastore_pb.RunQueryResponse() rsp_pb.batch.end_cursor = CURSOR no_more = datastore_pb.QueryResultBatch.NO_MORE_RESULTS rsp_pb.batch.more_results = no_more rsp_pb.batch.entity_result_type = datastore_pb.EntityResult.FULL conn = self._makeOne() conn.transaction(Transaction(TRANSACTION)) self.assertRaises( ValueError, conn.run_query, DATASET_ID, q_pb, eventual=True)
def query(self, **kwargs): """Proxy to :class:`gcloud.datastore.query.Query`. Passes our ``project``. Using query to search a datastore:: >>> from gcloud import datastore >>> client = datastore.Client() >>> query = client.query(kind='MyKind') >>> query.add_filter('property', '=', 'val') Using the query iterator's :meth:`next_page() <gcloud.datastore.query.Iterator.next_page>` method: >>> query_iter = query.fetch() >>> entities, more_results, cursor = query_iter.next_page() >>> entities [<list of Entity unmarshalled from protobuf>] >>> more_results <boolean of more results> >>> cursor <string containing cursor where fetch stopped> Under the hood this is doing: >>> connection.run_query('project', query.to_protobuf()) [<list of Entity Protobufs>], cursor, more_results, skipped_results :type kwargs: dict :param kwargs: Parameters for initializing and instance of :class:`gcloud.datastore.query.Query`. :rtype: :class:`gcloud.datastore.query.Query` :returns: An instance of :class:`gcloud.datastore.query.Query` """ if 'client' in kwargs: raise TypeError('Cannot pass client') if 'project' in kwargs: raise TypeError('Cannot pass project') kwargs['project'] = self.project if 'namespace' not in kwargs: kwargs['namespace'] = self.namespace return Query(self, **kwargs)
def query(self, *args, **kwargs): from gcloud.datastore.query import Query kwargs['dataset'] = self return Query(*args, **kwargs)
def _fetch_report_items(report): query = Query(kind='Expense Item') query.ancestor = report.key for item in query.fetch(): yield item
def _fetch_reports(employee): query = Query(kind='Expense Report') query.ancestor = employee.key for item in query.fetch(): yield item
def list_employees(): query = Query(kind='Employee') for employee in query.fetch(): yield _employee_info(employee)