From 9f3b9ded0930dc62e8ad948f6bb4fb23eb4e13d3 Mon Sep 17 00:00:00 2001 From: carlosribas Date: Tue, 23 Jul 2024 11:34:16 +0100 Subject: [PATCH] Add new unit tests --- app/tests.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 6 deletions(-) diff --git a/app/tests.py b/app/tests.py index 3d06523..3f34f15 100644 --- a/app/tests.py +++ b/app/tests.py @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -73,7 +73,7 @@ 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"} @@ -81,6 +81,60 @@ def test_fetch_data_with_empty_url(mock_fetch_data): 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, @@ -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")