|
1 | 1 | """Unit tests for the CurationTask and Grid models.""" |
2 | 2 |
|
| 3 | +import os |
3 | 4 | from unittest.mock import AsyncMock, patch |
4 | 5 |
|
5 | 6 | import pytest |
@@ -817,6 +818,81 @@ async def mock_list(*args, **kwargs): |
817 | 818 | assert len(results) == 1 |
818 | 819 | assert results[0].session_id == SESSION_ID |
819 | 820 |
|
| 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 | + |
820 | 896 |
|
821 | 897 | class TestCreateGridRequest: |
822 | 898 | """Tests for the CreateGridRequest helper dataclass.""" |
@@ -904,7 +980,7 @@ def test_fill_from_dict(self) -> None: |
904 | 980 | name="Diagnosis", column_type="STRING", maximum_size=7 |
905 | 981 | ) |
906 | 982 | 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 |
908 | 984 | ) |
909 | 985 | assert preview_response.sample_rows == [ |
910 | 986 | [None, "Female", "test", "Healthy", "1", None, None, None] |
|
0 commit comments