|
1 | 1 | """Unit tests for the CurationTask and Grid models.""" |
2 | 2 |
|
3 | 3 | import os |
4 | | -from unittest.mock import AsyncMock, patch |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
@@ -838,55 +838,79 @@ async def test_import_csv_async(self) -> None: |
838 | 838 | # GIVEN a Grid with a session_id |
839 | 839 | grid = Grid(session_id=SESSION_ID) |
840 | 840 |
|
841 | | - # Mock the CreateGridRequest's send_job_and_wait_async |
842 | 841 | csv_table_descriptor = CsvTableDescriptor( |
843 | 842 | separator=",", |
844 | 843 | quote_character='"', |
845 | 844 | escape_character="\\", |
846 | 845 | line_end=os.linesep, |
847 | 846 | is_first_line_header=True, |
848 | 847 | ) |
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( |
850 | 852 | 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, |
854 | 854 | sample_rows=[["value1"]], |
855 | 855 | rows_scanned=1, |
856 | 856 | ) |
857 | | - mock_import = GridCsvImportRequest( |
| 857 | + # Mock import response with row counts |
| 858 | + mock_import_response = GridCsvImportRequest( |
858 | 859 | file_handle_id="1234567", |
859 | | - csv_descriptor=csv_table_descriptor, |
860 | | - schema=UploadToTablePreviewRequest.suggested_columns, |
861 | 860 | total_count=1, |
862 | 861 | created_count=1, |
863 | 862 | updated_count=1, |
864 | 863 | ) |
865 | 864 |
|
| 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 | + |
866 | 879 | # WHEN I call import_csv_async |
867 | 880 | 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, |
880 | 889 | patch.object(self.syn, "logger") as mock_logger, |
881 | 890 | ): |
882 | 891 | 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, |
884 | 895 | ) |
885 | 896 |
|
886 | | - # THEN the grid should be populated with session data |
| 897 | + # THEN the grid is returned with the same session |
887 | 898 | assert result.session_id == SESSION_ID |
888 | 899 |
|
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 |
890 | 914 | mock_logger.info.assert_called_once() |
891 | 915 | log_message = mock_logger.info.call_args[0][0] |
892 | 916 | assert "total count: 1" in log_message |
|
0 commit comments