@@ -41,17 +41,17 @@ def test_get_files_for_doi(self, mock_data_api, mock_native_api, mock_config):
4141 self .assertEqual (len (result ), 1 )
4242 self .assertEqual (result [0 ]["dataFile" ]["id" ], 2222 )
4343
44- def test_get_dvfile_path (self ):
44+ def test_get_remote_path (self ):
4545 dvfile = {
4646 "dataFile" : {
4747 "filename" : "data.txt"
4848 }
4949 }
50- result = API .get_dvfile_path (dvfile , parent_dir = "/tmp" )
51- self .assertEqual (result , "/tmp/ data.txt" )
50+ result = API .get_remote_path (dvfile )
51+ self .assertEqual (result , "data.txt" )
5252 dvfile ["directoryLabel" ] = "results"
53- result = API .get_dvfile_path (dvfile , parent_dir = "/tmp" )
54- self .assertEqual (result , "/tmp/ results/data.txt" )
53+ result = API .get_remote_path (dvfile )
54+ self .assertEqual (result , "results/data.txt" )
5555
5656 @patch ('dva.api.Config' )
5757 @patch ('dva.api.NativeApi' )
@@ -62,9 +62,35 @@ def test_download_file(self, mock_data_api, mock_native_api, mock_config):
6262 "id" : 2222
6363 }
6464 }
65+ mock_data_api .return_value .get_datafile .return_value = Mock (headers = {
66+ 'Content-disposition' : 'attachment; filename="data.txt"'
67+ })
6568 api = get_api (url = None )
6669 with patch ("builtins.open" , mock_open ()) as mock_file :
67- api .download_file (dvfile , path = "/tmp/data.txt" )
70+ api .download_file (dvfile , dest = "/tmp" )
71+ mock_data_api .return_value .get_datafile .assert_called_with (2222 , data_format = None )
72+ mock_file .assert_called_with ("/tmp/data.txt" , "wb" )
73+ mock_file .return_value .write .assert_called_with (
74+ mock_data_api .return_value .get_datafile .return_value .content
75+ )
76+
77+ @patch ('dva.api.Config' )
78+ @patch ('dva.api.NativeApi' )
79+ @patch ('dva.api.DataAccessApi' )
80+ def test_download_file_original (self , mock_data_api , mock_native_api , mock_config ):
81+ dvfile = {
82+ "dataFile" : {
83+ "id" : 2222 ,
84+ "originalFileFormat" : "CSV"
85+ }
86+ }
87+ mock_data_api .return_value .get_datafile .return_value = Mock (headers = {
88+ 'Content-disposition' : 'attachment; filename="data.txt"'
89+ })
90+ api = get_api (url = None )
91+ with patch ("builtins.open" , mock_open ()) as mock_file :
92+ api .download_file (dvfile , dest = "/tmp" )
93+ mock_data_api .return_value .get_datafile .assert_called_with (2222 , data_format = 'original' )
6894 mock_file .assert_called_with ("/tmp/data.txt" , "wb" )
6995 mock_file .return_value .write .assert_called_with (
7096 mock_data_api .return_value .get_datafile .return_value .content
@@ -108,3 +134,14 @@ def test_verify_checksum(self, mock_datafile, mock_data_api, mock_native_api, mo
108134 with patch ("builtins.open" , mock_open (read_data = b"123" )) as mock_file :
109135 api .upload_file (doi = 'doi:10.70122/FK2/WUU4DM' , path = "/tmp/data.txt" )
110136 self .assertEqual (str (raised_exception .exception ), "Uploading failed with status bad." )
137+
138+ def test_get_download_filename (self ):
139+ good_values = [
140+ ('attachment; filename=bob.txt' , 'bob.txt' ),
141+ ('attachment; filename="tom.txt"' , 'tom.txt' ),
142+ ('attachment; filename="file1.txt"; other="a"' , 'file1.txt' ),
143+ ]
144+ response = Mock ()
145+ for content_disposition , expected_result in good_values :
146+ response .headers = {'Content-disposition' : content_disposition }
147+ self .assertEqual (expected_result , API .get_download_filename (response ))
0 commit comments