修复:隔离分组用量仓储测试时区

分组用量仓储测试原先固定恢复上海时区,覆盖集成测试包初始化的 UTC,并导致后续今日统计多算。统一保存并恢复测试进入前的时区,避免 unit 和 integration 用例相互污染。
This commit is contained in:
lyen1688
2026-08-14 23:48:03 +08:00
parent 89d826be29
commit 45dcce0e49
5 changed files with 23 additions and 18 deletions
@@ -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)) })
}
@@ -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")
}
@@ -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)
@@ -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) {
@@ -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)