|
8 | 8 | from synapseclient.core.constants.concrete_types import ( |
9 | 9 | FILE_BASED_METADATA_TASK_PROPERTIES, |
10 | 10 | RECORD_BASED_METADATA_TASK_PROPERTIES, |
| 11 | + UPLOAD_TO_TABLE_PREVIEW_REQUEST, |
11 | 12 | ) |
12 | 13 | from synapseclient.models.curation import ( |
13 | 14 | CreateGridRequest, |
|
16 | 17 | Grid, |
17 | 18 | GridRecordSetExportRequest, |
18 | 19 | RecordBasedMetadataTaskProperties, |
| 20 | + UploadToTablePreviewRequest, |
19 | 21 | _create_task_properties_from_dict, |
20 | 22 | ) |
21 | 23 | from synapseclient.models.recordset import ValidationSummary |
| 24 | +from synapseclient.models.table_components import Column, CsvTableDescriptor |
22 | 25 |
|
23 | 26 | TASK_ID = 42 |
24 | 27 | TASK_ID_2 = 99 |
@@ -856,6 +859,86 @@ def test_to_synapse_request_with_record_set_id(self) -> None: |
856 | 859 | assert "initialQuery" not in result |
857 | 860 |
|
858 | 861 |
|
| 862 | +class TestUploadToTablePreviewRequest: |
| 863 | + """Tests for the UploadToTablePreviewRequest helper dataclass.""" |
| 864 | + |
| 865 | + def test_fill_from_dict(self) -> None: |
| 866 | + # GIVEN a response with upload to table preview data |
| 867 | + raw_synapse_response = { |
| 868 | + "jobId": "1234", |
| 869 | + "concreteType": "org.sagebionetworks.repo.model.table.UploadToTablePreviewResult", |
| 870 | + "suggestedColumns": [ |
| 871 | + {"name": "etag", "columnType": "STRING", "maximumSize": 50}, |
| 872 | + {"name": "Sex", "columnType": "STRING", "maximumSize": 6}, |
| 873 | + {"name": "Component", "columnType": "STRING", "maximumSize": 4}, |
| 874 | + {"name": "Diagnosis", "columnType": "STRING", "maximumSize": 7}, |
| 875 | + {"name": "PatientID", "columnType": "INTEGER"}, |
| 876 | + {"name": "CancerType", "columnType": "STRING", "maximumSize": 50}, |
| 877 | + {"name": "YearofBirth", "columnType": "STRING", "maximumSize": 50}, |
| 878 | + {"name": "FamilyHistory", "columnType": "STRING", "maximumSize": 50}, |
| 879 | + ], |
| 880 | + "sampleRows": [ |
| 881 | + {"values": [None, "Female", "test", "Healthy", "1", None, None, None]} |
| 882 | + ], |
| 883 | + "rowsScanned": 1, |
| 884 | + } |
| 885 | + |
| 886 | + # WHEN I fill an UploadToTablePreviewRequest from the response |
| 887 | + preview_req = UploadToTablePreviewRequest() |
| 888 | + preview_response = preview_req.fill_from_dict(raw_synapse_response) |
| 889 | + |
| 890 | + # THEN the fields should be populated correctly |
| 891 | + assert len(preview_response.suggested_columns) == 8 |
| 892 | + assert preview_response.suggested_columns[0] == Column( |
| 893 | + name="etag", column_type="STRING", maximum_size=50 |
| 894 | + ) |
| 895 | + assert preview_response.suggested_columns[1] == Column( |
| 896 | + name="Sex", column_type="STRING", maximum_size=6 |
| 897 | + ) |
| 898 | + assert preview_response.suggested_columns[2] == Column( |
| 899 | + name="Component", column_type="STRING", maximum_size=4 |
| 900 | + ) |
| 901 | + assert preview_response.suggested_columns[3] == Column( |
| 902 | + name="Diagnosis", column_type="STRING", maximum_size=7 |
| 903 | + ) |
| 904 | + assert preview_response.suggested_columns[4] == Column( |
| 905 | + name="PatientID", column_type="INTEGER" |
| 906 | + ) |
| 907 | + assert preview_response.sample_rows == [ |
| 908 | + [None, "Female", "test", "Healthy", "1", None, None, None] |
| 909 | + ] |
| 910 | + assert preview_response.rows_scanned == 1 |
| 911 | + |
| 912 | + def test_to_synapse_request(self) -> None: |
| 913 | + # GIVEN an UploadToTablePreviewRequest |
| 914 | + preview_req = UploadToTablePreviewRequest( |
| 915 | + upload_file_handle_id="1234567", |
| 916 | + lines_to_skip=1, |
| 917 | + do_full_file_scan=True, |
| 918 | + csv_table_descriptor=CsvTableDescriptor( |
| 919 | + separator=";", |
| 920 | + quote_character='"', |
| 921 | + escape_character="\\", |
| 922 | + line_end="\n", |
| 923 | + is_first_line_header=True, |
| 924 | + ), |
| 925 | + ) |
| 926 | + |
| 927 | + # WHEN I convert it to a synapse request |
| 928 | + result = preview_req.to_synapse_request() |
| 929 | + |
| 930 | + # THEN it should contain the correct fields |
| 931 | + assert result["concreteType"] == UPLOAD_TO_TABLE_PREVIEW_REQUEST |
| 932 | + assert result["uploadFileHandleId"] == "1234567" |
| 933 | + assert result["linesToSkip"] == 1 |
| 934 | + assert result["doFullFileScan"] is True |
| 935 | + assert result["csvTableDescriptor"]["separator"] == ";" |
| 936 | + assert result["csvTableDescriptor"]["quoteCharacter"] == '"' |
| 937 | + assert result["csvTableDescriptor"]["escapeCharacter"] == "\\" |
| 938 | + assert result["csvTableDescriptor"]["lineEnd"] == "\n" |
| 939 | + assert result["csvTableDescriptor"]["isFirstLineHeader"] is True |
| 940 | + |
| 941 | + |
859 | 942 | class TestGridRecordSetExportRequest: |
860 | 943 | """Tests for the GridRecordSetExportRequest helper dataclass.""" |
861 | 944 |
|
|
0 commit comments