diff --git a/emgapi/metagenomics_exchange.py b/emgapi/metagenomics_exchange.py index fe3d5c0cc..1a8d92654 100644 --- a/emgapi/metagenomics_exchange.py +++ b/emgapi/metagenomics_exchange.py @@ -50,13 +50,13 @@ def post_request(self, endpoint: str, data: dict): response.raise_for_status() return response - def delete_request(self, endpoint: str, data: dict): + def delete_request(self, endpoint: str): headers = { "Accept": "application/json", "Authorization": self.__token, } response = requests.delete( - f"{self.base_url}/{endpoint}", json=data, headers=headers + f"{self.base_url}/{endpoint}", headers=headers ) response.raise_for_status() return response @@ -104,15 +104,16 @@ def check_analysis(self, source_id: str, public: bool) -> bool: return False def delete_analysis(self, registry_id: str): - data = {"registryID": registry_id} - response = self.delete_request(endpoint="datasets", data=data) + response = self.delete_request(endpoint=f"datasets/{registry_id}") if response.ok: - logging.info(f"{registry_id} was deleted") + logging.info(f"{registry_id} was deleted with {response.status_code}") return True else: if response.status_code == 400: logging.error(f"Bad request for {registry_id}") elif response.status_code == 401: + logging.error(f"Failed to authenticate for {registry_id}") + else: logging.error(f"{response.message} for {registry_id}") return False diff --git a/tests/me/test_metagenomics_exchange.py b/tests/me/test_metagenomics_exchange.py index ce7e55bdb..2621b1e90 100644 --- a/tests/me/test_metagenomics_exchange.py +++ b/tests/me/test_metagenomics_exchange.py @@ -11,17 +11,18 @@ import responses class TestME: - def test_check_existing_analysis(self): + + def test_check_existing_analysis_me(self): me_api = MetagenomicsExchangeAPI() source_id = "MGYA00293719" assert me_api.check_analysis(source_id, True) - def test_check_not_existing_analysis(self): + def test_check_not_existing_analysis_me(self): me_api = MetagenomicsExchangeAPI(broker="MAR") source_id = "MGYA00293719" assert not me_api.check_analysis(source_id, True) - def test_post_existing_analysis(self): + def test_post_existing_analysis_me(self): me_api = MetagenomicsExchangeAPI() source_id = "MGYA00293719" # Should return -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409 @@ -39,4 +40,45 @@ def test_mock_post_new_analysis(self): response = me_api.add_analysis(mgya="MGYA00593709", run_accession="SRR3960575", public=True) assert response.status_code == 201 - assert response.json() == {'success': True} \ No newline at end of file + assert response.json() == {'success': True} + + @responses.activate + def test_mock_delete_analysis_from_me(self): + me_api = MetagenomicsExchangeAPI() + registry_id = "MGX0000780" + endpoint = f"datasets/{registry_id}" + url = settings.ME_API + f"/{endpoint}" + + responses.add(responses.DELETE, url, json={'success': True}, status=201) + response = me_api.delete_request(endpoint) + + assert response.status_code == 201 + assert response.json() == {'success': True} + + + def test_wrong_delete_request_me(self): + me_api = MetagenomicsExchangeAPI() + registry_id = "MGX0000780" + endpoint = f"dataset/{registry_id}" + + with pytest.raises(requests.HTTPError, match="404 Client Error"): + me_api.delete_request(endpoint) + + def test_patch_analysis_me(self): + me_api = MetagenomicsExchangeAPI() + 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", + "brokerID": "EMG", + } + assert me_api.patch_analysis(registry_id, data) + diff --git a/tests/me/test_mgx_api.py b/tests/me/test_mgx_api.py index e8582c616..9a75acea0 100644 --- a/tests/me/test_mgx_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 @@ -24,7 +24,7 @@ from emgapi.metagenomics_exchange import MetagenomicsExchangeAPI from emgapi.models import AnalysisJob, MetagenomicsExchange, ME_Broker -""" + @pytest.mark.django_db class TestMeAPI: @patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.post_request")