@@ -56,6 +56,7 @@ class TestDownloadJsonResponsesParallel:
5656
5757 def test_download_single_json_response (self ):
5858 """Test downloading a single JSON response."""
59+ http_client = Mock ()
5960 label = {"id" : "label1" , "jsonResponseUrl" : "https://example.com/label1.json" }
6061 url_to_label_mapping = [("https://example.com/label1.json" , label )]
6162
@@ -65,7 +66,7 @@ def test_download_single_json_response(self):
6566 ) as mock_download :
6667 mock_download .return_value = {"annotation" : "data" }
6768
68- download_json_responses_parallel (url_to_label_mapping )
69+ download_json_responses_parallel (url_to_label_mapping , http_client )
6970
7071 # Verify the JSON was downloaded and assigned
7172 assert label ["jsonResponse" ] == {"annotation" : "data" }
@@ -75,6 +76,7 @@ def test_download_single_json_response(self):
7576
7677 def test_download_multiple_json_responses (self ):
7778 """Test downloading multiple JSON responses in parallel."""
79+ http_client = Mock ()
7880 label1 = {"id" : "label1" , "jsonResponseUrl" : "https://example.com/label1.json" }
7981 label2 = {"id" : "label2" , "jsonResponseUrl" : "https://example.com/label2.json" }
8082 label3 = {"id" : "label3" , "jsonResponseUrl" : "https://example.com/label3.json" }
@@ -86,7 +88,7 @@ def test_download_multiple_json_responses(self):
8688 ]
8789
8890 # Create mock side effect for different URLs
89- async def mock_download (url ):
91+ async def mock_download (url , client ):
9092 if "label1" in url :
9193 return {"data" : "label1" }
9294 elif "label2" in url :
@@ -99,7 +101,7 @@ async def mock_download(url):
99101 "kili.adapters.kili_api_gateway.asset.formatters._download_json_response" ,
100102 side_effect = mock_download ,
101103 ):
102- download_json_responses_parallel (url_to_label_mapping )
104+ download_json_responses_parallel (url_to_label_mapping , http_client )
103105
104106 # Verify all labels got their JSON
105107 assert label1 ["jsonResponse" ] == {"data" : "label1" }
@@ -113,25 +115,27 @@ async def mock_download(url):
113115
114116 def test_download_handles_request_exception (self ):
115117 """Test that request exceptions are handled gracefully."""
118+ http_client = Mock ()
116119 label = {"id" : "label1" , "jsonResponseUrl" : "https://example.com/label1.json" }
117120 url_to_label_mapping = [("https://example.com/label1.json" , label )]
118121
119- # Mock download to raise httpx.HTTPError
122+ # Mock download to return empty dict on error
120123 with patch (
121124 "kili.adapters.kili_api_gateway.asset.formatters._download_json_response" ,
122125 new_callable = AsyncMock ,
123126 ) as mock_download :
124127 mock_download .return_value = {} # Returns empty dict on error
125128
126129 # Should not raise exception
127- download_json_responses_parallel (url_to_label_mapping )
130+ download_json_responses_parallel (url_to_label_mapping , http_client )
128131
129132 # Should set empty dict on error
130133 assert label ["jsonResponse" ] == {}
131134 assert "jsonResponseUrl" not in label
132135
133136 def test_download_handles_json_decode_error (self ):
134137 """Test that JSON decode errors are handled gracefully."""
138+ http_client = Mock ()
135139 label = {"id" : "label1" , "jsonResponseUrl" : "https://example.com/label1.json" }
136140 url_to_label_mapping = [("https://example.com/label1.json" , label )]
137141
@@ -143,14 +147,15 @@ def test_download_handles_json_decode_error(self):
143147 mock_download .return_value = {} # Returns empty dict on error
144148
145149 # Should not raise exception
146- download_json_responses_parallel (url_to_label_mapping )
150+ download_json_responses_parallel (url_to_label_mapping , http_client )
147151
148152 # Should set empty dict on error
149153 assert label ["jsonResponse" ] == {}
150154 assert "jsonResponseUrl" not in label
151155
152156 def test_download_handles_timeout_error (self ):
153157 """Test that timeout errors are handled gracefully."""
158+ http_client = Mock ()
154159 label = {"id" : "label1" , "jsonResponseUrl" : "https://example.com/label1.json" }
155160 url_to_label_mapping = [("https://example.com/label1.json" , label )]
156161
@@ -162,14 +167,15 @@ def test_download_handles_timeout_error(self):
162167 mock_download .return_value = {} # Returns empty dict on error
163168
164169 # Should not raise exception
165- download_json_responses_parallel (url_to_label_mapping )
170+ download_json_responses_parallel (url_to_label_mapping , http_client )
166171
167172 # Should set empty dict on error
168173 assert label ["jsonResponse" ] == {}
169174 assert "jsonResponseUrl" not in label
170175
171176 def test_download_processes_in_batches (self ):
172177 """Test that downloads are processed in batches of JSON_RESPONSE_BATCH_SIZE."""
178+ http_client = Mock ()
173179 # Create more labels than batch size
174180 num_labels = JSON_RESPONSE_BATCH_SIZE + 5
175181 labels = [
@@ -188,7 +194,7 @@ def test_download_processes_in_batches(self):
188194 mock_download .return_value = {"data" : "test" }
189195
190196 # Call the function
191- download_json_responses_parallel (url_to_label_mapping )
197+ download_json_responses_parallel (url_to_label_mapping , http_client )
192198
193199 # Verify all labels got their JSON response
194200 for label in labels :
@@ -200,6 +206,7 @@ def test_download_processes_in_batches(self):
200206
201207 def test_download_mixed_success_and_failure (self ):
202208 """Test downloading with some successes and some failures."""
209+ http_client = Mock ()
203210 label_success = {"id" : "success" , "jsonResponseUrl" : "https://example.com/success.json" }
204211 label_fail = {"id" : "fail" , "jsonResponseUrl" : "https://example.com/fail.json" }
205212
@@ -209,7 +216,7 @@ def test_download_mixed_success_and_failure(self):
209216 ]
210217
211218 # Create mock side effect for different URLs
212- async def mock_download (url ):
219+ async def mock_download (url , client ):
213220 if "success" in url :
214221 return {"status" : "ok" }
215222 return {} # Failure returns empty dict
@@ -218,7 +225,7 @@ async def mock_download(url):
218225 "kili.adapters.kili_api_gateway.asset.formatters._download_json_response" ,
219226 side_effect = mock_download ,
220227 ):
221- download_json_responses_parallel (url_to_label_mapping )
228+ download_json_responses_parallel (url_to_label_mapping , http_client )
222229
223230 # Success should have data
224231 assert label_success ["jsonResponse" ] == {"status" : "ok" }
@@ -283,7 +290,7 @@ def test_load_asset_with_labels_json_response_url(self):
283290 fields = ["id" , "labels.jsonResponse" , "labels.jsonResponseUrl" ]
284291
285292 # Create mock side effect for different URLs
286- async def mock_download (url ):
293+ async def mock_download (url , client ):
287294 if "label1" in url :
288295 return {"data" : "label1" }
289296 elif "label2" in url :
@@ -345,7 +352,7 @@ def test_load_asset_with_both_labels_and_latest_label(self):
345352 ]
346353
347354 # Create mock side effect for different URLs
348- async def mock_download (url ):
355+ async def mock_download (url , client ):
349356 if "label1" in url :
350357 return {"data" : "label1" }
351358 elif "label2" in url :
0 commit comments