@@ -265,7 +265,7 @@ def test_basic_reduce(self):
265265 """Test reduce with sum operation."""
266266 transformer = createTransformer (int )
267267 reducer = transformer .reduce (lambda acc , x : acc + x , initial = 0 )
268- result = list (reducer ([1 , 2 , 3 , 4 ]))
268+ result = list (reducer ([1 , 2 , 3 , 4 ], None ))
269269 assert result == [10 ]
270270
271271 def test_reduce_with_context (self ):
@@ -280,9 +280,84 @@ def test_reduce_after_transformation(self):
280280 """Test reduce after map transformation."""
281281 transformer = createTransformer (int ).map (lambda x : x * 2 )
282282 reducer = transformer .reduce (lambda acc , x : acc + x , initial = 0 )
283- result = list (reducer ([1 , 2 , 3 ]))
283+ result = list (reducer ([1 , 2 , 3 ], None ))
284284 assert result == [12 ] # [2, 4, 6] summed = 12
285285
286+ def test_reduce_per_chunk_basic (self ):
287+ """Test reduce with per_chunk=True for basic operation."""
288+ transformer = createTransformer (int , chunk_size = 2 ).reduce (lambda acc , x : acc + x , initial = 0 , per_chunk = True )
289+ result = list (transformer ([1 , 2 , 3 , 4 , 5 ]))
290+ # With chunk_size=2: [1, 2] -> 3, [3, 4] -> 7, [5] -> 5
291+ assert result == [3 , 7 , 5 ]
292+
293+ def test_reduce_per_chunk_with_context (self ):
294+ """Test reduce with per_chunk=True and context-aware function."""
295+ context = PipelineContext ({"multiplier" : 2 })
296+ transformer = createTransformer (int , chunk_size = 2 ).reduce (
297+ lambda acc , x , ctx : acc + (x * ctx ["multiplier" ]), initial = 0 , per_chunk = True
298+ )
299+ result = list (transformer ([1 , 2 , 3 ], context ))
300+ # With chunk_size=2: [1, 2] -> (1*2) + (2*2) = 6, [3] -> (3*2) = 6
301+ assert result == [6 , 6 ]
302+
303+ def test_reduce_per_chunk_empty_chunks (self ):
304+ """Test reduce with per_chunk=True handles empty chunks correctly."""
305+ transformer = createTransformer (int , chunk_size = 5 ).reduce (lambda acc , x : acc + x , initial = 0 , per_chunk = True )
306+ result = list (transformer ([]))
307+ assert result == []
308+
309+ def test_reduce_per_chunk_single_element_chunks (self ):
310+ """Test reduce with per_chunk=True with single element chunks."""
311+ transformer = createTransformer (int , chunk_size = 1 ).reduce (lambda acc , x : acc + x , initial = 10 , per_chunk = True )
312+ result = list (transformer ([1 , 2 , 3 ]))
313+ # Each chunk has one element: [1] -> 10+1=11, [2] -> 10+2=12, [3] -> 10+3=13
314+ assert result == [11 , 12 , 13 ]
315+
316+ def test_reduce_per_chunk_chaining (self ):
317+ """Test reduce with per_chunk=True can be chained with other operations."""
318+ transformer = (
319+ createTransformer (int , chunk_size = 2 )
320+ .map (lambda x : x * 2 )
321+ .reduce (lambda acc , x : acc + x , initial = 0 , per_chunk = True )
322+ .map (lambda x : x * 10 )
323+ )
324+ result = list (transformer ([1 , 2 , 3 ]))
325+ # After map: [2, 4, 6]
326+ # With chunk_size=2: [2, 4] -> 6, [6] -> 6
327+ # After second map: [60, 60]
328+ assert result == [60 , 60 ]
329+
330+ def test_reduce_per_chunk_different_chunk_sizes (self ):
331+ """Test reduce with per_chunk=True works with different chunk sizes."""
332+ data = [1 , 2 , 3 , 4 , 5 , 6 ]
333+
334+ # Test with chunk_size=2
335+ transformer_2 = createTransformer (int , chunk_size = 2 ).reduce (lambda acc , x : acc + x , initial = 0 , per_chunk = True )
336+ result_2 = list (transformer_2 (data ))
337+ assert result_2 == [3 , 7 , 11 ] # [1,2]->3, [3,4]->7, [5,6]->11
338+
339+ # Test with chunk_size=3
340+ transformer_3 = createTransformer (int , chunk_size = 3 ).reduce (lambda acc , x : acc + x , initial = 0 , per_chunk = True )
341+ result_3 = list (transformer_3 (data ))
342+ assert result_3 == [6 , 15 ] # [1,2,3]->6, [4,5,6]->15
343+
344+ def test_reduce_per_chunk_versus_terminal (self ):
345+ """Test that per_chunk=True and per_chunk=False produce different behaviors."""
346+ data = [1 , 2 , 3 , 4 ]
347+
348+ # Terminal reduce (per_chunk=False) - returns a callable
349+ transformer_terminal = createTransformer (int , chunk_size = 2 )
350+ reducer_terminal = transformer_terminal .reduce (lambda acc , x : acc + x , initial = 0 , per_chunk = False )
351+ result_terminal = list (reducer_terminal (data , None ))
352+ assert result_terminal == [10 ] # Sum of all elements
353+
354+ # Per-chunk reduce (per_chunk=True) - returns a transformer
355+ transformer_per_chunk = createTransformer (int , chunk_size = 2 ).reduce (
356+ lambda acc , x : acc + x , initial = 0 , per_chunk = True
357+ )
358+ result_per_chunk = list (transformer_per_chunk (data ))
359+ assert result_per_chunk == [3 , 7 ] # Sum per chunk [1,2]->3, [3,4]->7
360+
286361
287362class TestTransformerEdgeCases :
288363 """Test edge cases and boundary conditions."""
0 commit comments