|
9 | 9 | import pytest |
10 | 10 |
|
11 | 11 | from synapseclient import Synapse |
12 | | -from synapseclient.models import Grid, Project, RecordSet |
| 12 | +from synapseclient.models import File, Grid, Project, RecordSet |
13 | 13 | from tests.integration import ASYNC_JOB_TIMEOUT_SEC |
14 | 14 |
|
15 | 15 |
|
@@ -183,3 +183,65 @@ async def test_delete_grid_session_validation_error_async(self) -> None: |
183 | 183 | match="session_id is required to delete a GridSession", |
184 | 184 | ): |
185 | 185 | 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