def setUp(self): super(ParticipantSummaryDaoTest, self).setUp(use_mysql=True) self.dao = ParticipantSummaryDao() self.order_dao = BiobankOrderDao() self.measurement_dao = PhysicalMeasurementsDao() self.participant_dao = ParticipantDao() self.no_filter_query = Query([], None, 2, None) self.one_filter_query = Query( [FieldFilter("participantId", Operator.EQUALS, 1)], None, 2, None) self.two_filter_query = Query([ FieldFilter("participantId", Operator.EQUALS, 1), FieldFilter("hpoId", Operator.EQUALS, PITT_HPO_ID) ], None, 2, None) self.ascending_biobank_id_query = Query([], OrderBy("biobankId", True), 2, None) self.descending_biobank_id_query = Query([], OrderBy("biobankId", False), 2, None) self.enrollment_status_order_query = Query([], OrderBy( "enrollmentStatus", True), 2, None) self.hpo_id_order_query = Query([], OrderBy("hpoId", True), 2, None) self.first_name_order_query = Query([], OrderBy("firstName", True), 2, None)
def _make_query(self): field_filters = [] max_results = DEFAULT_MAX_RESULTS pagination_token = None order_by = None missing_id_list = ['awardee', 'organization', 'site'] include_total = request.args.get('_includeTotal', False) for key, value in request.args.iteritems(multi=True): if value in missing_id_list: if 'awardee' in value: value = 'hpoId' else: value = value + 'Id' if key == '_count': max_results = int(request.args['_count']) if max_results < 1: raise BadRequest("_count < 1") if max_results > MAX_MAX_RESULTS: raise BadRequest("_count exceeds {}".format(MAX_MAX_RESULTS)) elif key == '_token': pagination_token = value elif key == '_sort' or key == '_sort:asc': order_by = OrderBy(value, True) elif key == '_sort:desc': order_by = OrderBy(value, False) else: field_filter = self.dao.make_query_filter(key, value) if field_filter: field_filters.append(field_filter) return Query(field_filters, order_by, max_results, pagination_token, include_total=include_total)
def _add_order_by(self, query, order_by, field_names, fields): if order_by.field_name in _CODE_FILTER_FIELDS: return super(ParticipantSummaryDao, self)._add_order_by( query, OrderBy(order_by.field_name + 'Id', order_by.ascending), field_names, fields) return super(ParticipantSummaryDao, self)._add_order_by(query, order_by, field_names, fields)
def get_new_version_number(self, genomic_set_name): genomic_sets = super(GenomicSetDao, self)\ .query(Query([FieldFilter('genomicSetName', Operator.EQUALS, genomic_set_name)], OrderBy('genomicSetVersion', False), 1, None)).items if genomic_sets: return genomic_sets[0].genomicSetVersion + 1 else: return 1
def get_sync_results_for_request(dao, max_results): token = request.args.get('_token') count_str = request.args.get('_count') count = int(count_str) if count_str else max_results results = dao.query(Query([], OrderBy('logPositionId', True), count, token, always_return_token=True)) return make_sync_results_for_request(dao, results)
def test_query_no_filters(self): query = Query([], OrderBy(field_name='foo', ascending=True), 2, None) results = PARENT_DAO.query(query) self.assertEquals([], results.items) self.assertEquals(None, results.pagination_token) parent1 = ParentModel(key=ndb.Key(ParentModel, '321'), foo="a") PARENT_DAO.insert(parent1) results = PARENT_DAO.query(query) self.assertEquals([parent1], results.items) self.assertEquals(None, results.pagination_token) parent2 = ParentModel(key=ndb.Key(ParentModel, '123'), foo="bob") PARENT_DAO.insert(parent2) parent3 = ParentModel(key=ndb.Key(ParentModel, '456'), foo="AARDVARK") PARENT_DAO.insert(parent3) results = PARENT_DAO.query(query) self.assertEquals([parent1, parent3], results.items) self.assertTrue(results.pagination_token) query2 = Query([], OrderBy(field_name='foo', ascending=True), 2, results.pagination_token) results2 = PARENT_DAO.query(query2) self.assertEquals([parent2], results2.items) self.assertEquals(None, results2.pagination_token) # Now in descending order query3 = Query([], OrderBy(field_name='foo', ascending=False), 2, None) results3 = PARENT_DAO.query(query3) self.assertEquals([parent2, parent3], results3.items) self.assertTrue(results3.pagination_token) query4 = Query([], OrderBy(field_name='foo', ascending=False), 2, results3.pagination_token) results4 = PARENT_DAO.query(query4) self.assertEquals([parent1], results4.items) self.assertEquals(None, results4.pagination_token) # Now sort by last_modified query5 = Query([], OrderBy(field_name='last_modified', ascending=True), 3, None) results5 = PARENT_DAO.query(query5) self.assertEquals([parent1, parent2, parent3], results5.items) self.assertEquals(None, results5.pagination_token)
def _make_query(self): field_filters = [] max_results = DEFAULT_MAX_RESULTS pagination_token = None order_by = None for key, value in request.args.iteritems(multi=True): if key == '_count': max_results = int(request.args['_count']) if max_results < 1: raise BadRequest("_count < 1") if max_results > MAX_MAX_RESULTS: raise BadRequest( "_count exceeds {}".format(MAX_MAX_RESULTS)) elif key == '_token': pagination_token = value elif key == '_sort' or key == '_sort:asc': order_by = OrderBy(value, True) elif key == '_sort:desc': order_by = OrderBy(value, False) else: field_filter = self.dao.make_query_filter(key, value) if field_filter: field_filters.append(field_filter) return Query(field_filters, order_by, max_results, pagination_token)
def get_sync_results_for_request(dao, max_results): token = request.args.get('_token') count_str = request.args.get('_count') count = int(count_str) if count_str else max_results results = dao.query( Query([], OrderBy('logPositionId', True), count, token, always_return_token=True)) bundle_dict = {'resourceType': 'Bundle', 'type': 'history'} if results.pagination_token: query_params = request.args.copy() query_params['_token'] = results.pagination_token link_type = 'next' if results.more_available else 'sync' next_url = url_for(request.url_rule.endpoint, _external=True, **query_params) bundle_dict['link'] = [{'relation': link_type, 'url': next_url}] entries = [] for item in results.items: entries.append({'resource': dao.to_client_json(item)}) bundle_dict['entry'] = entries return jsonify(bundle_dict)
def test_query_with_filters(self): query = Query([FieldFilter('foo', Operator.EQUALS, 'A')], OrderBy(field_name='foo', ascending=True), 2, None) results = PARENT_DAO.query(query) self.assertEquals([], results.items) self.assertEquals(None, results.pagination_token) parent1 = ParentModel(key=ndb.Key(ParentModel, '321'), foo="a") PARENT_DAO.insert(parent1) results = PARENT_DAO.query(query) self.assertEquals([parent1], results.items) self.assertEquals(None, results.pagination_token) parent2 = ParentModel(key=ndb.Key(ParentModel, '123'), foo="bob") PARENT_DAO.insert(parent2) parent3 = ParentModel(key=ndb.Key(ParentModel, '456'), foo="AARDVARK") PARENT_DAO.insert(parent3) results = PARENT_DAO.query(query) self.assertEquals([parent1], results.items) self.assertEquals(None, results.pagination_token) query2 = Query([FieldFilter('foo', Operator.LESS_THAN, "bob")], OrderBy(field_name='foo', ascending=True), 2, None) results2 = PARENT_DAO.query(query2) self.assertEquals([parent1, parent3], results2.items) self.assertEquals(None, results2.pagination_token) query3 = Query( [FieldFilter('foo', Operator.LESS_THAN_OR_EQUALS, "bob")], OrderBy(field_name='foo', ascending=False), 2, None) results3 = PARENT_DAO.query(query3) self.assertEquals([parent2, parent3], results3.items) self.assertTrue(results3.pagination_token) query4 = Query( [FieldFilter('foo', Operator.LESS_THAN_OR_EQUALS, "bob")], OrderBy(field_name='foo', ascending=False), 2, results3.pagination_token) results4 = PARENT_DAO.query(query4) self.assertEquals([parent1], results4.items) self.assertEquals(None, results4.pagination_token) query5 = Query([FieldFilter('foo', Operator.GREATER_THAN, "a")], OrderBy(field_name='foo', ascending=True), 2, None) results5 = PARENT_DAO.query(query5) self.assertEquals([parent3, parent2], results5.items) self.assertEquals(None, results5.pagination_token) query6 = Query( [FieldFilter('foo', Operator.GREATER_THAN_OR_EQUALS, "a")], OrderBy(field_name='foo', ascending=True), 2, None) results6 = PARENT_DAO.query(query6) self.assertEquals([parent1, parent3], results6.items) self.assertTrue(results6.pagination_token) query7 = Query( [FieldFilter('foo', Operator.GREATER_THAN_OR_EQUALS, "a")], OrderBy(field_name='foo', ascending=True), 2, results6.pagination_token) results7 = PARENT_DAO.query(query7) self.assertEquals([parent2], results7.items) self.assertEquals(None, results7.pagination_token) query8 = Query([ FieldFilter('last_modified', Operator.EQUALS, results7.items[0].last_modified) ], OrderBy(field_name='foo', ascending=True), 2, None) results8 = PARENT_DAO.query(query8) self.assertEquals([parent2], results8.items) self.assertEquals(None, results8.pagination_token) query9 = Query([ FieldFilter('last_modified', Operator.EQUALS, parent2.last_modified), FieldFilter('foo', Operator.EQUALS, parent2.foo) ], OrderBy(field_name='foo', ascending=True), 2, None) results9 = PARENT_DAO.query(query9) self.assertEquals([parent2], results9.items) self.assertEquals(None, results9.pagination_token) query10 = Query([ FieldFilter('last_modified', Operator.EQUALS, parent2.last_modified), FieldFilter('foo', Operator.EQUALS, parent1.foo) ], OrderBy(field_name='foo', ascending=True), 2, None) results10 = PARENT_DAO.query(query10) self.assertEquals([], results10.items) self.assertEquals(None, results10.pagination_token)