diff --git a/emgapi/management/commands/mgx_api.py b/emgapi/management/commands/mgx_api.py index 7bbb1ce9d..984f8fe6e 100644 --- a/emgapi/management/commands/mgx_api.py +++ b/emgapi/management/commands/mgx_api.py @@ -17,10 +17,9 @@ import logging from django.core.management import BaseCommand -from django.conf import settings from emgapi.models import AnalysisJob, MetagenomicsExchange, ME_Broker -from metagenomics_exchange import MetagenomicsExchangeAPI +from emgapi.metagenomics_exchange import MetagenomicsExchangeAPI logger = logging.getLogger(__name__) @@ -101,22 +100,24 @@ def handle(self, *args, **options): self.assembly_accession = options.get("assembly") self.run_accession = options.get("run") - # FIXME: this command needs adjustments broker = options.get("broker") - url = settings.ME_API['dev'] - check_url = url + f'brokers/{self.broker}/datasets' - ME = MetagenomicsExchangeAPI() + broker_obj = ME_Broker.objects.get_or_create(broker) + ME = MetagenomicsExchangeAPI(broker=broker) analyses = self._filtering_analyses() for analysis in analyses: MGYA = analysis.accession public = not analysis.is_private # check is MGYA in ME - if ME.check_analysis(url=check_url, sourceID=MGYA, public=public, token=settings.ME_TOKEN): + if ME.check_analysis(source_id=MGYA, public=public): logging.info(f"{MGYA} exists in ME") else: logging.info(f"{MGYA} does not exist in ME") - ME.add_record(url=url, mgya=MGYA, run_accession=analysis.run.accesison, public=public, broker=broker, - token=settings.ME_TOKEN) + response = ME.add_record(mgya=MGYA, run_accession=analysis.run, public=public) + if response.status_code == 201: + logging.info(f"Populating MetagenomicsExchange table with {MGYA}") + MetagenomicsExchange.objects.get_or_create(analysis, broker=broker_obj) + else: + logging.error(f"{MGYA} response exit code: {response.status_code}. No population to DB.") diff --git a/emgapi/metagenomics_exchange.py b/emgapi/metagenomics_exchange.py index 166fd617e..2b2d44309 100644 --- a/emgapi/metagenomics_exchange.py +++ b/emgapi/metagenomics_exchange.py @@ -53,7 +53,7 @@ def post_request(self, endpoint: str, data: dict): def add_record(self, mgya: str, run_accession: str, public: bool): data = { "confidence": "full", - "endPoint": f"https://www.ebi.ac.uk/metagenomics/analyses/mgya", + "endPoint": f"https://www.ebi.ac.uk/metagenomics/analyses/{mgya}", "method": ["other_metadata"], "sourceID": mgya, "sequenceID": run_accession, @@ -61,7 +61,7 @@ def add_record(self, mgya: str, run_accession: str, public: bool): "brokerID": self.broker, } response = self.post_request(endpoint="datasets", data=data) - return response.json() + return response def check_analysis(self, source_id: str, public: bool) -> bool: logging.info(f"Check {source_id}") @@ -75,7 +75,7 @@ def check_analysis(self, source_id: str, public: bool) -> bool: datasets = data.get("datasets") for item in datasets: if item.get("sourceID") == source_id: + logging.info(f"{source_id} exists") return True - logging.info(f"{source_id} exists") - + logging.info(f"{source_id} does not exist") return False diff --git a/emgapi/models.py b/emgapi/models.py index b9348add6..3f6fb9442 100644 --- a/emgapi/models.py +++ b/emgapi/models.py @@ -1615,18 +1615,16 @@ class ME_Broker(models.Model): class MetagenomicsExchangeManager(models.Manager): - def get_or_create(self, analysis, broker=None): + def get_or_create(self, analysis, broker): try: me_record = MetagenomicsExchange.objects.get( - analysis=analysis + analysis=analysis, broker=broker ) except ObjectDoesNotExist: logging.warning(f"{analysis.accession} not in ME db, creating.") me_record = MetagenomicsExchange( - analysis=analysis, + analysis=analysis, broker=broker ) - if broker: - me_record.broker = broker me_record.save() logging.info(f"Created record ID: {me_record.id}") return me_record @@ -1644,6 +1642,9 @@ class MetagenomicsExchange(models.Model): objects = MetagenomicsExchangeManager() objects_admin = models.Manager() + def __str__(self): + return f"{self.analysis.job_id} under {self.broker}" + class StudyErrorType(models.Model): error_id = models.IntegerField( diff --git a/tests/me/test_metagenomics_exchange.py b/tests/me/test_metagenomics_exchange.py index e59ea4a33..f387618df 100644 --- a/tests/me/test_metagenomics_exchange.py +++ b/tests/me/test_metagenomics_exchange.py @@ -6,7 +6,9 @@ from emgapi.metagenomics_exchange import MetagenomicsExchangeAPI -from requests import HTTPError +import requests +import responses +import settings class TestME: @@ -24,5 +26,21 @@ def test_post_existing_analysis(self): me_api = MetagenomicsExchangeAPI() source_id = "MGYA00293719" # Should return -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409 - with pytest.raises(HTTPError, match="409 Client Error"): - me_api.add_record(mgya=source_id, run_accession="ERR3063408", public=True) + with pytest.raises(requests.HTTPError, match="409 Client Error"): + me_api.add_record(mgya=source_id, run_accession="ERR3063408", public=True).json() + + @responses.activate + def test_mock_post_new_analysis(self): + me_api = MetagenomicsExchangeAPI() + endpoint = "datasets" + url = settings.ME_API + f"/{endpoint}" + + responses.add(responses.POST, url, json={'success': True}, status=201) + + response = me_api.add_record(mgya="MGYA00593709", run_accession="SRR3960575", public=True) + + assert response.status_code == 201 + assert response.json() == {'success': True} + + + diff --git a/tests/me/test_api.py b/tests/me/test_mgx_api.py similarity index 80% rename from tests/me/test_api.py rename to tests/me/test_mgx_api.py index ee09abdba..a219747a7 100644 --- a/tests/me/test_api.py +++ b/tests/me/test_mgx_api.py @@ -13,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -""" + import pytest from unittest.mock import patch @@ -25,18 +25,21 @@ @pytest.mark.django_db class TestMeAPI: - @patch("emgapi.management.commands.mgx_api.Command.post_request") + @patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.post_request") + @pytest.mark.usefixtures( + "me_broker", + "metagenomics_exchange", + ) def test_new_analysis_population( self, - mock_post_request, - run_multiple_analysis + mock_post_request ): class MockResponse: def __init__(self, ok, status_code): self.ok = ok self.status_code = status_code - mock_post_request.return_value = MockResponse(True, 200) + mock_post_request.return_value = MockResponse(True, 201) test_run = "ABC01234" test_pipeline_version = 5.0 test_broker = 'EMG' @@ -49,9 +52,14 @@ def __init__(self, ok, status_code): ) analysis = AnalysisJob.objects.filter(run__accession=test_run).filter(pipeline__pipeline_id=test_pipeline_version).first() assert ME_Broker.objects.filter(brokerID=test_broker).count() == 1 - assert MetagenomicsExchange.objects.filter(analysis=analysis).count() == 1 + broker_id = ME_Broker.objects.filter(brokerID=test_broker).first().id + assert MetagenomicsExchange.objects.filter(analysis=analysis).filter(broker=broker_id).count() == 1 - @patch("emgapi.management.commands.mgx_api.Command.get_request") + @pytest.mark.usefixtures( + "me_broker", + "metagenomics_exchange", + ) + @patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.get_request") def test_check_existing_analysis( self, mock_get_request, @@ -67,6 +75,7 @@ def __init__(self, ok, status_code, json_data): def json(self): return self.json_data + test_run = "ABC01234" test_pipeline_version = 1.0 test_broker = 'MAR' @@ -83,5 +92,5 @@ def json(self): ) assert ME_Broker.objects.filter(brokerID=test_broker).count() == 1 - assert MetagenomicsExchange.objects.filter(analysis=analysis).count() == 1 - """ \ No newline at end of file + broker_id = ME_Broker.objects.filter(brokerID=test_broker).first().id + assert MetagenomicsExchange.objects.filter(analysis=analysis, broker=broker_id).count() == 1