Skip to content

Commit bf262a9

Browse files
author
Lingling Peng
committed
correct errors; add unit test for import_csv
1 parent 45be50d commit bf262a9

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

synapseclient/models/curation.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ class UploadToTablePreviewRequest(AsynchronousCommunicator):
10811081
upload_file_handle_id: Optional[str] = None
10821082
"""The ID of the file handle for a type of UPLOAD"""
10831083

1084-
line_to_skip: Optional[int] = None
1084+
lines_to_skip: Optional[int] = None
10851085
"""The number of lines to skip from the start of the file. The default value of 0 will be used if this is not provided by the caller."""
10861086

10871087
csv_table_descriptor: CsvTableDescriptor = field(default_factory=CsvTableDescriptor)
@@ -1137,8 +1137,8 @@ def to_synapse_request(self) -> Dict[str, Any]:
11371137
request_dict: Dict[str, Any] = {"concreteType": self.concrete_type}
11381138
if self.upload_file_handle_id is not None:
11391139
request_dict["uploadFileHandleId"] = self.upload_file_handle_id
1140-
if self.line_to_skip is not None:
1141-
request_dict["linesToSkip"] = self.line_to_skip
1140+
if self.lines_to_skip is not None:
1141+
request_dict["linesToSkip"] = self.lines_to_skip
11421142
if self.csv_table_descriptor is not None:
11431143
request_dict["csvTableDescriptor"] = (
11441144
self.csv_table_descriptor.to_synapse_request()
@@ -2094,13 +2094,21 @@ async def main():
20942094
asyncio.run(main())
20952095
```
20962096
"""
2097+
2098+
if not self.session_id:
2099+
raise ValueError(
2100+
"session_id is required to import a CSV into a GridSession"
2101+
)
2102+
20972103
upload_to_table_preview = UploadToTablePreviewRequest(
20982104
csv_table_descriptor=csv_table_descriptor,
20992105
upload_file_handle_id=file_handle_id,
21002106
)
2107+
21012108
preview_response = await upload_to_table_preview.send_job_and_wait_async(
21022109
timeout=timeout
21032110
)
2111+
21042112
# ROW_ID and ROW_VERSION must be prepended to the schema to avoid:
21052113
# SynapseHTTPError: 400 Client Error: The CSV header does not match the schema size.
21062114
all_columns = [
@@ -2117,7 +2125,8 @@ async def main():
21172125
import_response = await import_request.send_job_and_wait_async(
21182126
synapse_client=synapse_client, timeout=timeout
21192127
)
2120-
print(
2128+
client = Synapse.get_client(synapse_client=synapse_client)
2129+
client.logger.info(
21212130
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}"
21222131
)
21232132
return self

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

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Unit tests for the CurationTask and Grid models."""
22

3+
import os
34
from unittest.mock import AsyncMock, patch
45

56
import pytest
@@ -817,6 +818,81 @@ async def mock_list(*args, **kwargs):
817818
assert len(results) == 1
818819
assert results[0].session_id == SESSION_ID
819820

821+
async def test_import_csv_async_without_session_id(self) -> None:
822+
"""Test that calling import_csv_async without a session_id raises a ValueError."""
823+
# GIVEN a Grid without a session_id
824+
grid = Grid()
825+
826+
# WHEN I call import_csv_async
827+
# THEN it should raise ValueError
828+
with pytest.raises(
829+
ValueError,
830+
match="session_id is required to import a CSV into a GridSession",
831+
):
832+
await grid.import_csv_async(
833+
synapse_client=self.syn, file_handle_id="1234567"
834+
)
835+
836+
async def test_import_csv_async(self) -> None:
837+
"""Test the import_csv_async method of the Grid class, ensuring it correctly calls the preview and import requests and logs the results."""
838+
# GIVEN a Grid with a session_id
839+
grid = Grid(session_id=SESSION_ID)
840+
841+
# Mock the CreateGridRequest's send_job_and_wait_async
842+
csv_table_descriptor = CsvTableDescriptor(
843+
separator=",",
844+
quote_character='"',
845+
escape_character="\\",
846+
line_end=os.linesep,
847+
is_first_line_header=True,
848+
)
849+
mock_preview = UploadToTablePreviewRequest(
850+
csv_table_descriptor=csv_table_descriptor,
851+
suggested_columns=[
852+
Column(name="col1", column_type="STRING", maximum_size=50)
853+
],
854+
sample_rows=[["value1"]],
855+
rows_scanned=1,
856+
)
857+
mock_import = GridCsvImportRequest(
858+
file_handle_id="1234567",
859+
csv_descriptor=csv_table_descriptor,
860+
schema=UploadToTablePreviewRequest.suggested_columns,
861+
total_count=1,
862+
created_count=1,
863+
updated_count=1,
864+
)
865+
866+
# WHEN I call import_csv_async
867+
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+
),
880+
patch.object(self.syn, "logger") as mock_logger,
881+
):
882+
result = await grid.import_csv_async(
883+
synapse_client=self.syn, file_handle_id="1234567"
884+
)
885+
886+
# THEN the grid should be populated with session data
887+
assert result.session_id == SESSION_ID
888+
889+
# AND the log message should contain the import counts
890+
mock_logger.info.assert_called_once()
891+
log_message = mock_logger.info.call_args[0][0]
892+
assert "total count: 1" in log_message
893+
assert "total created: 1" in log_message
894+
assert "total updated: 1" in log_message
895+
820896

821897
class TestCreateGridRequest:
822898
"""Tests for the CreateGridRequest helper dataclass."""
@@ -904,7 +980,7 @@ def test_fill_from_dict(self) -> None:
904980
name="Diagnosis", column_type="STRING", maximum_size=7
905981
)
906982
assert preview_response.suggested_columns[4] == Column(
907-
name="PatientID", column_type="INTEGER", max_size=None
983+
name="PatientID", column_type="INTEGER", maximum_size=None
908984
)
909985
assert preview_response.sample_rows == [
910986
[None, "Female", "test", "Healthy", "1", None, None, None]

0 commit comments

Comments
 (0)