diff --git a/emgapi/management/commands/populate_metagenomics_exchange.py b/emgapi/management/commands/populate_metagenomics_exchange.py index b5525d163..2e1d0da39 100644 --- a/emgapi/management/commands/populate_metagenomics_exchange.py +++ b/emgapi/management/commands/populate_metagenomics_exchange.py @@ -104,7 +104,6 @@ def process_to_index_and_update_records(self, analyses_to_index_and_update): ): jobs_to_update = [] for annotation_job in page: - sequence_accession = "" if annotation_job.run: sequence_accession = annotation_job.run.accession @@ -112,7 +111,7 @@ def process_to_index_and_update_records(self, analyses_to_index_and_update): sequence_accession = annotation_job.assembly.accession metadata = self.mgx_api.generate_metadata( - mgya=annotation_job.accession, run_accession=sequence_accession + mgya=annotation_job.accession, sequence_accession=sequence_accession ) registry_id, metadata_match = self.mgx_api.check_analysis( mgya=annotation_job.accession, @@ -129,7 +128,8 @@ def process_to_index_and_update_records(self, analyses_to_index_and_update): continue response = self.mgx_api.add_analysis( - mgya=annotation_job.accession, run_accession=annotation_job.run + mgya=annotation_job.accession, + sequence_accession=annotation_job.run, ) if response.ok: logging.info(f"Successfully added {annotation_job}") @@ -193,7 +193,6 @@ def process_to_delete_records(self, analyses_to_delete): jobs_to_update = [] for annotation_job in page: - sequence_accession = "" if annotation_job.run: sequence_accession = annotation_job.run.accession @@ -201,7 +200,7 @@ def process_to_delete_records(self, analyses_to_delete): sequence_accession = annotation_job.assembly.accession metadata = self.mgx_api.generate_metadata( - mgya=annotation_job.accession, run_accession=annotation_job.run + mgya=annotation_job.accession, sequence_accession=sequence_accession ) registry_id, _ = self.mgx_api.check_analysis( mgya=annotation_job.accession, diff --git a/tests/me/test_metagenomics_exchange.py b/tests/me/test_metagenomics_exchange.py index c964c9d85..6531bc63f 100644 --- a/tests/me/test_metagenomics_exchange.py +++ b/tests/me/test_metagenomics_exchange.py @@ -1,32 +1,29 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import pytest -import logging +from unittest import mock +import pytest +import requests +import responses from django.conf import settings from emgapi.metagenomics_exchange import MetagenomicsExchangeAPI -import requests -import responses -from unittest import mock - class TestME: - def test_check_existing_analysis_me(self): me_api = MetagenomicsExchangeAPI() - source_id = "MGYA00293719" - seq_id = "ERR3063408" - return_values = me_api.check_analysis(source_id, seq_id, True) + mgya = "MGYA00293719" + sequence_accession = "ERR3063408" + return_values = me_api.check_analysis(mgya, sequence_accession) assert return_values[0] def test_check_not_existing_analysis_me(self): me_api = MetagenomicsExchangeAPI() source_id = "MGYA10293719" seq_id = "ERR3063408" - return_values = me_api.check_analysis(source_id, seq_id, True) + return_values = me_api.check_analysis(source_id, seq_id) assert not return_values[0] @pytest.mark.skip(reason="Error on ME API side") @@ -35,9 +32,7 @@ def test_post_existing_analysis_me(self): source_id = "MGYA00293719" # Should return -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409 with pytest.raises(requests.HTTPError, match="401 Client Error"): - me_api.add_analysis( - mgya=source_id, run_accession="ERR3063408", public=True - ).json() + me_api.add_analysis(mgya=source_id, sequence_accession="ERR3063408").json() @responses.activate def test_mock_post_new_analysis(self): @@ -48,7 +43,7 @@ def test_mock_post_new_analysis(self): responses.add(responses.POST, url, json={"success": True}, status=201) response = me_api.add_analysis( - mgya="MGYA00593709", run_accession="SRR3960575", public=True + mgya="MGYA00593709", sequence_accession="SRR3960575" ) assert response.status_code == 201 diff --git a/tests/me/test_populate_metagenomics_exchange.py b/tests/me/test_populate_metagenomics_exchange.py index d9a35f32d..d10083ac1 100644 --- a/tests/me/test_populate_metagenomics_exchange.py +++ b/tests/me/test_populate_metagenomics_exchange.py @@ -42,8 +42,10 @@ def test_population_dry_mode(self, caplog): assert "Dry-mode run: no addition to real ME for MGYA00466090" in caplog.text assert "Dry-mode run: no addition to real ME for MGYA00466091" in caplog.text assert "Processing 1 analyses to remove" in caplog.text - assert "MGYA00005678 doesn't exist in the registry, nothing to delete" in caplog.text - + assert ( + "MGYA00005678 doesn't exist in the registry, nothing to delete" + in caplog.text + ) @pytest.mark.usefixtures("run_multiple_analysis_me") @mock.patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.add_analysis") @@ -56,16 +58,18 @@ def test_add_new_analysis(self, mock_check_analysis, mock_add_analysis, caplog): """ pipeline = 4.1 registry_id = "MGX1" + 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 def mock_check_process(*args, **kwargs): - if 'metadata' in kwargs: + if "metadata" in kwargs: return None, True else: return registry_id, True @@ -84,14 +88,10 @@ def mock_check_process(*args, **kwargs): assert ajob.last_mgx_indexed assert ajob.mgx_accession == registry_id - @pytest.mark.usefixtures("run_multiple_analysis_me") @mock.patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.check_analysis") @mock.patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.delete_analysis") - def test_removals(self, - mock_delete_analysis, - mock_check_analysis, - caplog): + def test_removals(self, mock_delete_analysis, mock_check_analysis, caplog): """ Test delete process. 1 analysis should be removed and updated indexed field in DB @@ -100,10 +100,7 @@ def test_removals(self, mock_check_analysis.return_value = True, True mock_delete_analysis.return_value = True - call_command( - "populate_metagenomics_exchange", - pipeline=pipeline - ) + call_command("populate_metagenomics_exchange", pipeline=pipeline) assert "Indexing 0 new analyses" in caplog.text assert "Processing 1 analyses to remove" in caplog.text assert "Deleting MGYA00005678" in caplog.text @@ -114,10 +111,7 @@ def test_removals(self, @pytest.mark.usefixtures("run_multiple_analysis_me") @mock.patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.check_analysis") @mock.patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.patch_analysis") - def test_update(self, - mock_patch_analysis, - mock_check_analysis, - caplog): + def test_update(self, mock_patch_analysis, mock_check_analysis, caplog): """ Test update process for job that was indexed before updated. MGX accession and last_mgx_indexed should be updated @@ -138,5 +132,3 @@ def test_update(self, ajob = AnalysisJob.objects.filter(pipeline__release_version=pipeline).first() assert ajob.last_mgx_indexed.date() == timezone.now().date() assert ajob.mgx_accession == registry_id - -