Skip to content

Commit f8af1ad

Browse files
author
Lingling Peng
committed
add integration test
1 parent bf262a9 commit f8af1ad

3 files changed

Lines changed: 68 additions & 15 deletions

File tree

docs/reference/experimental/async/curator.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ at your own risk.
5656
members:
5757
- create_async
5858
- export_to_record_set_async
59+
- import_csv_async
5960
---
6061
[](){ #query-reference-async }
6162
::: synapseclient.models.Query

synapseclient/models/curation.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@
3838
from synapseclient.core.utils import delete_none_keys, merge_dataclass_entities
3939
from synapseclient.models.mixins.asynchronous_job import AsynchronousCommunicator
4040
from synapseclient.models.recordset import ValidationSummary
41-
from synapseclient.models.table_components import (
42-
Column,
43-
ColumnType,
44-
CsvTableDescriptor,
45-
Query,
46-
)
41+
from synapseclient.models.table_components import Column, CsvTableDescriptor, Query
4742

4843

4944
@dataclass
@@ -2106,15 +2101,10 @@ async def main():
21062101
)
21072102

21082103
preview_response = await upload_to_table_preview.send_job_and_wait_async(
2109-
timeout=timeout
2104+
timeout=timeout, synapse_client=synapse_client
21102105
)
21112106

2112-
# ROW_ID and ROW_VERSION must be prepended to the schema to avoid:
2113-
# SynapseHTTPError: 400 Client Error: The CSV header does not match the schema size.
2114-
all_columns = [
2115-
Column(name="ROW_ID", column_type=ColumnType.STRING),
2116-
Column(name="ROW_VERSION", column_type=ColumnType.STRING),
2117-
] + preview_response.suggested_columns
2107+
all_columns = preview_response.suggested_columns
21182108
import_request = GridCsvImportRequest(
21192109
session_id=self.session_id,
21202110
file_handle_id=file_handle_id,
@@ -2123,7 +2113,7 @@ async def main():
21232113
if csv_table_descriptor:
21242114
import_request.csv_descriptor = csv_table_descriptor
21252115
import_response = await import_request.send_job_and_wait_async(
2126-
synapse_client=synapse_client, timeout=timeout
2116+
timeout=timeout, synapse_client=synapse_client
21272117
)
21282118
client = Synapse.get_client(synapse_client=synapse_client)
21292119
client.logger.info(

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
from synapseclient import Synapse
12-
from synapseclient.models import Grid, Project, RecordSet
12+
from synapseclient.models import File, Grid, Project, RecordSet
1313
from tests.integration import ASYNC_JOB_TIMEOUT_SEC
1414

1515

@@ -183,3 +183,65 @@ async def test_delete_grid_session_validation_error_async(self) -> None:
183183
match="session_id is required to delete a GridSession",
184184
):
185185
await grid.delete_async(synapse_client=self.syn)
186+
187+
async def test_import_csv_to_grid_session_async(
188+
self, record_set_fixture: RecordSet, schedule_for_cleanup: Callable[..., None]
189+
) -> None:
190+
"""Test importing a CSV file into a grid session."""
191+
192+
# GIVEN: Create a grid session first
193+
grid = Grid(record_set_id=record_set_fixture.id)
194+
created_grid = await grid.create_async(
195+
timeout=ASYNC_JOB_TIMEOUT_SEC, synapse_client=self.syn
196+
)
197+
198+
assert created_grid.session_id is not None
199+
200+
# AND a CSV file uploaded to Synapse
201+
test_data = pd.DataFrame(
202+
{
203+
"id": [6, 7, 8, 9, 10],
204+
"name": ["Alpha", "Beta", "Gamma", "Delta", "Epsilon"],
205+
"value": [10.5, 20.3, 30.7, 40.1, 50.9],
206+
"category": ["A", "B", "A", "C", "B"],
207+
"active": [True, False, False, True, True],
208+
}
209+
)
210+
211+
project_name = str(uuid.uuid4())
212+
project = Project(name=project_name)
213+
project = await project.store_async(synapse_client=self.syn)
214+
215+
# Create a temporary CSV file
216+
with tempfile.NamedTemporaryFile("w", suffix=".csv", delete=False) as temp_csv:
217+
test_data.to_csv(temp_csv.name, index=False)
218+
self.schedule_for_cleanup(temp_csv.name)
219+
220+
file = await File(path=temp_csv.name, parent_id=project.id).store_async(
221+
synapse_client=self.syn
222+
)
223+
224+
# WHEN: Importing the CSV into the grid session
225+
imported_grid = await created_grid.import_csv_async(
226+
file_handle_id=file.file_handle.id,
227+
timeout=ASYNC_JOB_TIMEOUT_SEC,
228+
synapse_client=self.syn,
229+
)
230+
231+
schedule_for_cleanup(file.id)
232+
schedule_for_cleanup(project.id)
233+
234+
# THEN: The import should complete and return the Grid with the same session
235+
assert imported_grid.session_id == created_grid.session_id
236+
237+
# WHEN: Exporting the grid back to the record set
238+
exported_grid = await imported_grid.export_to_record_set_async(
239+
timeout=ASYNC_JOB_TIMEOUT_SEC, synapse_client=self.syn
240+
)
241+
242+
# THEN: The export should contain 10 total rows
243+
# (5 from the original record set + 5 imported)
244+
assert exported_grid.validation_summary_statistics is not None
245+
assert (
246+
exported_grid.validation_summary_statistics.total_number_of_children == 10
247+
)

0 commit comments

Comments
 (0)