1+ from datetime import date
12from unittest .mock import patch
23
34import pytest
45
56from django .test .client import RequestFactory
7+ from django .utils import timezone
68
7- from pontoon .messaging .emails import send_verification_email
9+ from pontoon .insights .models import LocaleInsightsSnapshot
10+ from pontoon .messaging .emails import (
11+ _get_monthly_locale_stats ,
12+ send_verification_email ,
13+ )
14+ from pontoon .test .factories import LocaleFactory
815
916
1017@pytest .mark .django_db
@@ -21,3 +28,38 @@ def test_send_verification_email(member):
2128 kwargs = mock_email_message .call_args .kwargs
2229 assert link in kwargs ["body" ]
2330 assert kwargs ["to" ] == [request .user .email ]
31+
32+
33+ @pytest .mark .django_db
34+ def test_get_monthly_locale_stats_uses_end_of_month_snapshot ():
35+ locale = LocaleFactory (code = "x-test" , name = "Test Language" )
36+
37+ # Simulate 6 strings added on October 30.
38+ LocaleInsightsSnapshot .objects .create (
39+ locale = locale ,
40+ created_at = date (2025 , 10 , 31 ),
41+ total_strings = 100 ,
42+ approved_strings = 94 ,
43+ completion = 94.0 ,
44+ )
45+
46+ # Nov 1 snapshot is taken at midnight, capturing end of Oct 31.
47+ # The 6 strings were translated.
48+ snapshot_nov_1 = LocaleInsightsSnapshot .objects .create (
49+ locale = locale ,
50+ created_at = date (2025 , 11 , 1 ),
51+ total_strings = 100 ,
52+ approved_strings = 100 ,
53+ completion = 100.0 ,
54+ )
55+
56+ with patch ("pontoon.messaging.emails.timezone" ) as mock_tz :
57+ mock_tz .now .return_value = timezone .datetime (
58+ 2025 , 11 , 1 , 6 , 30 , 0 , tzinfo = timezone .utc
59+ )
60+ result = _get_monthly_locale_stats (months_ago = 1 )
61+
62+ assert locale .pk in result
63+ assert result [locale .pk ].pk == snapshot_nov_1 .pk
64+ assert result [locale .pk ].approved_strings == 100
65+ assert result [locale .pk ].completion == 100.0
0 commit comments