Skip to content

Commit f4677c1

Browse files
author
Lingling Peng
committed
edit test; fix typo; fix log
1 parent f8af1ad commit f4677c1

3 files changed

Lines changed: 63 additions & 39 deletions

File tree

synapseclient/models/curation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ class GridCsvImportRequest(AsynchronousCommunicator):
10131013
"""The description of a csv for upload or download."""
10141014

10151015
schema: Optional[List[Column]] = None
1016-
"""The list of ColumnModel that describe the CSV file. Currently this is is required."""
1016+
"""The list of ColumnModel that describe the CSV file. Currently this is required."""
10171017

10181018
# Response fields (populated by fill_from_dict)
10191019
total_count: Optional[int] = field(default=None, compare=False)
@@ -2110,13 +2110,15 @@ async def main():
21102110
file_handle_id=file_handle_id,
21112111
schema=all_columns,
21122112
)
2113-
if csv_table_descriptor:
2114-
import_request.csv_descriptor = csv_table_descriptor
21152113
import_response = await import_request.send_job_and_wait_async(
21162114
timeout=timeout, synapse_client=synapse_client
21172115
)
21182116
client = Synapse.get_client(synapse_client=synapse_client)
21192117
client.logger.info(
2120-
f"CSV import to grid session {self.session_id} completed successfully, total count: {import_response.total_count}, total created: {import_response.created_count}, total updated: {import_response.updated_count}"
2118+
f"CSV import to grid session {self.session_id} completed successfully, "
2119+
f"total count: {import_response.total_count}, "
2120+
f"total created: {import_response.created_count}, "
2121+
f"total updated: {import_response.updated_count}"
21212122
)
2123+
21222124
return self

tests/integration/synapseclient/models/async/test_grid_async.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ async def test_delete_grid_session_validation_error_async(self) -> None:
185185
await grid.delete_async(synapse_client=self.syn)
186186

187187
async def test_import_csv_to_grid_session_async(
188-
self, record_set_fixture: RecordSet, schedule_for_cleanup: Callable[..., None]
188+
self,
189+
project_model,
190+
record_set_fixture: RecordSet,
191+
schedule_for_cleanup: Callable[..., None],
189192
) -> None:
190193
"""Test importing a CSV file into a grid session."""
191194

@@ -208,18 +211,16 @@ async def test_import_csv_to_grid_session_async(
208211
}
209212
)
210213

211-
project_name = str(uuid.uuid4())
212-
project = Project(name=project_name)
213-
project = await project.store_async(synapse_client=self.syn)
214-
215214
# Create a temporary CSV file
216215
with tempfile.NamedTemporaryFile("w", suffix=".csv", delete=False) as temp_csv:
217216
test_data.to_csv(temp_csv.name, index=False)
218217
self.schedule_for_cleanup(temp_csv.name)
219218

220-
file = await File(path=temp_csv.name, parent_id=project.id).store_async(
221-
synapse_client=self.syn
222-
)
219+
file = await File(
220+
path=temp_csv.name, parent_id=project_model.id
221+
).store_async(synapse_client=self.syn)
222+
223+
schedule_for_cleanup(file.id)
223224

224225
# WHEN: Importing the CSV into the grid session
225226
imported_grid = await created_grid.import_csv_async(
@@ -228,9 +229,6 @@ async def test_import_csv_to_grid_session_async(
228229
synapse_client=self.syn,
229230
)
230231

231-
schedule_for_cleanup(file.id)
232-
schedule_for_cleanup(project.id)
233-
234232
# THEN: The import should complete and return the Grid with the same session
235233
assert imported_grid.session_id == created_grid.session_id
236234

tests/unit/synapseclient/models/async/unit_test_curation_async.py

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Unit tests for the CurationTask and Grid models."""
22

33
import os
4-
from unittest.mock import AsyncMock, patch
4+
from unittest.mock import AsyncMock, MagicMock, patch
55

66
import pytest
77

@@ -838,55 +838,79 @@ async def test_import_csv_async(self) -> None:
838838
# GIVEN a Grid with a session_id
839839
grid = Grid(session_id=SESSION_ID)
840840

841-
# Mock the CreateGridRequest's send_job_and_wait_async
842841
csv_table_descriptor = CsvTableDescriptor(
843842
separator=",",
844843
quote_character='"',
845844
escape_character="\\",
846845
line_end=os.linesep,
847846
is_first_line_header=True,
848847
)
849-
mock_preview = UploadToTablePreviewRequest(
848+
expected_columns = [Column(name="col1", column_type="STRING", maximum_size=50)]
849+
850+
# Mock preview response with suggested columns
851+
mock_preview_response = UploadToTablePreviewRequest(
850852
csv_table_descriptor=csv_table_descriptor,
851-
suggested_columns=[
852-
Column(name="col1", column_type="STRING", maximum_size=50)
853-
],
853+
suggested_columns=expected_columns,
854854
sample_rows=[["value1"]],
855855
rows_scanned=1,
856856
)
857-
mock_import = GridCsvImportRequest(
857+
# Mock import response with row counts
858+
mock_import_response = GridCsvImportRequest(
858859
file_handle_id="1234567",
859-
csv_descriptor=csv_table_descriptor,
860-
schema=UploadToTablePreviewRequest.suggested_columns,
861860
total_count=1,
862861
created_count=1,
863862
updated_count=1,
864863
)
865864

865+
# Set up mock preview class: constructor returns a mock whose
866+
# send_job_and_wait_async returns the mock_preview_response
867+
mock_preview_instance = MagicMock()
868+
mock_preview_instance.send_job_and_wait_async = AsyncMock(
869+
return_value=mock_preview_response
870+
)
871+
872+
# Set up mock import class: constructor returns a mock whose
873+
# send_job_and_wait_async returns the mock_import_response
874+
mock_import_instance = MagicMock()
875+
mock_import_instance.send_job_and_wait_async = AsyncMock(
876+
return_value=mock_import_response
877+
)
878+
866879
# WHEN I call import_csv_async
867880
with (
868-
patch.object(
869-
UploadToTablePreviewRequest,
870-
"send_job_and_wait_async",
871-
new_callable=AsyncMock,
872-
return_value=mock_preview,
873-
),
874-
patch.object(
875-
GridCsvImportRequest,
876-
"send_job_and_wait_async",
877-
new_callable=AsyncMock,
878-
return_value=mock_import,
879-
),
881+
patch(
882+
"synapseclient.models.curation.UploadToTablePreviewRequest",
883+
return_value=mock_preview_instance,
884+
) as MockPreview,
885+
patch(
886+
"synapseclient.models.curation.GridCsvImportRequest",
887+
return_value=mock_import_instance,
888+
) as MockImport,
880889
patch.object(self.syn, "logger") as mock_logger,
881890
):
882891
result = await grid.import_csv_async(
883-
synapse_client=self.syn, file_handle_id="1234567"
892+
synapse_client=self.syn,
893+
file_handle_id="1234567",
894+
csv_table_descriptor=csv_table_descriptor,
884895
)
885896

886-
# THEN the grid should be populated with session data
897+
# THEN the grid is returned with the same session
887898
assert result.session_id == SESSION_ID
888899

889-
# AND the log message should contain the import counts
900+
# AND UploadToTablePreviewRequest was constructed with the right arguments
901+
MockPreview.assert_called_once_with(
902+
csv_table_descriptor=csv_table_descriptor,
903+
upload_file_handle_id="1234567",
904+
)
905+
906+
# AND GridCsvImportRequest was constructed with the schema from the preview
907+
MockImport.assert_called_once_with(
908+
session_id=SESSION_ID,
909+
file_handle_id="1234567",
910+
schema=expected_columns,
911+
)
912+
913+
# AND the log message contains the import counts
890914
mock_logger.info.assert_called_once()
891915
log_message = mock_logger.info.call_args[0][0]
892916
assert "total count: 1" in log_message

0 commit comments

Comments
 (0)