11import pytest
22import uuid
3- from io import BytesIO
4- from unittest .mock import Mock , patch , MagicMock
3+ from unittest .mock import Mock , patch
54from PIL import Image
65import requests
76
@@ -191,11 +190,10 @@ def test_successful_upload(self, mock_uuid, mock_getenv, mock_put):
191190 """Test successful image upload with UUID-based filename"""
192191 # Setup mocks
193192 mock_uuid .return_value = uuid .UUID ('12345678-1234-5678-1234-567812345678' )
194- mock_getenv .return_value = 'https://s3.amazonaws.com/my-bucket'
193+ mock_getenv .return_value = 'https://s3.amazonaws.com/my-bucket/ '
195194
196195 mock_response = Mock ()
197196 mock_response .status_code = 200
198- mock_response .json .return_value = {'url' : f'https://s3.amazonaws.com/uploaded-image.jpg' }
199197 mock_put .return_value = mock_response
200198
201199 # Create a real PIL image for testing
@@ -206,41 +204,28 @@ def test_successful_upload(self, mock_uuid, mock_getenv, mock_put):
206204 result = upload_image (img , 'image/jpeg' )
207205
208206 # Verify response
209- assert result == 'https://s3.amazonaws.com/uploaded-image.jpg '
207+ assert result == 'https://s3.amazonaws.com/my-bucket/12345678-1234-5678-1234-567812345678.jpeg '
210208 assert mock_put .called
211209 assert mock_put .call_args [1 ]['timeout' ] == 30
212210
213- # Verify UUID-based filename is used
214- call_args = mock_put .call_args
215- filename , file_obj , mime_type = call_args [1 ]['files' ]['file' ]
216- assert filename == '12345678-1234-5678-1234-567812345678.jpeg'
217- assert mime_type == 'image/jpeg'
218-
219211 @patch ('lf_toolkit.evaluation.image_upload.requests.put' )
220212 @patch ('lf_toolkit.evaluation.image_upload.os.getenv' )
221213 @patch ('lf_toolkit.evaluation.image_upload.uuid.uuid4' )
222214 def test_upload_with_png_image (self , mock_uuid , mock_getenv , mock_put ):
223215 """Test uploading PNG image with UUID-based filename"""
224216 mock_uuid .return_value = uuid .UUID ('aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' )
225- mock_getenv .return_value = 'https://storage.example.com'
217+ mock_getenv .return_value = 'https://storage.example.com/ '
226218
227219 mock_response = Mock ()
228220 mock_response .status_code = 200
229- mock_response .json .return_value = {'url' : 'https://storage.example.com/image.png' }
230221 mock_put .return_value = mock_response
231222
232223 img = Image .new ('RGBA' , (50 , 50 ), color = (0 , 255 , 0 , 128 ))
233224 img .format = 'PNG'
234225
235226 result = upload_image (img , 'image/png' )
236227
237- assert result == 'https://storage.example.com/image.png'
238-
239- # Verify UUID-based filename is used
240- call_args = mock_put .call_args
241- filename , file_obj , mime_type = call_args [1 ]['files' ]['file' ]
242- assert filename == 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee.png'
243- assert mime_type == 'image/png'
228+ assert result == 'https://storage.example.com/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee.png'
244229
245230 @patch ('lf_toolkit.evaluation.image_upload.os.getenv' )
246231 def test_upload_missing_s3_uri (self , mock_getenv ):
@@ -341,91 +326,36 @@ def test_upload_mime_type_mismatch(self, mock_uuid, mock_getenv, mock_put):
341326 def test_upload_image_no_format (self , mock_uuid , mock_getenv , mock_put ):
342327 """Test upload with image that has no format (defaults to PNG) uses UUID filename"""
343328 mock_uuid .return_value = uuid .UUID ('12345678-1234-5678-1234-567812345678' )
344- mock_getenv .return_value = 'https://s3.amazonaws.com/bucket'
329+ mock_getenv .return_value = 'https://s3.amazonaws.com/bucket/ '
345330
346331 mock_response = Mock ()
347332 mock_response .status_code = 200
348- mock_response .json .return_value = {'url' : 'https://s3.amazonaws.com/image.png' }
349333 mock_put .return_value = mock_response
350334
351335 img = Image .new ('RGB' , (100 , 100 ))
352336 img .format = None
353337
354338 result = upload_image (img , 'image/png' )
355339
356- assert result == 'https://s3.amazonaws.com/image.png'
357-
358- # Verify UUID-based filename with default .png extension
359- call_args = mock_put .call_args
360- filename , file_obj , mime_type = call_args [1 ]['files' ]['file' ]
361- assert filename == '12345678-1234-5678-1234-567812345678.png'
362- assert mime_type == 'image/png'
363-
364- @patch ('lf_toolkit.evaluation.image_upload.requests.put' )
365- @patch ('lf_toolkit.evaluation.image_upload.os.getenv' )
366- @patch ('lf_toolkit.evaluation.image_upload.uuid.uuid4' )
367- def test_upload_uses_different_uuid_each_time (self , mock_uuid , mock_getenv , mock_put ):
368- """Test that each upload generates a unique UUID-based filename"""
369- mock_getenv .return_value = 'https://s3.amazonaws.com/bucket'
370-
371- mock_response = Mock ()
372- mock_response .status_code = 200
373- mock_response .json .return_value = {'url' : 'https://s3.amazonaws.com/uploaded.jpg' }
374- mock_put .return_value = mock_response
375-
376- # First upload with first UUID
377- uuid1 = uuid .UUID ('11111111-1111-1111-1111-111111111111' )
378- mock_uuid .return_value = uuid1
379-
380- img1 = Image .new ('RGB' , (100 , 100 ))
381- img1 .format = 'JPEG'
382- upload_image (img1 , 'image/jpeg' )
383-
384- filename1 = mock_put .call_args [1 ]['files' ]['file' ][0 ]
385-
386- # Second upload with different UUID
387- uuid2 = uuid .UUID ('22222222-2222-2222-2222-222222222222' )
388- mock_uuid .return_value = uuid2
389-
390- img2 = Image .new ('RGB' , (100 , 100 ))
391- img2 .format = 'JPEG'
392- upload_image (img2 , 'image/jpeg' )
393-
394- filename2 = mock_put .call_args [1 ]['files' ]['file' ][0 ]
395-
396- # Verify different UUIDs result in different filenames
397- assert filename1 == '11111111-1111-1111-1111-111111111111.jpeg'
398- assert filename2 == '22222222-2222-2222-2222-222222222222.jpeg'
399- assert filename1 != filename2
340+ assert result == 'https://s3.amazonaws.com/bucket/12345678-1234-5678-1234-567812345678.png'
400341
401342 @patch ('lf_toolkit.evaluation.image_upload.requests.put' )
402343 @patch ('lf_toolkit.evaluation.image_upload.os.getenv' )
403344 @patch ('lf_toolkit.evaluation.image_upload.uuid.uuid4' )
404345 def test_upload_verifies_correct_file_uploaded (self , mock_uuid , mock_getenv , mock_put ):
405346 """Test that the correct file data is sent in upload request"""
406347 mock_uuid .return_value = uuid .UUID ('12345678-1234-5678-1234-567812345678' )
407- mock_getenv .return_value = 'https://s3.amazonaws.com/bucket'
348+ mock_getenv .return_value = 'https://s3.amazonaws.com/bucket/ '
408349
409350 mock_response = Mock ()
410351 mock_response .status_code = 200
411- mock_response .json .return_value = {'url' : 'https://s3.amazonaws.com/image.jpg' }
412352 mock_put .return_value = mock_response
413353
414354 img = Image .new ('RGB' , (100 , 100 ), color = 'blue' )
415355 img .format = 'JPEG'
416356
417357 upload_image (img , 'image/jpeg' )
418358
419- # Verify the put was called with correct arguments
420- call_args = mock_put .call_args
421- assert call_args [0 ][0 ] == 'https://s3.amazonaws.com/bucket'
422- assert 'files' in call_args [1 ]
423- assert 'file' in call_args [1 ]['files' ]
424-
425- filename , file_obj , mime_type = call_args [1 ]['files' ]['file' ]
426- assert filename == '12345678-1234-5678-1234-567812345678.jpeg'
427- assert mime_type == 'image/jpeg'
428-
429359
430360class TestExceptionHierarchy :
431361 """Test suite for custom exception classes"""
0 commit comments