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)