Skip to content

Commit 36e95aa

Browse files
committed
Switched to put
1 parent b76b20e commit 36e95aa

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

lf_toolkit/evaluation/image_upload.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def upload_image(img: Image.Image, mime_type: str) -> Dict:
142142
buffer.seek(0)
143143

144144
files: Dict[str, tuple] = {'file': (filename, buffer, mime_type)}
145-
response: requests.Response = requests.post(url, files=files, timeout=30)
145+
response: requests.Response = requests.put(url, files=files, timeout=30)
146146

147147
if response.status_code != 200:
148148
raise ImageUploadError(
@@ -158,3 +158,9 @@ def upload_image(img: Image.Image, mime_type: str) -> Dict:
158158
except Exception as e:
159159
raise ImageUploadError(f"Unexpected error: {str(e)}")
160160

161+
if __name__ == "__main__":
162+
img = Image.new('RGB', (100, 100), color='red')
163+
img.format = 'JPEG'
164+
165+
# Execute
166+
result = upload_image(img, 'image/jpeg')

tests/evaluation/image_upload_test.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ def test_get_s3_bucket_uri_empty_string(self):
184184
class TestUploadImage:
185185
"""Test suite for upload_image function"""
186186

187-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
187+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
188188
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
189189
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
190-
def test_successful_upload(self, mock_uuid, mock_getenv, mock_post):
190+
def test_successful_upload(self, mock_uuid, mock_getenv, mock_put):
191191
"""Test successful image upload with UUID-based filename"""
192192
# Setup mocks
193193
mock_uuid.return_value = uuid.UUID('12345678-1234-5678-1234-567812345678')
@@ -196,7 +196,7 @@ def test_successful_upload(self, mock_uuid, mock_getenv, mock_post):
196196
mock_response = Mock()
197197
mock_response.status_code = 200
198198
mock_response.json.return_value = {'url': f'https://s3.amazonaws.com/uploaded-image.jpg'}
199-
mock_post.return_value = mock_response
199+
mock_put.return_value = mock_response
200200

201201
# Create a real PIL image for testing
202202
img = Image.new('RGB', (100, 100), color='red')
@@ -207,27 +207,27 @@ def test_successful_upload(self, mock_uuid, mock_getenv, mock_post):
207207

208208
# Verify response
209209
assert result == 'https://s3.amazonaws.com/uploaded-image.jpg'
210-
assert mock_post.called
211-
assert mock_post.call_args[1]['timeout'] == 30
210+
assert mock_put.called
211+
assert mock_put.call_args[1]['timeout'] == 30
212212

213213
# Verify UUID-based filename is used
214-
call_args = mock_post.call_args
214+
call_args = mock_put.call_args
215215
filename, file_obj, mime_type = call_args[1]['files']['file']
216216
assert filename == '12345678-1234-5678-1234-567812345678.jpeg'
217217
assert mime_type == 'image/jpeg'
218218

219-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
219+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
220220
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
221221
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
222-
def test_upload_with_png_image(self, mock_uuid, mock_getenv, mock_post):
222+
def test_upload_with_png_image(self, mock_uuid, mock_getenv, mock_put):
223223
"""Test uploading PNG image with UUID-based filename"""
224224
mock_uuid.return_value = uuid.UUID('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee')
225225
mock_getenv.return_value = 'https://storage.example.com'
226226

227227
mock_response = Mock()
228228
mock_response.status_code = 200
229229
mock_response.json.return_value = {'url': 'https://storage.example.com/image.png'}
230-
mock_post.return_value = mock_response
230+
mock_put.return_value = mock_response
231231

232232
img = Image.new('RGBA', (50, 50), color=(0, 255, 0, 128))
233233
img.format = 'PNG'
@@ -237,7 +237,7 @@ def test_upload_with_png_image(self, mock_uuid, mock_getenv, mock_post):
237237
assert result == 'https://storage.example.com/image.png'
238238

239239
# Verify UUID-based filename is used
240-
call_args = mock_post.call_args
240+
call_args = mock_put.call_args
241241
filename, file_obj, mime_type = call_args[1]['files']['file']
242242
assert filename == 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee.png'
243243
assert mime_type == 'image/png'
@@ -264,18 +264,18 @@ def test_upload_invalid_mime_type(self, mock_getenv):
264264
with pytest.raises(InvalidMimeTypeError):
265265
upload_image(img, 'image/invalid')
266266

267-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
267+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
268268
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
269269
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
270-
def test_upload_server_error(self, mock_uuid, mock_getenv, mock_post):
270+
def test_upload_server_error(self, mock_uuid, mock_getenv, mock_put):
271271
"""Test upload fails when server returns error"""
272272
mock_uuid.return_value = uuid.UUID('12345678-1234-5678-1234-567812345678')
273273
mock_getenv.return_value = 'https://s3.amazonaws.com/bucket'
274274

275275
mock_response = Mock()
276276
mock_response.status_code = 500
277277
mock_response.text = 'Internal Server Error'
278-
mock_post.return_value = mock_response
278+
mock_put.return_value = mock_response
279279

280280
img = Image.new('RGB', (100, 100))
281281
img.format = 'JPEG'
@@ -285,15 +285,15 @@ def test_upload_server_error(self, mock_uuid, mock_getenv, mock_post):
285285

286286
assert "Upload failed with status code 500" in str(exc_info.value)
287287

288-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
288+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
289289
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
290290
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
291-
def test_upload_network_error(self, mock_uuid, mock_getenv, mock_post):
291+
def test_upload_network_error(self, mock_uuid, mock_getenv, mock_put):
292292
"""Test upload fails on network error"""
293293
mock_uuid.return_value = uuid.UUID('12345678-1234-5678-1234-567812345678')
294294
mock_getenv.return_value = 'https://s3.amazonaws.com/bucket'
295295

296-
mock_post.side_effect = requests.exceptions.ConnectionError('Connection failed')
296+
mock_put.side_effect = requests.exceptions.ConnectionError('Connection failed')
297297

298298
img = Image.new('RGB', (100, 100))
299299
img.format = 'JPEG'
@@ -303,15 +303,15 @@ def test_upload_network_error(self, mock_uuid, mock_getenv, mock_post):
303303

304304
assert "Network error" in str(exc_info.value)
305305

306-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
306+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
307307
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
308308
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
309-
def test_upload_timeout_error(self, mock_uuid, mock_getenv, mock_post):
309+
def test_upload_timeout_error(self, mock_uuid, mock_getenv, mock_put):
310310
"""Test upload fails on timeout"""
311311
mock_uuid.return_value = uuid.UUID('12345678-1234-5678-1234-567812345678')
312312
mock_getenv.return_value = 'https://s3.amazonaws.com/bucket'
313313

314-
mock_post.side_effect = requests.exceptions.Timeout('Request timed out')
314+
mock_put.side_effect = requests.exceptions.Timeout('Request timed out')
315315

316316
img = Image.new('RGB', (100, 100))
317317
img.format = 'JPEG'
@@ -321,10 +321,10 @@ def test_upload_timeout_error(self, mock_uuid, mock_getenv, mock_post):
321321

322322
assert "Network error" in str(exc_info.value)
323323

324-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
324+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
325325
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
326326
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
327-
def test_upload_mime_type_mismatch(self, mock_uuid, mock_getenv, mock_post):
327+
def test_upload_mime_type_mismatch(self, mock_uuid, mock_getenv, mock_put):
328328
"""Test upload fails when MIME type doesn't match image format"""
329329
mock_uuid.return_value = uuid.UUID('12345678-1234-5678-1234-567812345678')
330330
mock_getenv.return_value = 'https://s3.amazonaws.com/bucket'
@@ -335,18 +335,18 @@ def test_upload_mime_type_mismatch(self, mock_uuid, mock_getenv, mock_post):
335335
with pytest.raises(InvalidMimeTypeError):
336336
upload_image(img, 'image/jpeg')
337337

338-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
338+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
339339
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
340340
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
341-
def test_upload_image_no_format(self, mock_uuid, mock_getenv, mock_post):
341+
def test_upload_image_no_format(self, mock_uuid, mock_getenv, mock_put):
342342
"""Test upload with image that has no format (defaults to PNG) uses UUID filename"""
343343
mock_uuid.return_value = uuid.UUID('12345678-1234-5678-1234-567812345678')
344344
mock_getenv.return_value = 'https://s3.amazonaws.com/bucket'
345345

346346
mock_response = Mock()
347347
mock_response.status_code = 200
348348
mock_response.json.return_value = {'url': 'https://s3.amazonaws.com/image.png'}
349-
mock_post.return_value = mock_response
349+
mock_put.return_value = mock_response
350350

351351
img = Image.new('RGB', (100, 100))
352352
img.format = None
@@ -356,22 +356,22 @@ def test_upload_image_no_format(self, mock_uuid, mock_getenv, mock_post):
356356
assert result == 'https://s3.amazonaws.com/image.png'
357357

358358
# Verify UUID-based filename with default .png extension
359-
call_args = mock_post.call_args
359+
call_args = mock_put.call_args
360360
filename, file_obj, mime_type = call_args[1]['files']['file']
361361
assert filename == '12345678-1234-5678-1234-567812345678.png'
362362
assert mime_type == 'image/png'
363363

364-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
364+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
365365
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
366366
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
367-
def test_upload_uses_different_uuid_each_time(self, mock_uuid, mock_getenv, mock_post):
367+
def test_upload_uses_different_uuid_each_time(self, mock_uuid, mock_getenv, mock_put):
368368
"""Test that each upload generates a unique UUID-based filename"""
369369
mock_getenv.return_value = 'https://s3.amazonaws.com/bucket'
370370

371371
mock_response = Mock()
372372
mock_response.status_code = 200
373373
mock_response.json.return_value = {'url': 'https://s3.amazonaws.com/uploaded.jpg'}
374-
mock_post.return_value = mock_response
374+
mock_put.return_value = mock_response
375375

376376
# First upload with first UUID
377377
uuid1 = uuid.UUID('11111111-1111-1111-1111-111111111111')
@@ -381,7 +381,7 @@ def test_upload_uses_different_uuid_each_time(self, mock_uuid, mock_getenv, mock
381381
img1.format = 'JPEG'
382382
upload_image(img1, 'image/jpeg')
383383

384-
filename1 = mock_post.call_args[1]['files']['file'][0]
384+
filename1 = mock_put.call_args[1]['files']['file'][0]
385385

386386
# Second upload with different UUID
387387
uuid2 = uuid.UUID('22222222-2222-2222-2222-222222222222')
@@ -391,33 +391,33 @@ def test_upload_uses_different_uuid_each_time(self, mock_uuid, mock_getenv, mock
391391
img2.format = 'JPEG'
392392
upload_image(img2, 'image/jpeg')
393393

394-
filename2 = mock_post.call_args[1]['files']['file'][0]
394+
filename2 = mock_put.call_args[1]['files']['file'][0]
395395

396396
# Verify different UUIDs result in different filenames
397397
assert filename1 == '11111111-1111-1111-1111-111111111111.jpeg'
398398
assert filename2 == '22222222-2222-2222-2222-222222222222.jpeg'
399399
assert filename1 != filename2
400400

401-
@patch('lf_toolkit.evaluation.image_upload.requests.post')
401+
@patch('lf_toolkit.evaluation.image_upload.requests.put')
402402
@patch('lf_toolkit.evaluation.image_upload.os.getenv')
403403
@patch('lf_toolkit.evaluation.image_upload.uuid.uuid4')
404-
def test_upload_verifies_correct_file_uploaded(self, mock_uuid, mock_getenv, mock_post):
404+
def test_upload_verifies_correct_file_uploaded(self, mock_uuid, mock_getenv, mock_put):
405405
"""Test that the correct file data is sent in upload request"""
406406
mock_uuid.return_value = uuid.UUID('12345678-1234-5678-1234-567812345678')
407407
mock_getenv.return_value = 'https://s3.amazonaws.com/bucket'
408408

409409
mock_response = Mock()
410410
mock_response.status_code = 200
411411
mock_response.json.return_value = {'url': 'https://s3.amazonaws.com/image.jpg'}
412-
mock_post.return_value = mock_response
412+
mock_put.return_value = mock_response
413413

414414
img = Image.new('RGB', (100, 100), color='blue')
415415
img.format = 'JPEG'
416416

417417
upload_image(img, 'image/jpeg')
418418

419-
# Verify the post was called with correct arguments
420-
call_args = mock_post.call_args
419+
# Verify the put was called with correct arguments
420+
call_args = mock_put.call_args
421421
assert call_args[0][0] == 'https://s3.amazonaws.com/bucket'
422422
assert 'files' in call_args[1]
423423
assert 'file' in call_args[1]['files']

0 commit comments

Comments
 (0)