Skip to content

Commit

Permalink
Add delete and patch commands with tests to ME API
Browse files Browse the repository at this point in the history
  • Loading branch information
KateSakharova committed Oct 24, 2023
1 parent a4ce3e7 commit fd2ff98
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
11 changes: 6 additions & 5 deletions emgapi/metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
50 changes: 46 additions & 4 deletions tests/me/test_metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
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)

4 changes: 2 additions & 2 deletions tests/me/test_mgx_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit fd2ff98

Please sign in to comment.