1
- """ Tests for runpod | serverless | modules | download.py """
1
+ """Tests for runpod | serverless | modules | download.py"""
2
2
3
3
# pylint: disable=R0903,W0613
4
4
17
17
URL_LIST = [
18
18
"https://example.com/picture.jpg" ,
19
19
"https://example.com/picture.jpg?X-Amz-Signature=123" ,
20
+ "https://example.com/file_without_extension" ,
20
21
]
21
22
22
23
JOB_ID = "job_123"
@@ -75,9 +76,7 @@ def test_calculate_chunk_size(self):
75
76
self .assertEqual (calculate_chunk_size (1024 ), 1024 )
76
77
self .assertEqual (calculate_chunk_size (1024 * 1024 ), 1024 )
77
78
self .assertEqual (calculate_chunk_size (1024 * 1024 * 1024 ), 1024 * 1024 )
78
- self .assertEqual (
79
- calculate_chunk_size (1024 * 1024 * 1024 * 10 ), 1024 * 1024 * 10
80
- )
79
+ self .assertEqual (calculate_chunk_size (1024 * 1024 * 1024 * 10 ), 1024 * 1024 * 10 )
81
80
82
81
@patch ("os.makedirs" , return_value = None )
83
82
@patch ("runpod.http_client.SyncClientSession.get" , side_effect = mock_requests_get )
@@ -86,29 +85,26 @@ def test_download_files_from_urls(self, mock_open_file, mock_get, mock_makedirs)
86
85
"""
87
86
Tests download_files_from_urls
88
87
"""
88
+ urls = ["https://example.com/picture.jpg" , "https://example.com/file_without_extension" ]
89
89
downloaded_files = download_files_from_urls (
90
90
JOB_ID ,
91
- [
92
- "https://example.com/picture.jpg" ,
93
- ],
91
+ urls ,
94
92
)
95
93
96
- self .assertEqual (len (downloaded_files ), 1 )
94
+ self .assertEqual (len (downloaded_files ), len ( urls ) )
97
95
98
- # Check that the url was called with SyncClientSession.get
99
- self .assertIn ("https://example.com/picture.jpg" , mock_get .call_args_list [0 ][0 ])
96
+ for index , url in enumerate (urls ):
97
+ # Check that the url was called with SyncClientSession.get
98
+ self .assertIn (url , mock_get .call_args_list [index ][0 ])
100
99
101
- # Check that the file has the correct extension
102
- self .assertTrue (downloaded_files [0 ].endswith (".jpg" ))
100
+ # Check that the file has the correct extension
101
+ self .assertTrue (downloaded_files [index ].endswith (".jpg" ))
103
102
104
- mock_open_file .assert_called_once_with (downloaded_files [0 ], "wb" )
105
- mock_makedirs .assert_called_once_with (
106
- os .path .abspath (f"jobs/{ JOB_ID } /downloaded_files" ), exist_ok = True
107
- )
103
+ mock_open_file .assert_any_call (downloaded_files [index ], "wb" )
108
104
109
- string_download_file = download_files_from_urls (
110
- JOB_ID , "https://example.com/picture.jpg"
111
- )
105
+ mock_makedirs . assert_called_once_with ( os . path . abspath ( f"jobs/ { JOB_ID } /downloaded_files" ), exist_ok = True )
106
+
107
+ string_download_file = download_files_from_urls ( JOB_ID , "https://example.com/picture.jpg" )
112
108
self .assertTrue (string_download_file [0 ].endswith (".jpg" ))
113
109
114
110
# Check if None is returned when url is None
@@ -124,9 +120,7 @@ def test_download_files_from_urls(self, mock_open_file, mock_get, mock_makedirs)
124
120
@patch ("os.makedirs" , return_value = None )
125
121
@patch ("runpod.http_client.SyncClientSession.get" , side_effect = mock_requests_get )
126
122
@patch ("builtins.open" , new_callable = mock_open )
127
- def test_download_files_from_urls_signed (
128
- self , mock_open_file , mock_get , mock_makedirs
129
- ):
123
+ def test_download_files_from_urls_signed (self , mock_open_file , mock_get , mock_makedirs ):
130
124
"""
131
125
Tests download_files_from_urls with signed urls
132
126
"""
@@ -147,9 +141,7 @@ def test_download_files_from_urls_signed(
147
141
self .assertTrue (downloaded_files [0 ].endswith (".jpg" ))
148
142
149
143
mock_open_file .assert_called_once_with (downloaded_files [0 ], "wb" )
150
- mock_makedirs .assert_called_once_with (
151
- os .path .abspath (f"jobs/{ JOB_ID } /downloaded_files" ), exist_ok = True
152
- )
144
+ mock_makedirs .assert_called_once_with (os .path .abspath (f"jobs/{ JOB_ID } /downloaded_files" ), exist_ok = True )
153
145
154
146
155
147
class FileDownloaderTestCase (unittest .TestCase ):
@@ -179,6 +171,30 @@ def test_download_file(self, mock_file, mock_get):
179
171
# Check that the file was written correctly
180
172
mock_file ().write .assert_called_once_with (b"file content" )
181
173
174
+ @patch ("runpod.serverless.utils.rp_download.SyncClientSession.get" )
175
+ @patch ("builtins.open" , new_callable = mock_open )
176
+ def test_download_file (self , mock_file , mock_get ):
177
+ """
178
+ Tests download_file using filename from Content-Disposition
179
+ """
180
+ # Mock the response from SyncClientSession.get
181
+ mock_response = MagicMock ()
182
+ mock_response .content = b"file content"
183
+ mock_response .headers = {"Content-Disposition" : 'inline; filename="test_file.txt"' }
184
+ mock_get .return_value = mock_response
185
+
186
+ # Call the function with a test URL
187
+ result = file ("http://test.com/file_without_extension" )
188
+
189
+ # Check the result
190
+ self .assertEqual (result ["type" ], "txt" )
191
+ self .assertEqual (result ["original_name" ], "test_file.txt" )
192
+ self .assertTrue (result ["file_path" ].endswith (".txt" ))
193
+ self .assertIsNone (result ["extracted_path" ])
194
+
195
+ # Check that the file was written correctly
196
+ mock_file ().write .assert_called_once_with (b"file content" )
197
+
182
198
@patch ("runpod.serverless.utils.rp_download.SyncClientSession.get" )
183
199
@patch ("builtins.open" , new_callable = mock_open )
184
200
@patch ("runpod.serverless.utils.rp_download.zipfile.ZipFile" )
0 commit comments