def test_query_pi(db: Session): study1 = fakes.StudyFactory(id="study1", principal_investigator__name="John Doe") study2 = fakes.StudyFactory(id="study2", principal_investigator__name="Jane Doe") fakes.BiosampleFactory(id="sample1", study=study1) fakes.BiosampleFactory(id="sample2", study=study2) db.commit() q = query.StudyQuerySchema() assert q.facet(db, "principal_investigator_name") == { "John Doe": 1, "Jane Doe": 1, } q = query.StudyQuerySchema(conditions=[{ "table": "study", "field": "principal_investigator_name", "value": "John Doe", }]) assert ["study1"] == [r.id for r in q.execute(db)] qp = query.BiosampleQuerySchema(conditions=[{ "table": "study", "field": "principal_investigator_name", "value": "John Doe", }]) assert ["sample1"] == [r.id for r in qp.execute(db)]
def binned_facet_study( db: Session, attribute: str, conditions: List[query.ConditionSchema], **kwargs, ) -> query.BinnedFacetResponse: bins, facets = query.StudyQuerySchema(conditions=conditions).binned_facet( db, attribute, **kwargs) return query.BinnedFacetResponse(bins=bins, facets=facets)
def test_distinct_results(db: Session): study = fakes.StudyFactory(id="study1") fakes.BiosampleFactory(id="sample1", study=study) fakes.BiosampleFactory(id="sample2", study=study) fakes.BiosampleFactory(id="sample3", study=study) fakes.BiosampleFactory(id="sample4", study=study) db.commit() q = query.StudyQuerySchema(conditions=[]) assert len(q.execute(db).all()) == 1
def test_facet_foreign_table(db: Session): env_local1 = fakes.EnvoTermFactory(label="local1") env_local2 = fakes.EnvoTermFactory(label="local2") fakes.BiosampleFactory(id="sample1", env_local_scale=env_local1) fakes.BiosampleFactory(id="sample2", env_local_scale=env_local2) fakes.BiosampleFactory(id="sample3", env_local_scale=env_local2) db.commit() q = query.StudyQuerySchema(conditions=[]) assert q.facet(db, "env_local_scale") == {} assert q.facet(db, "sample_id") == {}
def test_indirect_join(db: Session): study = fakes.StudyFactory(id="study1") fakes.OmicsProcessingFactory(id="omics_processing1", biosample__study=study) db.commit() q = query.StudyQuerySchema(conditions=[{ "table": "omics_processing", "field": "id", "value": "omics_processing1", "op": "==" }]) assert {s.id for s in q.execute(db)} == {"study1"}
def test_basic_query(db: Session, table): tests: Dict[str, Tuple[fakes.AnnotatedFactory, query.BaseQuerySchema]] = { "study": (fakes.StudyFactory(), query.StudyQuerySchema()), "omics_processing": (fakes.OmicsProcessingFactory(), query.OmicsProcessingQuerySchema()), "biosample": (fakes.BiosampleFactory(), query.BiosampleQuerySchema()), "reads_qc": (fakes.ReadsQCFactory(), query.ReadsQCQuerySchema()), "metagenome_assembly": ( fakes.MetagenomeAssemblyFactory(), query.MetagenomeAssemblyQuerySchema(), ), "metagenome_annotation": ( fakes.MetagenomeAnnotationFactory(), query.MetagenomeAnnotationQuerySchema(), ), "metaproteomic_analysis": ( fakes.MetaproteomicAnalysisFactory(), query.MetaproteomicAnalysisQuerySchema(), ), } db.commit() q = tests[table][1].execute(db) assert tests[table][0].id in {r.id for r in q.all()}
def facet_study( db: Session, attribute: str, conditions: List[query.ConditionSchema]) -> query.FacetResponse: facets = query.StudyQuerySchema(conditions=conditions).facet(db, attribute) return query.FacetResponse(facets=facets)
def search_study(db: Session, conditions: List[query.ConditionSchema]) -> Query: return query.StudyQuerySchema(conditions=conditions).execute(db)