Skip to content

Commit 5d9daba

Browse files
author
Lingling Peng
committed
move test to async dir; also add test for download; remove syn fixture in test class
1 parent 2a62c48 commit 5d9daba

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

tests/unit/synapseclient/models/unit_test_form.py renamed to tests/unit/synapseclient/models/async/unit_test_form_async.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from unittest.mock import AsyncMock, MagicMock, patch
23

34
import pytest
@@ -10,12 +11,6 @@
1011
class TestFormGroup:
1112
"""Unit tests for the FormGroup model."""
1213

13-
@pytest.fixture
14-
def syn(self):
15-
"""Mock Synapse client"""
16-
mock_syn = MagicMock(spec=Synapse)
17-
return mock_syn
18-
1914
@pytest.fixture
2015
def mock_response(self):
2116
"""Mock API response from create_form_group_async"""
@@ -64,23 +59,9 @@ async def test_create_async_without_name_raises_error(self, syn):
6459
await form_group.create_async(synapse_client=syn)
6560

6661

67-
class AsyncIteratorMock(MagicMock):
68-
async def __aiter__(self):
69-
# Simulate yielding items
70-
yield 1
71-
yield 2
72-
yield 3
73-
74-
7562
class TestFormData:
7663
"""Unit tests for the FormData model."""
7764

78-
@pytest.fixture
79-
def syn(self):
80-
"""Mock Synapse client"""
81-
mock_syn = MagicMock(spec=Synapse)
82-
return mock_syn
83-
8465
@pytest.fixture
8566
def mock_response(self):
8667
"""Mock API response from create_form_data_async"""
@@ -267,3 +248,48 @@ async def test_validate_filter_by_state_raises_error_for_invalid_states(
267248
filter_by_state=filter_by_state,
268249
allow_waiting_submission=not as_reviewer,
269250
)
251+
252+
async def test_download_async(self, syn):
253+
"""Test downloading form data asynchronously"""
254+
# GIVEN a FormData with a form_data_id
255+
form_data = FormData(form_data_id="67890", data_file_handle_id="54321")
256+
257+
# WHEN downloading the form data
258+
with patch(
259+
"synapseclient.core.download.download_functions.download_by_file_handle",
260+
new_callable=AsyncMock,
261+
) as mock_download_file_handle, patch.object(syn, "cache") as mock_cache, patch(
262+
"synapseclient.core.download.download_functions.ensure_download_location_is_directory",
263+
) as mock_ensure_dir:
264+
mock_cache.get.side_effect = "/tmp/foo"
265+
mock_ensure_dir.return_value = (
266+
mock_cache.get_cache_dir.return_value
267+
) = "/tmp/download"
268+
mock_file_name = f"SYNAPSE_FORM_{form_data.data_file_handle_id}.csv"
269+
270+
result = await form_data.download_async(
271+
synapse_client=syn, synapse_id="mock synapse_id"
272+
)
273+
274+
# THEN the API should be called with correct parameters
275+
mock_download_file_handle.assert_called_once_with(
276+
file_handle_id=form_data.data_file_handle_id,
277+
synapse_id="mock synapse_id",
278+
entity_type="FileEntity",
279+
destination=os.path.join(mock_ensure_dir.return_value, mock_file_name),
280+
synapse_client=syn,
281+
)
282+
283+
async def test_download_async_without_form_data_id_raises_error(self, syn):
284+
"""Test that downloading without form_data_id raises ValueError"""
285+
# GIVEN a FormData without a form_data_id
286+
form_data = FormData(form_data_id="67890")
287+
288+
# WHEN downloading the form data
289+
# THEN it should raise ValueError
290+
with pytest.raises(
291+
ValueError, match="data_file_handle_id must be set to download the file."
292+
):
293+
await form_data.download_async(
294+
synapse_client=syn, synapse_id="mock synapse_id"
295+
)

0 commit comments

Comments
 (0)