22import os
33import unittest
44import urllib
5- from unittest .mock import patch
5+ from unittest .mock import mock_open , patch
66
77import responses
88
@@ -22,10 +22,8 @@ class TestUploadImage(unittest.TestCase):
2222 IMAGE_NAME_HOSTED = os .path .basename (IMAGE_PATH_HOSTED )
2323
2424 @responses .activate
25- @patch ("roboflow.util.image_utils.file2jpeg" )
26- def test_upload_image_local (self , mock_file2jpeg ):
27- mock_file2jpeg .return_value = b"image_data"
28-
25+ @patch ("roboflow.adapters.rfapi.open" , new_callable = mock_open , read_data = b"image_data" )
26+ def test_upload_image_local (self , _mock_file ):
2927 scenarios = [
3028 {
3129 "desc" : "with batch_name" ,
@@ -123,10 +121,8 @@ def test_upload_image_hosted(self):
123121 self .assertTrue (result ["success" ], msg = f"Failed in scenario: { scenario ['desc' ]} " )
124122
125123 @responses .activate
126- @patch ("roboflow.util.image_utils.file2jpeg" )
127- def test_upload_image_local_with_metadata (self , mock_file2jpeg ):
128- mock_file2jpeg .return_value = b"image_data"
129-
124+ @patch ("roboflow.adapters.rfapi.open" , new_callable = mock_open , read_data = b"image_data" )
125+ def test_upload_image_local_with_metadata (self , _mock_file ):
130126 metadata = {"camera_id" : "cam001" , "location" : "warehouse" }
131127 expected_url = (
132128 f"{ API_URL } /dataset/{ self .PROJECT_URL } /upload?"
@@ -172,6 +168,32 @@ def test_upload_image_hosted_with_metadata(self):
172168 )
173169 self .assertTrue (result ["success" ])
174170
171+ @responses .activate
172+ def test_upload_image_local_uploads_original_bytes (self ):
173+ """Server-side dedup relies on the SDK uploading the file bytes exactly as-is."""
174+ import tempfile
175+
176+ raw_bytes = b"\x89 PNG\r \n \x1a \n fake-png-bytes-not-decodable-as-jpeg"
177+ with tempfile .NamedTemporaryFile (suffix = ".png" , delete = False ) as tf :
178+ tf .write (raw_bytes )
179+ tmp_path = tf .name
180+
181+ try :
182+ expected_url = (
183+ f"{ API_URL } /dataset/{ self .PROJECT_URL } /upload?"
184+ f"api_key={ self .API_KEY } &batch={ urllib .parse .quote_plus (DEFAULT_BATCH_NAME )} "
185+ )
186+ responses .add (responses .POST , expected_url , json = {"success" : True }, status = 200 )
187+
188+ result = upload_image (self .API_KEY , self .PROJECT_URL , tmp_path )
189+ self .assertTrue (result ["success" ])
190+
191+ request_body = responses .calls [0 ].request .body
192+ self .assertIn (raw_bytes , request_body )
193+ self .assertIn (b"image/png" , request_body )
194+ finally :
195+ os .unlink (tmp_path )
196+
175197 def _reset_responses (self ):
176198 responses .reset ()
177199
0 commit comments