Skip to content

Commit

Permalink
Mock the Metagenomics Exchange API
Browse files Browse the repository at this point in the history
  • Loading branch information
mberacochea committed Feb 16, 2024
1 parent 6235f03 commit fe6d2b8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
22 changes: 15 additions & 7 deletions emgapi/metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ def check_analysis(self, mgya: str, sequence_accession: str, metadata=None):
Either the Run accession or the Assembly accession related to the MGYA.
Returns:
requests.models.Response
The response object from the API request.
tuple
A tuple containing two elements:
- analysis_registry_id : str
The analysis registry ID.
- metadata_match : boolean
True, if the metadata matchs.
"""
if not mgya:
raise ValueError(f"mgya is mandatory.")
Expand All @@ -143,7 +147,7 @@ def check_analysis(self, mgya: str, sequence_accession: str, metadata=None):

endpoint = f"sequences/{sequence_accession}/datasets"
analysis_registry_id = None
metadata_match = True
metadata_match = False

try:
response = self.get_request(endpoint=endpoint, params=params)
Expand Down Expand Up @@ -171,18 +175,22 @@ def check_analysis(self, mgya: str, sequence_accession: str, metadata=None):
]
logging.info(f"{mgya} exists in ME")
analysis_registry_id = found_record.get("registryID")
if not analysis_registry_id:
raise ValueError(f"The Metagenomics Exchange 'registryID' for {mgya} is null.")

if metadata:
for metadata_record in metadata:
if not (metadata_record in found_record):
metadata_match = False
return analysis_registry_id, metadata_match
return analysis_registry_id, False
else:
if metadata[metadata_record] != found_record[metadata_record]:
metadata_match = False
logging.info(
f"Incorrect field {metadata[metadata_record]} != {found_record[metadata_record]})"
f"The metadata doesn't match, for field {metadata[metadata_record]} != {found_record[metadata_record]})"
)
return analysis_registry_id, metadata_match
else:
metadata_match = True
return analysis_registry_id, metadata_match
return analysis_registry_id, metadata_match

return analysis_registry_id, metadata_match
Expand Down
70 changes: 44 additions & 26 deletions tests/me/test_metagenomics_exchange.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from unittest import mock

import pytest
import requests
import responses
Expand All @@ -12,18 +10,35 @@


class TestME:
def test_check_existing_analysis_me(self):
me_api = MetagenomicsExchangeAPI()

@responses.activate
def test_check_existing_analysis_me(self, settings):
# FIXME: this test doesn't check if the metadata matches or not.
mgya = "MGYA00293719"
sequence_accession = "ERR3063408"
return_values = me_api.check_analysis(mgya, sequence_accession)
assert return_values[0]
responses.add(
responses.GET,
f"{settings.METAGENOMICS_EXCHANGE_API}/sequences/{sequence_accession}/datasets",
json={"datasets": [{"sourceID": mgya, "registryID": "MGX_FAKE"}]},
status=200,
)
me_api = MetagenomicsExchangeAPI()

registry_id, _ = me_api.check_analysis(mgya, sequence_accession)
assert registry_id == "MGX_FAKE"

@responses.activate
def test_check_not_existing_analysis_me(self):
mgya = "MGYA10293719"
sequence_accession = "ERR3063408"
responses.add(
responses.GET,
f"{settings.METAGENOMICS_EXCHANGE_API}/sequences/{sequence_accession}/datasets",
json={"datasets": []},
status=200,
)
me_api = MetagenomicsExchangeAPI()
source_id = "MGYA10293719"
seq_id = "ERR3063408"
return_values = me_api.check_analysis(source_id, seq_id)
return_values = me_api.check_analysis(mgya, sequence_accession)
assert not return_values[0]

@pytest.mark.skip(reason="Error on ME API side")
Expand Down Expand Up @@ -62,38 +77,41 @@ def test_mock_delete_analysis_from_me(self):
assert response.status_code == 201
assert response.json() == {"success": True}

def test_wrong_delete_request_me(self):
@responses.activate
def test_incorrect_delete_request_me(self):
# TODO: this test doesn't make much sense
me_api = MetagenomicsExchangeAPI()
responses.add(
responses.DELETE,
f"{settings.METAGENOMICS_EXCHANGE_API}/dataset/MGX0000780",
status=401,
)
registry_id = "MGX0000780"
endpoint = f"dataset/{registry_id}"
assert not me_api.delete_request(endpoint)
response = me_api.delete_request(endpoint)
assert response.status_code == 401

@mock.patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.patch_request")
def test_patch_analysis_me(self, mock_patch_request):
@responses.activate
def test_patch_analysis_me(self):
me_api = MetagenomicsExchangeAPI()

class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
self.ok = True

def json(self):
return self.json_data

mock_patch_request.return_value = MockResponse({}, 200)
registry_id = "MGX0000788"
mgya = "MGYA00593709"
run_accession = "SRR3960575"
public = False

data = {
"confidence": "full",
"endPoint": f"https://www.ebi.ac.uk/metagenomics/analyses/{mgya}",
"method": ["other_metadata"],
"sourceID": mgya,
"sequenceID": run_accession,
"status": "public" if public else "private",
"status": "public",
"brokerID": "EMG",
}

responses.add(
responses.PATCH,
f"{settings.METAGENOMICS_EXCHANGE_API}/datasets/{registry_id}",
status=200,
)

assert me_api.patch_analysis(registry_id, data)

0 comments on commit fe6d2b8

Please sign in to comment.