Skip to content

Commit

Permalink
adding more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
costero-e committed Jan 23, 2025
1 parent a70b5fc commit 0202716
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 113 deletions.
4 changes: 2 additions & 2 deletions beacon/auth/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async def test_fetch_user_info():
IDP_JWKS_URL = os.getenv('JWKS_URL')
list_visa_datasets=[]
user, list_visa_datasets = await fetch_user_info(self, mock_access_token, IDP_USER_INFO, IDP_ISSUER, list_visa_datasets)
assert user.get('preferred_username') == 'costero-e'
assert user.get('preferred_username') == 'jane'
loop.run_until_complete(test_fetch_user_info())
loop.run_until_complete(client.close())
def test_auth_authentication(self):
Expand All @@ -113,7 +113,7 @@ def test_auth_authentication(self):
loop.run_until_complete(client.start_server())
async def test_authentication():
user, list_visa_datasets = await authentication(self, mock_access_token)
assert user.get('preferred_username') == 'costero-e'
assert user.get('preferred_username') == 'jane'
loop.run_until_complete(test_authentication())
loop.run_until_complete(client.close())
def test_auth_authentication_false(self):
Expand Down
4 changes: 2 additions & 2 deletions beacon/connections/mongo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pymongo.mongo_client import MongoClient
from beacon.connections.mongo import conf

if conf.database_cluster:
if conf.database_cluster:# pragma: no cover
uri = "mongodb+srv://{}:{}@{}/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000".format(
conf.database_user,
conf.database_password,
Expand All @@ -17,7 +17,7 @@
conf.database_auth_source
)

if conf.database_certificate != '' and conf.database_cafile != '':
if conf.database_certificate != '' and conf.database_cafile != '':# pragma: no cover
uri += '&tls=true&tlsCertificateKeyFile={}&tlsCAFile={}'.format(conf.database_certificate, conf.database_cafile)

client = MongoClient(uri)
18 changes: 3 additions & 15 deletions beacon/connections/mongo/analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,12 @@ def get_variants_of_analysis(self, entry_id: Optional[str], qparams: RequestPara
for bioid in bioids:
if bioid == analysis_ids["biosampleId"]:
break
position+=1
if position == len(bioids):
position+=1# pragma: no cover
if position == len(bioids):# pragma: no cover
schema = DefaultSchemas.GENOMICVARIATIONS
return schema, 0, -1, None, dataset
position=str(position)
filters=qparams.query.filters
if filters != []:
for filter in filters:
if filter['id']=='GENO:GENO_0000458':
query_cl={"$or": [{ position: "10", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
qparams.query.filters.remove(filter)
elif filter['id']=='GENO:GENO_0000136':
query_cl={"$or": [{ position: "11", "datasetId": dataset}]}
qparams.query.filters.remove(filter)
else:
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
else:
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
string_of_ids = client.beacon.caseLevelData \
.find(query_cl, {"id": 1, "_id": 0}).limit(qparams.query.pagination.limit).skip(qparams.query.pagination.skip)
HGVSIds=list(string_of_ids)
Expand Down
16 changes: 2 additions & 14 deletions beacon/connections/mongo/biosamples.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,11 @@ def get_variants_of_biosample(self, entry_id: Optional[str], qparams: RequestPar
if bioid == entry_id:
break
position+=1
if position == len(bioids):
if position == len(bioids):# pragma: no cover
schema = DefaultSchemas.GENOMICVARIATIONS
return schema, 0, -1, None, dataset
position=str(position)
filters=qparams.query.filters
if filters != []:
for filter in filters:
if filter['id']=='GENO:GENO_0000458':
query_cl={"$or": [{ position: "10", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
qparams.query.filters.remove(filter)
elif filter['id']=='GENO:GENO_0000136':
query_cl={"$or": [{ position: "11", "datasetId": dataset}]}
qparams.query.filters.remove(filter)
else:
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
else:
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
string_of_ids = client.beacon.caseLevelData \
.find(query_cl, {"id": 1, "_id": 0}).limit(qparams.query.pagination.limit).skip(qparams.query.pagination.skip)
HGVSIds=list(string_of_ids)
Expand Down
4 changes: 2 additions & 2 deletions beacon/connections/mongo/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ async def execute_function(self, entry_type: str, datasets: list, qparams: Reque
datasets_docs[dataset]=records
datasets_count[dataset]=dataset_count
else:
datasets.remove(dataset)
datasets.remove(dataset)# pragma: no cover
elif include == 'HIT':
if dataset_count != -1 and dataset_count != 0:
new_count+=dataset_count
datasets_docs[dataset]=records
datasets_count[dataset]=dataset_count
else:
datasets.remove(dataset)
else:
else:# pragma: no cover
if dataset_count == 0:
new_count+=dataset_count
datasets_docs[dataset]=records
Expand Down
54 changes: 30 additions & 24 deletions beacon/connections/mongo/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,21 @@ def cross_query(self, query: dict, scope: str, collection: str, request_paramete
HGVSIds = client.beacon.genomicVariations \
.find(query, {"identifiers.genomicHGVSId": 1, "_id": 0})
HGVSIds=list(HGVSIds)
HGVSId=HGVSIds[0]["identifiers"]["genomicHGVSId"]
queryHGVSId={"datasetId": dataset, "id": HGVSId}
list_of_variants_found=[]
for variant_found in HGVSIds:
list_of_variants_found.append(variant_found["identifiers"]["genomicHGVSId"])
queryHGVSId={"datasetId": dataset, "id": {"$in": list_of_variants_found}}
string_of_ids = client.beacon.caseLevelData \
.find(queryHGVSId, {"biosampleIds": 1, "_id": 0})
.find(queryHGVSId)
targets = client.beacon.targets \
.find({"datasetId": dataset}, {"biosampleIds": 1, "_id": 0})
targets=list(targets)
list_of_targets=targets[0]["biosampleIds"]
list_of_positions_strings= string_of_ids[0]['biosampleIds'].split(',')
list_of_positions_strings= string_of_ids[0]
biosampleIds=[]
for position in list_of_positions_strings:
if position != '':
biosampleIds.append(list_of_targets[int(position)])
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id':
biosampleIds.append(list_of_targets[int(key)])
try:
finalquery={}
finalquery["$or"]=[]
Expand All @@ -105,7 +107,7 @@ def cross_query(self, query: dict, scope: str, collection: str, request_paramete
finalids=[]
if finalids==[]:
finalids=biosampleIds
except Exception:
except Exception:# pragma: no cover
finalids=biosampleIds
query={}
query["$or"]=[]
Expand All @@ -116,49 +118,53 @@ def cross_query(self, query: dict, scope: str, collection: str, request_paramete
HGVSIds = client.beacon.genomicVariations \
.find(query, {"identifiers.genomicHGVSId": 1, "_id": 0})
HGVSIds=list(HGVSIds)
HGVSId=HGVSIds[0]["identifiers"]["genomicHGVSId"]
queryHGVSId={"datasetId": dataset, "id": HGVSId}
list_of_variants_found=[]
for variant_found in HGVSIds:
list_of_variants_found.append(variant_found["identifiers"]["genomicHGVSId"])
queryHGVSId={"datasetId": dataset, "id": {"$in": list_of_variants_found}}
string_of_ids = client.beacon.caseLevelData \
.find(queryHGVSId, {"biosampleIds": 1, "_id": 0})
.find(queryHGVSId)
targets = client.beacon.targets \
.find({"datasetId": dataset}, {"biosampleIds": 1, "_id": 0})
targets=list(targets)
list_of_targets=targets[0]["biosampleIds"]
list_of_positions_strings= string_of_ids[0]['biosampleIds'].split(',')
list_of_positions_strings= string_of_ids[0]
biosampleIds=[]
for position in list_of_positions_strings:
if position != '':
biosampleIds.append(list_of_targets[int(position)])
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id':
biosampleIds.append(list_of_targets[int(key)])
finalids=biosampleIds
try:
finalids=[]
for bioid in biosampleIds:
finalids.append({"biosampleId": bioid})
finalids.append({"id": bioid})
except Exception:# pragma: no cover
finalids=[]
query = {"$and": [{"$or": finalids}]}
elif scope == 'genomicVariation' and collection in ['analyses','runs']:
HGVSIds = client.beacon.genomicVariations \
.find(query, {"identifiers.genomicHGVSId": 1, "_id": 0})
HGVSIds=list(HGVSIds)
HGVSId=HGVSIds[0]["identifiers"]["genomicHGVSId"]
queryHGVSId={"datasetId": dataset, "id": HGVSId}
list_of_variants_found=[]
for variant_found in HGVSIds:
list_of_variants_found.append(variant_found["identifiers"]["genomicHGVSId"])
queryHGVSId={"datasetId": dataset, "id": {"$in": list_of_variants_found}}
string_of_ids = client.beacon.caseLevelData \
.find(queryHGVSId, {"biosampleIds": 1, "_id": 0})
.find(queryHGVSId)
targets = client.beacon.targets \
.find({"datasetId": dataset}, {"biosampleIds": 1, "_id": 0})
targets=list(targets)
list_of_targets=targets[0]["biosampleIds"]
list_of_positions_strings= string_of_ids[0]['biosampleIds'].split(',')
list_of_positions_strings= string_of_ids[0]
biosampleIds=[]
for position in list_of_positions_strings:
if position != '':
biosampleIds.append(list_of_targets[int(position)])
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id':
biosampleIds.append(list_of_targets[int(key)])
finalids=biosampleIds
try:
finalids=[]
for bioid in biosampleIds:
finalids.append({"id": bioid})
finalids.append({"biosampleId": bioid})
except Exception:# pragma: no cover
finalids=[]
query = {"$and": [{"$or": finalids}]}
Expand Down
31 changes: 17 additions & 14 deletions beacon/connections/mongo/g_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_biosamples_of_variant(self, entry_id: Optional[str], qparams: RequestPar
HGVSIds = client.beacon.genomicVariations \
.find(query, {"identifiers.genomicHGVSId": 1, "datasetId": 1, "_id": 0})
HGVSDataset=HGVSIds[0]["datasetId"]
if dataset != HGVSDataset:
if dataset != HGVSDataset:# pragma: no cover
schema = DefaultSchemas.BIOSAMPLES
return schema, 0, 0, [], dataset
HGVSIds=list(HGVSIds)
Expand All @@ -90,12 +90,12 @@ def get_biosamples_of_variant(self, entry_id: Optional[str], qparams: RequestPar
filters=qparams.query.filters
if filters != []:
for filter in filters:
if filter['id']=='GENO:GENO_0000458':
if filter['id']=='GENO:0000458':
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id' and value != '11':
biosampleIds.append(list_of_targets[int(key)])
qparams.query.filters.remove(filter)
elif filter['id']=='GENO:GENO_0000136':
elif filter['id']=='GENO:0000136':
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id' and value != '10' and value != '01':
biosampleIds.append(list_of_targets[int(key)])
Expand Down Expand Up @@ -142,7 +142,7 @@ def get_runs_of_variant(self, entry_id: Optional[str], qparams: RequestParams, d
HGVSIds = client.beacon.genomicVariations \
.find(query, {"identifiers.genomicHGVSId": 1, "datasetId": 1, "_id": 0})
HGVSDataset=HGVSIds[0]["datasetId"]
if dataset != HGVSDataset:
if dataset != HGVSDataset:# pragma: no cover
schema = DefaultSchemas.INDIVIDUALS
return schema, 0, 0, [], dataset
HGVSIds=list(HGVSIds)
Expand All @@ -159,12 +159,12 @@ def get_runs_of_variant(self, entry_id: Optional[str], qparams: RequestParams, d
filters=qparams.query.filters
if filters != []:
for filter in filters:
if filter['id']=='GENO:GENO_0000458':
if filter['id']=='GENO:0000458':
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id' and value != '11':
biosampleIds.append(list_of_targets[int(key)])
qparams.query.filters.remove(filter)
elif filter['id']=='GENO:GENO_0000136':
elif filter['id']=='GENO:0000136':
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id' and value != '10' and value != '01':
biosampleIds.append(list_of_targets[int(key)])
Expand Down Expand Up @@ -197,6 +197,9 @@ def get_runs_of_variant(self, entry_id: Optional[str], qparams: RequestParams, d

@log_with_args(level)
def get_analyses_of_variant(self, entry_id: Optional[str], qparams: RequestParams, dataset: str):
LOG.debug(entry_id)
LOG.debug(qparams)
LOG.debug(dataset)
collection = 'g_variants'
mongo_collection = client.beacon.analyses
query = {"$and": [{"variantInternalId": entry_id}]}
Expand All @@ -210,7 +213,7 @@ def get_analyses_of_variant(self, entry_id: Optional[str], qparams: RequestParam
HGVSIds = client.beacon.genomicVariations \
.find(query, {"identifiers.genomicHGVSId": 1, "datasetId": 1, "_id": 0})
HGVSDataset=HGVSIds[0]["datasetId"]
if dataset != HGVSDataset:
if dataset != HGVSDataset:# pragma: no cover
schema = DefaultSchemas.INDIVIDUALS
return schema, 0, 0, [], dataset
HGVSIds=list(HGVSIds)
Expand All @@ -227,12 +230,12 @@ def get_analyses_of_variant(self, entry_id: Optional[str], qparams: RequestParam
filters=qparams.query.filters
if filters != []:
for filter in filters:
if filter['id']=='GENO:GENO_0000458':
if filter['id']=='GENO:0000458':
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id' and value != '11':
biosampleIds.append(list_of_targets[int(key)])
qparams.query.filters.remove(filter)
elif filter['id']=='GENO:GENO_0000136':
elif filter['id']=='GENO:0000136':
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id' and value != '10' and value != '01':
biosampleIds.append(list_of_targets[int(key)])
Expand Down Expand Up @@ -279,7 +282,7 @@ def get_individuals_of_variant(self, entry_id: Optional[str], qparams: RequestPa
.find(query, {"identifiers.genomicHGVSId": 1, "datasetId": 1, "_id": 0})
HGVSIds=list(HGVSIds)
HGVSDataset=HGVSIds[0]["datasetId"]
if dataset != HGVSDataset:
if dataset != HGVSDataset:# pragma: no cover
schema = DefaultSchemas.INDIVIDUALS
return schema, 0, 0, [], dataset
HGVSId=HGVSIds[0]["identifiers"]["genomicHGVSId"]
Expand All @@ -295,12 +298,12 @@ def get_individuals_of_variant(self, entry_id: Optional[str], qparams: RequestPa
filters=qparams.query.filters
if filters != []:
for filter in filters:
if filter['id']=='GENO:GENO_0000458':
if filter['id']=='GENO:0000458':
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id' and value != '11':
biosampleIds.append(list_of_targets[int(key)])
qparams.query.filters.remove(filter)
elif filter['id']=='GENO:GENO_0000136':
elif filter['id']=='GENO:0000136':
for key, value in list_of_positions_strings.items():
if key != 'datasetId' and key != 'id' and key != '_id' and value != '10' and value != '01':
biosampleIds.append(list_of_targets[int(key)])
Expand Down Expand Up @@ -328,8 +331,8 @@ def get_individuals_of_variant(self, entry_id: Optional[str], qparams: RequestPa
except Exception:# pragma: no cover
finalids=[]
if finalids==[]:
finalids=biosampleIds
except Exception:
finalids=biosampleIds# pragma: no cover
except Exception:# pragma: no cover
finalids=biosampleIds
finalquery={}
finalquery["$or"]=[]
Expand Down
14 changes: 1 addition & 13 deletions beacon/connections/mongo/individuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,7 @@ def get_variants_of_individual(self, entry_id: Optional[str], qparams: RequestPa
schema = DefaultSchemas.GENOMICVARIATIONS
return schema, 0, -1, None, dataset
position=str(position)
filters=qparams.query.filters
if filters != []:
for filter in filters:
if filter['id']=='GENO:GENO_0000458':
query_cl={"$or": [{ position: "10", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
qparams.query.filters.remove(filter)
elif filter['id']=='GENO:GENO_0000136':
query_cl={"$or": [{ position: "11", "datasetId": dataset}]}
qparams.query.filters.remove(filter)
else:
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
else:
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
query_cl={"$or": [{ position: "10", "datasetId": dataset},{ position: "11", "datasetId": dataset}, { position: "01", "datasetId": dataset}]}
string_of_ids = client.beacon.caseLevelData \
.find(query_cl, {"id": 1, "_id": 0}).limit(qparams.query.pagination.limit).skip(qparams.query.pagination.skip)
HGVSIds=list(string_of_ids)
Expand Down
Loading

0 comments on commit 0202716

Please sign in to comment.