from __future__ import annotations from checkpointing import CheckpointController from fivetran.operations import RecordingOperationSink from state import CheckpointPolicy, normalize_state def test_checkpoint_controller_waits_until_policy_fires(): sink = RecordingOperationSink() state = normalize_state({}, sync_task_id="task-001") policy = CheckpointPolicy(rows=2, seconds=999_999) checkpoint = CheckpointController(sink, state, policy) checkpoint.record_row() assert checkpoint.has_pending_rows is True assert sink.calls == [] checkpoint.record_row() assert checkpoint.has_pending_rows is False assert [name for name, _ in sink.calls] == ["checkpoint"] assert policy.rows_since_checkpoint == 0 def test_checkpoint_controller_manual_checkpoint_flushes_pending_rows(): sink = RecordingOperationSink() state = normalize_state({}, sync_task_id="task-001") policy = CheckpointPolicy(rows=100, seconds=999_999) checkpoint = CheckpointController(sink, state, policy) checkpoint.record_row() checkpoint.checkpoint() assert checkpoint.has_pending_rows is False assert [name for name, _ in sink.calls] == ["checkpoint"] assert policy.rows_since_checkpoint == 0