-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error handling to add errors to DB, fix tests.
- Loading branch information
1 parent
2de3d4d
commit f59cdd4
Showing
5 changed files
with
178 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import unittest | ||
from unittest import mock | ||
|
||
from ena_submission_helper import SubmissionConfig, create_ena_project | ||
from ena_types import default_project_type | ||
from requests import exceptions | ||
|
||
# Setup a mock configuration | ||
test_config = SubmissionConfig( | ||
ena_submission_url="https://test.url", | ||
ena_submission_password="test_password", | ||
ena_submission_username="test_user", | ||
) | ||
|
||
# Example XML responses | ||
test_project_xml_response = response = """ | ||
<RECEIPT receiptDate="2017-05-09T16:58:08.634+01:00" submissionFile="submission.xml" success="true"> | ||
<PROJECT accession="PRJEB20767" alias="cheddar_cheese" status="PRIVATE" /> | ||
<SUBMISSION accession="ERA912529" alias="cheese" /> | ||
<MESSAGES> | ||
<INFO>This submission is a TEST submission and will be discarded within 24 hours</INFO> | ||
</MESSAGES> | ||
<ACTIONS>ADD</ACTIONS> | ||
</RECEIPT> | ||
""" | ||
|
||
|
||
def mock_requests_post(status_code, text): | ||
mock_response = mock.Mock() | ||
mock_response.status_code = status_code | ||
mock_response.text = text | ||
return mock_response | ||
|
||
|
||
class ProjectCreationTests(unittest.TestCase): | ||
@mock.patch("requests.post") | ||
def test_create_project_success(self, mock_post): | ||
# Testing successful project creation | ||
mock_post.return_value = mock_requests_post(200, test_project_xml_response) | ||
project_set = default_project_type() | ||
response = create_ena_project(test_config, project_set) | ||
desired_response = { | ||
"ena_project_accession": "PRJEB20767", | ||
"ena_submission_accession": "ERA912529", | ||
} | ||
assert response.results == desired_response | ||
|
||
@mock.patch("requests.post") | ||
def test_create_project_failure(self, mock_post): | ||
# Testing project creation failure | ||
mock_post.return_value = mock_requests_post(500, "Internal Server Error") | ||
mock_post.return_value.raise_for_status.side_effect = exceptions.RequestException() | ||
project_set = default_project_type() | ||
response = create_ena_project(test_config, project_set) | ||
error_message_part = "Request failed with status:500." | ||
self.assertIn(error_message_part, response.errors[0]) | ||
print("success handled correctly") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.