Skip to content

Commit

Permalink
Add new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosribas committed Jul 23, 2024
1 parent 75d83e9 commit 9f3b9de
Showing 1 changed file with 86 additions and 6 deletions.
92 changes: 86 additions & 6 deletions app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@pytest.fixture
def mock_fetch_data(mocker):
def mock_data(mocker):
mock_task = Mock()
mock_task.id = "mock-task-id"
mocker.patch.object(
Expand All @@ -21,7 +21,7 @@ def mock_fetch_data(mocker):
return mock_task


def test_fetch_data_with_fasta(mock_fetch_data):
def test_submit_data_with_fasta(mock_data):
api = ("https://wwwdev.ebi.ac.uk/ebisearch/ws/rest/rnacentral?"
"query=(TAXONOMY:559292)&size=1&sort=id&format=json")
response = client.post(
Expand All @@ -35,7 +35,7 @@ def test_fetch_data_with_fasta(mock_fetch_data):
}


def test_fetch_data_with_txt(mock_fetch_data):
def test_submit_data_with_txt(mock_data):
api = ("https://wwwdev.ebi.ac.uk/ebisearch/ws/rest/rnacentral?"
"query=(TAXONOMY:559292)&size=1&sort=id&format=json")
response = client.post(
Expand All @@ -49,7 +49,7 @@ def test_fetch_data_with_txt(mock_fetch_data):
}


def test_fetch_data_with_json(mock_fetch_data):
def test_submit_data_with_json(mock_data):
api = ("https://wwwdev.ebi.ac.uk/ebisearch/ws/rest/rnacentral?"
"query=(TAXONOMY:559292)&size=1&sort=id&format=json")
response = client.post(
Expand All @@ -63,7 +63,7 @@ def test_fetch_data_with_json(mock_fetch_data):
}


def test_fetch_data_with_invalid_data_type(mock_fetch_data):
def test_submit_data_with_invalid_data_type(mock_data):
api = ("https://wwwdev.ebi.ac.uk/ebisearch/ws/rest/rnacentral?"
"query=(TAXONOMY:559292)&size=1&sort=id&format=json")
response = client.post(
Expand All @@ -73,14 +73,68 @@ def test_fetch_data_with_invalid_data_type(mock_fetch_data):
assert response.status_code == 422


def test_fetch_data_with_empty_url(mock_fetch_data):
def test_submit_data_with_empty_url(mock_data):
response = client.post(
"/submit/",
json={"api_url": "", "data_type": "json"}
)
assert response.status_code == 422


def test_get_task_info_success(mocker):
mock_result = Mock(state="SUCCESS")
mock_result.result = {
"query": "test_query",
"data_type": "json",
"hit_count": 100,
"progress_ids": 100,
"progress_db_data": 100
}
mocker.patch.object(
fetch_data_from_search_index,
"AsyncResult",
return_value=mock_result
)
response = client.get("/status/mock-task-id")
assert response.status_code == 200
assert response.json() == {
"task_id": "mock-task-id",
"state": "SUCCESS",
"query": "test_query",
"data_type": "json",
"hit_count": 100,
"progress_ids": 100,
"progress_db_data": 100
}


def test_get_task_info_running(mocker):
mock_result = Mock(state="RUNNING")
mock_result.info = {
"query": "test_query",
"data_type": "fasta",
"hit_count": 200,
"progress_ids": 50,
"progress_fasta": 25
}
mocker.patch.object(
fetch_data_from_search_index,
"AsyncResult",
return_value=mock_result
)
response = client.get("/status/mock-task-id")
assert response.status_code == 200
assert response.json() == {
"task_id": "mock-task-id",
"state": "RUNNING",
"query": "test_query",
"data_type": "fasta",
"hit_count": 200,
"progress_ids": 50,
"progress_fasta": 25
}


def test_download_file_pending(mocker):
mocker.patch.object(
fetch_data_from_search_index,
Expand Down Expand Up @@ -171,3 +225,29 @@ def test_download_file_progress(mocker):
"progress_ids": 50,
"progress_db_data": 0
}


def test_stream_file_success(mocker):
file_path = "/srv/results/mock-task-id.json.gz"
filename = "mock-task-id.json.gz"
file_size = 1024

mocker.patch("os.path.exists", return_value=True)
mocker.patch("os.path.getsize", return_value=file_size)
mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data=b"data"))

mock_result = Mock()
mock_result.state = "SUCCESS"
mock_result.info = {}
mocker.patch.object(
fetch_data_from_search_index,
"AsyncResult",
return_value=mock_result
)

response = client.get("/download/mock-task-id/json")

assert response.status_code == 200
assert response.headers["Content-Disposition"] == f"attachment; filename={filename}"
assert response.headers["Content-Length"] == str(file_size)
mock_open.assert_called_once_with(file_path, "rb")

0 comments on commit 9f3b9de

Please sign in to comment.