From 45dcce0e49b979c4eeea9002b844dc5e923bb681 Mon Sep 17 00:00:00 2001 From: lyen1688 Date: Fri, 14 Aug 2026 23:48:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E9=9A=94=E7=A6=BB?= =?UTF-8?q?=E5=88=86=E7=BB=84=E7=94=A8=E9=87=8F=E4=BB=93=E5=82=A8=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=97=B6=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 分组用量仓储测试原先固定恢复上海时区,覆盖集成测试包初始化的 UTC,并导致后续今日统计多算。统一保存并恢复测试进入前的时区,避免 unit 和 integration 用例相互污染。 --- .../custom_group_usage_timezone_test.go | 16 ++++++++++++++++ .../dashboard_aggregation_group_usage_test.go | 7 ++----- ...roup_usage_rollup_trigger_integration_test.go | 10 +++------- .../repository/usage_cleanup_repo_test.go | 4 +--- .../usage_log_repo_group_summary_test.go | 4 +--- 5 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 backend/internal/repository/custom_group_usage_timezone_test.go diff --git a/backend/internal/repository/custom_group_usage_timezone_test.go b/backend/internal/repository/custom_group_usage_timezone_test.go new file mode 100644 index 0000000000..c77c5b41bc --- /dev/null +++ b/backend/internal/repository/custom_group_usage_timezone_test.go @@ -0,0 +1,16 @@ +package repository + +import ( + "testing" + + appTimezone "github.com/Wei-Shaw/sub2api/internal/pkg/timezone" + "github.com/stretchr/testify/require" +) + +func useGroupUsageRepositoryTestTimezone(t *testing.T, name string) { + t.Helper() + + previousName := appTimezone.Name() + require.NoError(t, appTimezone.Init(name)) + t.Cleanup(func() { require.NoError(t, appTimezone.Init(previousName)) }) +} diff --git a/backend/internal/repository/dashboard_aggregation_group_usage_test.go b/backend/internal/repository/dashboard_aggregation_group_usage_test.go index 98c2264c8c..cbb5d3c6b6 100644 --- a/backend/internal/repository/dashboard_aggregation_group_usage_test.go +++ b/backend/internal/repository/dashboard_aggregation_group_usage_test.go @@ -10,7 +10,6 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" - appTimezone "github.com/Wei-Shaw/sub2api/internal/pkg/timezone" "github.com/Wei-Shaw/sub2api/internal/service" "github.com/stretchr/testify/require" ) @@ -32,8 +31,7 @@ func TestDashboardAggregationRepositorySyncGroupUsageRollupsNoopsAtCurrentDate(t } func TestDashboardAggregationRepositorySyncGroupUsageRollupsRebuildsWhenTimezoneChanges(t *testing.T) { - require.NoError(t, appTimezone.Init("America/New_York")) - t.Cleanup(func() { require.NoError(t, appTimezone.Init("Asia/Shanghai")) }) + useGroupUsageRepositoryTestTimezone(t, "America/New_York") db, mock := newSQLMock(t) repo := newDashboardAggregationRepositoryWithSQL(db) @@ -317,6 +315,5 @@ func TestDashboardAggregationRepositoryCleanupUsageLogsPartitionFailureRollsBack func setGroupUsageRollupTestTimezone(t *testing.T) { t.Helper() - require.NoError(t, appTimezone.Init("Asia/Shanghai")) - t.Cleanup(func() { require.NoError(t, appTimezone.Init("Asia/Shanghai")) }) + useGroupUsageRepositoryTestTimezone(t, "Asia/Shanghai") } diff --git a/backend/internal/repository/group_usage_rollup_trigger_integration_test.go b/backend/internal/repository/group_usage_rollup_trigger_integration_test.go index c12f0c3a41..db182be495 100644 --- a/backend/internal/repository/group_usage_rollup_trigger_integration_test.go +++ b/backend/internal/repository/group_usage_rollup_trigger_integration_test.go @@ -9,7 +9,6 @@ import ( "testing" "time" - appTimezone "github.com/Wei-Shaw/sub2api/internal/pkg/timezone" "github.com/Wei-Shaw/sub2api/migrations" "github.com/lib/pq" "github.com/stretchr/testify/require" @@ -259,8 +258,7 @@ func TestGroupUsageRollupTriggerUsesSessionTimezoneAcrossDST(t *testing.T) { func TestGroupUsageSummaryIncludesYesterdayAcrossWatermark(t *testing.T) { ctx := context.Background() - require.NoError(t, appTimezone.Init("Asia/Shanghai")) - t.Cleanup(func() { require.NoError(t, appTimezone.Init("Asia/Shanghai")) }) + useGroupUsageRepositoryTestTimezone(t, "Asia/Shanghai") todayStart := time.Date(2026, 8, 13, 16, 0, 0, 0, time.UTC) tests := []struct { @@ -317,8 +315,7 @@ func TestGroupUsageSummaryIncludesYesterdayAcrossWatermark(t *testing.T) { func TestGroupUsageRollupSyncRebuildsAfterTimezoneChange(t *testing.T) { ctx := context.Background() - require.NoError(t, appTimezone.Init("America/New_York")) - t.Cleanup(func() { require.NoError(t, appTimezone.Init("Asia/Shanghai")) }) + useGroupUsageRepositoryTestTimezone(t, "America/New_York") todayStart := time.Date(2026, 3, 9, 4, 0, 0, 0, time.UTC) schema := createGroupUsageRollupTriggerTestSchema(t, ctx, false) tx := beginGroupUsageRollupTriggerTestTx(t, ctx, schema) @@ -373,8 +370,7 @@ func TestGroupUsageRollupSyncRebuildsAfterTimezoneChange(t *testing.T) { func TestGroupUsageSummaryUsesConfiguredDSTBoundaries(t *testing.T) { ctx := context.Background() - require.NoError(t, appTimezone.Init("America/New_York")) - t.Cleanup(func() { require.NoError(t, appTimezone.Init("Asia/Shanghai")) }) + useGroupUsageRepositoryTestTimezone(t, "America/New_York") todayStart := time.Date(2026, 3, 9, 4, 0, 0, 0, time.UTC) schema := createGroupUsageRollupTriggerTestSchema(t, ctx, false) tx := beginGroupUsageRollupTriggerTestTx(t, ctx, schema) diff --git a/backend/internal/repository/usage_cleanup_repo_test.go b/backend/internal/repository/usage_cleanup_repo_test.go index 1437dbd0ca..b147d03a4f 100644 --- a/backend/internal/repository/usage_cleanup_repo_test.go +++ b/backend/internal/repository/usage_cleanup_repo_test.go @@ -9,7 +9,6 @@ import ( "github.com/DATA-DOG/go-sqlmock" "github.com/Wei-Shaw/sub2api/internal/pkg/pagination" - appTimezone "github.com/Wei-Shaw/sub2api/internal/pkg/timezone" "github.com/Wei-Shaw/sub2api/internal/service" "github.com/stretchr/testify/require" ) @@ -24,8 +23,7 @@ func newSQLMock(t *testing.T) (*sql.DB, sqlmock.Sqlmock) { func setUsageCleanupRollupTestTimezone(t *testing.T) { t.Helper() - require.NoError(t, appTimezone.Init("Asia/Shanghai")) - t.Cleanup(func() { require.NoError(t, appTimezone.Init("Asia/Shanghai")) }) + useGroupUsageRepositoryTestTimezone(t, "Asia/Shanghai") } func TestNewUsageCleanupRepository(t *testing.T) { diff --git a/backend/internal/repository/usage_log_repo_group_summary_test.go b/backend/internal/repository/usage_log_repo_group_summary_test.go index 124744c0aa..f67facb4b9 100644 --- a/backend/internal/repository/usage_log_repo_group_summary_test.go +++ b/backend/internal/repository/usage_log_repo_group_summary_test.go @@ -8,15 +8,13 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" - appTimezone "github.com/Wei-Shaw/sub2api/internal/pkg/timezone" "github.com/stretchr/testify/require" ) func TestUsageLogRepositoryGetAllGroupUsageSummaryUsesRollupTail(t *testing.T) { db, mock := newSQLMock(t) repo := newUsageLogRepositoryWithSQL(nil, db) - require.NoError(t, appTimezone.Init("America/New_York")) - t.Cleanup(func() { require.NoError(t, appTimezone.Init("Asia/Shanghai")) }) + useGroupUsageRepositoryTestTimezone(t, "America/New_York") todayStart := time.Date(2026, 3, 9, 4, 0, 0, 0, time.UTC) yesterdayStart := time.Date(2026, 3, 8, 5, 0, 0, 0, time.UTC)