1010import pytest
1111from PIL import Image
1212
13- from withoutbg .core import remove_background_batch
13+ from withoutbg .core import WithoutBG
1414
1515
1616@pytest .fixture
@@ -58,10 +58,11 @@ def test_small_batch_performance(self, create_test_images, mock_processing):
5858 image_files = create_test_images (count = 3 , size = (256 , 256 ))
5959
6060 try :
61- with patch ("withoutbg.core.remove_background" , side_effect = mock_processing ):
61+ model = WithoutBG .opensource ()
62+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
6263 # Measure batch processing time
6364 start_time = time .time ()
64- results = remove_background_batch (image_files )
65+ results = model . remove_background_batch (image_files )
6566 end_time = time .time ()
6667
6768 batch_time = end_time - start_time
@@ -93,10 +94,11 @@ def test_medium_batch_performance(self, create_test_images, mock_processing):
9394 image_files = create_test_images (count = 10 , size = (512 , 384 ))
9495
9596 try :
96- with patch ("withoutbg.core.remove_background" , side_effect = mock_processing ):
97+ model = WithoutBG .opensource ()
98+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
9799 # Measure batch processing time
98100 start_time = time .time ()
99- results = remove_background_batch (image_files )
101+ results = model . remove_background_batch (image_files )
100102 end_time = time .time ()
101103
102104 batch_time = end_time - start_time
@@ -122,10 +124,11 @@ def test_large_batch_performance(self, create_test_images, mock_processing):
122124 image_files = create_test_images (count = 25 , size = (400 , 300 ))
123125
124126 try :
125- with patch ("withoutbg.core.remove_background" , side_effect = mock_processing ):
127+ model = WithoutBG .opensource ()
128+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
126129 # Measure batch processing time
127130 start_time = time .time ()
128- results = remove_background_batch (image_files )
131+ results = model . remove_background_batch (image_files )
129132 end_time = time .time ()
130133
131134 batch_time = end_time - start_time
@@ -156,12 +159,11 @@ def test_batch_size_scaling(self, create_test_images, mock_processing):
156159 image_files = create_test_images (count = batch_size , size = (256 , 256 ))
157160
158161 try :
159- with patch (
160- "withoutbg.core.remove_background" , side_effect = mock_processing
161- ):
162+ model = WithoutBG .opensource ()
163+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
162164 # Measure batch processing
163165 start_time = time .time ()
164- results = remove_background_batch (image_files )
166+ results = model . remove_background_batch (image_files )
165167 end_time = time .time ()
166168
167169 batch_time = end_time - start_time
@@ -200,9 +202,10 @@ def test_batch_memory_efficiency(self, create_test_images, mock_processing):
200202 # Force garbage collection
201203 gc .collect ()
202204
203- with patch ("withoutbg.core.remove_background" , side_effect = mock_processing ):
205+ model = WithoutBG .opensource ()
206+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
204207 # Process batch and monitor memory
205- results = remove_background_batch (image_files )
208+ results = model . remove_background_batch (image_files )
206209
207210 # Verify processing completed
208211 assert len (results ) == len (image_files )
@@ -226,12 +229,11 @@ def test_batch_with_output_directory_performance(
226229
227230 with tempfile .TemporaryDirectory () as output_dir :
228231 try :
229- with patch (
230- "withoutbg.core.remove_background" , side_effect = mock_processing
231- ):
232+ model = WithoutBG .opensource ()
233+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
232234 # Measure batch processing with output directory
233235 start_time = time .time ()
234- results = remove_background_batch (
236+ results = model . remove_background_batch (
235237 image_files , output_dir = output_dir
236238 )
237239 end_time = time .time ()
@@ -270,15 +272,17 @@ def mock_processing_with_errors(input_image, **kwargs):
270272 return Image .new ("RGBA" , img .size , color = (100 , 150 , 200 , 128 ))
271273
272274 try :
273- with patch (
274- "withoutbg.core.remove_background" ,
275+ model = WithoutBG .opensource ()
276+ with patch .object (
277+ model .model ,
278+ "remove_background" ,
275279 side_effect = mock_processing_with_errors ,
276280 ):
277281 # Measure batch processing with errors
278282 start_time = time .time ()
279283
280284 try :
281- results = remove_background_batch (image_files )
285+ results = model . remove_background_batch (image_files )
282286 # If no exception is raised, check results
283287 assert len (results ) <= len (image_files ) # Some may have failed
284288 except Exception :
@@ -313,10 +317,11 @@ def test_batch_different_image_sizes_performance(self, mock_processing):
313317 image_files .append (tmp_file .name )
314318
315319 try :
316- with patch ("withoutbg.core.remove_background" , side_effect = mock_processing ):
320+ model = WithoutBG .opensource ()
321+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
317322 # Measure batch processing with mixed sizes
318323 start_time = time .time ()
319- results = remove_background_batch (image_files )
324+ results = model . remove_background_batch (image_files )
320325 end_time = time .time ()
321326
322327 batch_time = end_time - start_time
@@ -347,14 +352,15 @@ def test_batch_processing_consistency(self, create_test_images, mock_processing)
347352 image_files = create_test_images (count = 5 , size = (300 , 300 ))
348353
349354 try :
350- with patch ("withoutbg.core.remove_background" , side_effect = mock_processing ):
355+ model = WithoutBG .opensource ()
356+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
351357 # Process the same batch multiple times
352358 all_results = []
353359 processing_times = []
354360
355361 for _run in range (3 ):
356362 start_time = time .time ()
357- results = remove_background_batch (image_files )
363+ results = model . remove_background_batch (image_files )
358364 end_time = time .time ()
359365
360366 processing_times .append (end_time - start_time )
@@ -389,10 +395,11 @@ def test_batch_processing_with_pil_images(self, mock_processing):
389395 for i in range (8 )
390396 ]
391397
392- with patch ("withoutbg.core.remove_background" , side_effect = mock_processing ):
398+ model = WithoutBG .opensource ()
399+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
393400 # Measure batch processing with PIL images
394401 start_time = time .time ()
395- results = remove_background_batch (test_images )
402+ results = model . remove_background_batch (test_images )
396403 end_time = time .time ()
397404
398405 batch_time = end_time - start_time
@@ -419,14 +426,15 @@ def test_concurrent_batch_processing_simulation(
419426 all_batches = [batch1 , batch2 , batch3 ]
420427
421428 try :
422- with patch ("withoutbg.core.remove_background" , side_effect = mock_processing ):
429+ model = WithoutBG .opensource ()
430+ with patch .object (model .model , "remove_background" , side_effect = mock_processing ):
423431 # Process batches sequentially (simulating concurrent workload)
424432 total_start_time = time .time ()
425433 all_results = []
426434
427435 for i , batch in enumerate (all_batches ):
428436 start_time = time .time ()
429- results = remove_background_batch (batch )
437+ results = model . remove_background_batch (batch )
430438 end_time = time .time ()
431439
432440 all_results .append (results )
0 commit comments