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 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 testInsert_getFailsForWithdrawnParticipant(self): self._make_summary() self.dao.insert(self._make_physical_measurements()) self.participant.version += 1 self.participant.withdrawalStatus = WithdrawalStatus.NO_USE ParticipantDao().update(self.participant) with self.assertRaises(Forbidden): self.dao.get(1) with self.assertRaises(Forbidden): self.dao.query(Query([FieldFilter('participantId', Operator.EQUALS, self.participant.participantId)], None, 10, None))
def make_query_filter(self, field_name, value): """Attempts to make a query filter for the model property with the specified name, matching the specified value. If no such property exists, None is returned. """ prop = getattr(self.model_type, field_name, None) if prop: property_type = get_property_type(prop) filter_value = None operator = Operator.EQUALS # If we're dealing with a comparable property type, look for a prefix that indicates an # operator other than EQUALS and strip it off if property_type in _COMPARABLE_PROPERTY_TYPES: for prefix, op in _OPERATOR_PREFIX_MAP.iteritems(): if isinstance(value, (str, unicode)) and value.startswith(prefix): operator = op value = value[len(prefix):] break filter_value = self._parse_value(prop, property_type, value) return FieldFilter(field_name, operator, filter_value) else: return None
def make_query_filter(self, field_name, value): """Attempts to make a query filter for the model property with the specified name, matching the specified value. If no such property exists, None is returned. """ prop = getattr(self.model_type, field_name, None) if prop: property_type = PROPERTY_TYPE_MAP.get(prop.__class__.__name__) filter_value = None try: if property_type == PropertyType.DATE: filter_value = api_util.parse_date(value) elif property_type == PropertyType.ENUM: filter_value = prop._enum_type(value) elif property_type == PropertyType.INTEGER: filter_value = int(value) else: filter_value = value except ValueError: raise BadRequest("Invalid value for %s of type %s: %s" % (field_name, property_type, value)) return FieldFilter(field_name, Operator.EQUALS, filter_value) else: return None
def get_one_by_file_name(self, filename): return super(GenomicSetDao, self) \ .query(Query([FieldFilter('genomicSetFile', Operator.EQUALS, filename)], None, 1, None)).items
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)
def list(self, participant_id=None): query = Query( [FieldFilter('participantId', Operator.EQUALS, participant_id)], None, DEFAULT_MAX_RESULTS, request.args.get('_token')) results = self.dao.query(query) return self._make_bundle(results, 'id', participant_id)