11"""Tests for GitHub snapshot writing."""
22
3- from datetime import UTC , datetime , timezone
3+ from datetime import UTC , datetime
44from unittest .mock import MagicMock
55
66import pytest
@@ -99,8 +99,8 @@ def test_collect_snapshot_data() -> None:
9999 scores = [
100100 Score (
101101 github_user = "alice" ,
102- period_start = datetime (2024 , 1 , 1 , tzinfo = timezone . utc ),
103- period_end = datetime (2024 , 1 , 31 , tzinfo = timezone . utc ),
102+ period_start = datetime (2024 , 1 , 1 , tzinfo = UTC ),
103+ period_end = datetime (2024 , 1 , 31 , tzinfo = UTC ),
104104 points = 100 ,
105105 ),
106106 ]
@@ -110,10 +110,10 @@ def test_collect_snapshot_data() -> None:
110110 "456" : ["Maintainer" ],
111111 }
112112
113- period_start = datetime (2024 , 1 , 1 , tzinfo = timezone . utc )
114- period_end = datetime (2024 , 1 , 31 , tzinfo = timezone . utc )
113+ period_start = datetime (2024 , 1 , 1 , tzinfo = UTC )
114+ period_end = datetime (2024 , 1 , 31 , tzinfo = UTC )
115115 run_id = "test-run-123"
116- generated_at = datetime (2024 , 1 , 31 , 12 , 0 , 0 , tzinfo = timezone . utc )
116+ generated_at = datetime (2024 , 1 , 31 , 12 , 0 , 0 , tzinfo = UTC )
117117
118118 snapshots = _collect_snapshot_data (
119119 storage = storage ,
@@ -193,8 +193,8 @@ def test_collect_snapshot_data_with_contributors() -> None:
193193 prs_reviewed = 2 ,
194194 comments = 10 ,
195195 total_score = 50 ,
196- period_start = datetime (2024 , 1 , 1 , tzinfo = timezone . utc ),
197- period_end = datetime (2024 , 1 , 31 , tzinfo = timezone . utc ),
196+ period_start = datetime (2024 , 1 , 1 , tzinfo = UTC ),
197+ period_end = datetime (2024 , 1 , 31 , tzinfo = UTC ),
198198 ),
199199 ]
200200
@@ -204,11 +204,11 @@ def test_collect_snapshot_data_with_contributors() -> None:
204204 identity_mappings = [],
205205 scores = [],
206206 member_roles = {},
207- period_start = datetime (2024 , 1 , 1 , tzinfo = timezone . utc ),
208- period_end = datetime (2024 , 1 , 31 , tzinfo = timezone . utc ),
207+ period_start = datetime (2024 , 1 , 1 , tzinfo = UTC ),
208+ period_end = datetime (2024 , 1 , 31 , tzinfo = UTC ),
209209 contribution_summaries = contribution_summaries ,
210210 run_id = "test-run" ,
211- generated_at = datetime (2024 , 1 , 31 , 12 , 0 , 0 , tzinfo = timezone . utc ),
211+ generated_at = datetime (2024 , 1 , 31 , 12 , 0 , 0 , tzinfo = UTC ),
212212 )
213213
214214 contributors = snapshots ["contributors.json" ]
@@ -246,8 +246,8 @@ def test_write_snapshots_disabled() -> None:
246246 identity_mappings = [],
247247 scores = [],
248248 member_roles = {},
249- period_start = datetime (2024 , 1 , 1 , tzinfo = timezone . utc ),
250- period_end = datetime (2024 , 1 , 31 , tzinfo = timezone . utc ),
249+ period_start = datetime (2024 , 1 , 1 , tzinfo = UTC ),
250+ period_end = datetime (2024 , 1 , 31 , tzinfo = UTC ),
251251 )
252252
253253 assert len (github_writer .files_written ) == 0
@@ -283,8 +283,8 @@ def test_write_snapshots_enabled() -> None:
283283 ],
284284 scores = [],
285285 member_roles = {},
286- period_start = datetime (2024 , 1 , 1 , tzinfo = timezone . utc ),
287- period_end = datetime (2024 , 1 , 31 , tzinfo = timezone . utc ),
286+ period_start = datetime (2024 , 1 , 1 , tzinfo = UTC ),
287+ period_end = datetime (2024 , 1 , 31 , tzinfo = UTC ),
288288 )
289289
290290 # Should have written snapshot files
@@ -332,8 +332,8 @@ def test_write_snapshots_handles_errors() -> None:
332332 identity_mappings = [],
333333 scores = [],
334334 member_roles = {},
335- period_start = datetime (2024 , 1 , 1 , tzinfo = timezone . utc ),
336- period_end = datetime (2024 , 1 , 31 , tzinfo = timezone . utc ),
335+ period_start = datetime (2024 , 1 , 1 , tzinfo = UTC ),
336+ period_end = datetime (2024 , 1 , 31 , tzinfo = UTC ),
337337 )
338338
339339 # Should not have written files due to error
@@ -468,6 +468,44 @@ def test_raw_events_respects_period_start() -> None:
468468 assert events_data [0 ]["github_user" ] == "bob"
469469
470470
471+ def test_raw_events_respects_period_end () -> None :
472+ """Only events at or before period_end are included in events.json."""
473+ storage = MockStorage ()
474+ period_end = datetime (2024 , 1 , 31 , tzinfo = UTC )
475+ storage .contributions = [
476+ ContributionEvent (
477+ github_user = "alice" ,
478+ event_type = "pr_merged" ,
479+ repo = "org/repo" ,
480+ created_at = datetime (2024 , 1 , 15 , tzinfo = UTC ), # within period
481+ payload = {},
482+ ),
483+ ContributionEvent (
484+ github_user = "bob" ,
485+ event_type = "pr_merged" ,
486+ repo = "org/repo" ,
487+ created_at = datetime (2024 , 2 , 5 , tzinfo = UTC ), # after period_end
488+ payload = {},
489+ ),
490+ ]
491+ snapshots = _collect_snapshot_data (
492+ storage = storage ,
493+ config = _make_config (),
494+ identity_mappings = [],
495+ scores = [],
496+ member_roles = {},
497+ period_start = datetime (2024 , 1 , 1 , tzinfo = UTC ),
498+ period_end = period_end ,
499+ contribution_summaries = None ,
500+ run_id = "test-run" ,
501+ generated_at = datetime (2024 , 1 , 31 , 12 , 0 , 0 , tzinfo = UTC ),
502+ include_raw_events = True ,
503+ )
504+ events_data = snapshots ["events.json" ]["data" ]
505+ assert len (events_data ) == 1
506+ assert events_data [0 ]["github_user" ] == "alice"
507+
508+
471509def test_write_snapshots_raw_events_via_config () -> None :
472510 """include_raw_events=True in SnapshotConfig results in events.json being written."""
473511 storage = MockStorage ()
0 commit comments