@@ -312,21 +312,6 @@ def test_set_initial_state_delegation(mock_config, mock_underlying_router):
312312 mock_underlying_router .set_initial_state .assert_called_once_with (mock_state )
313313
314314
315- def test_get_stream_state_delegation (mock_config , mock_underlying_router ):
316- """Test that get_stream_state delegates to the underlying router."""
317- router = GroupingPartitionRouter (
318- group_size = 2 ,
319- underlying_partition_router = mock_underlying_router ,
320- config = mock_config ,
321- )
322- mock_state = {"state_key" : "state_value" }
323- mock_underlying_router .get_stream_state = MagicMock (return_value = mock_state )
324-
325- state = router .get_stream_state ()
326- assert state == mock_state
327- mock_underlying_router .get_stream_state .assert_called_once ()
328-
329-
330315def test_stream_slices_extra_fields_varied (mock_config ):
331316 """Test grouping with varied extra fields across partitions."""
332317 parent_stream = MockStream (
@@ -458,3 +443,72 @@ def test_get_request_params_default(mock_config, mock_underlying_router):
458443 )
459444 )
460445 assert params == {}
446+
447+
448+ def test_stream_slices_resume_from_state (mock_config , mock_underlying_router ):
449+ """Test that stream_slices resumes correctly from a previous state."""
450+
451+ # Simulate underlying router state handling
452+ class MockPartitionRouter :
453+ def __init__ (self ):
454+ self .slices = [
455+ StreamSlice (
456+ partition = {"board_ids" : i },
457+ cursor_slice = {},
458+ extra_fields = {"name" : f"Board { i } " , "owner" : f"User{ i } " },
459+ )
460+ for i in range (5 )
461+ ]
462+ self .state = {"last_board_id" : 0 } # Initial state
463+
464+ def set_initial_state (self , state ):
465+ self .state = state
466+
467+ def get_stream_state (self ):
468+ return self .state
469+
470+ def stream_slices (self ):
471+ last_board_id = self .state .get ("last_board_id" , - 1 )
472+ for slice in self .slices :
473+ board_id = slice .partition ["board_ids" ]
474+ if board_id <= last_board_id :
475+ continue
476+ self .state = {"last_board_id" : board_id }
477+ yield slice
478+
479+ underlying_router = MockPartitionRouter ()
480+ router = GroupingPartitionRouter (
481+ group_size = 2 ,
482+ underlying_partition_router = underlying_router ,
483+ config = mock_config ,
484+ deduplicate = True ,
485+ )
486+
487+ # First sync: process first two slices
488+ router .set_initial_state ({"last_board_id" : 0 })
489+ slices_iter = router .stream_slices ()
490+ first_batch = next (slices_iter )
491+ assert first_batch == StreamSlice (
492+ partition = {"board_ids" : [1 , 2 ]},
493+ cursor_slice = {},
494+ extra_fields = {"name" : ["Board 1" , "Board 2" ], "owner" : ["User1" , "User2" ]},
495+ )
496+ state_after_first = router .get_stream_state ()
497+ assert state_after_first == {"last_board_id" : 2 }, "State should reflect last processed board_id"
498+
499+ # Simulate a new sync resuming from the previous state
500+ new_router = GroupingPartitionRouter (
501+ group_size = 2 ,
502+ underlying_partition_router = MockPartitionRouter (),
503+ config = mock_config ,
504+ deduplicate = True ,
505+ )
506+ new_router .set_initial_state (state_after_first )
507+ resumed_slices = list (new_router .stream_slices ())
508+ assert resumed_slices == [
509+ StreamSlice (
510+ partition = {"board_ids" : [3 , 4 ]},
511+ cursor_slice = {},
512+ extra_fields = {"name" : ["Board 3" , "Board 4" ], "owner" : ["User3" , "User4" ]},
513+ )
514+ ], "Should resume from board_id 3"
0 commit comments