@@ -204,6 +204,40 @@ def test_time_in_draft_with_attribute_error_scenario(self):
204204expected = timedelta (days = 3 )
205205self .assertEqual (result ,expected ,"The time in draft should be 3 days." )
206206
207+ def test_time_in_draft_with_iterator_events (self ):
208+ """
209+ Test measure_time_in_draft with events() returning an iterator instead of a list.
210+ This test ensures the function works correctly when events() returns an iterator
211+ (as it does in the real GitHub API), which can only be consumed once.
212+ """
213+ # Set up issue created_at time
214+ self .issue .issue .created_at = "2021-01-01T00:00:00Z"
215+
216+ # Create an iterator of events (simulating real GitHub API behavior)
217+ def events_iterator ():
218+ return iter (
219+ [
220+ MagicMock (
221+ event = "converted_to_draft" ,
222+ created_at = datetime (2021 ,1 ,1 ,tzinfo = pytz .utc ),
223+ ),
224+ MagicMock (
225+ event = "ready_for_review" ,
226+ created_at = datetime (2021 ,1 ,3 ,tzinfo = pytz .utc ),
227+ ),
228+ ]
229+ )
230+
231+ self .issue .issue .events = events_iterator
232+
233+ result = measure_time_in_draft (self .issue )
234+ expected = timedelta (days = 2 )
235+ self .assertEqual (
236+ result ,
237+ expected ,
238+ "The time in draft should be 2 days when events() returns an iterator." ,
239+ )
240+
207241
208242class TestGetStatsTimeInDraft (unittest .TestCase ):
209243"""