Files
data-engine/sync/sync_test.go

136 lines
3.7 KiB
Go
Raw Normal View History

2026-04-07 09:51:32 +08:00
package sync
import (
"fmt"
"testing"
"time"
_ "github.com/gogf/gf/contrib/drivers/pgsql/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func init() {
fmt.Println("=== 初始化测试环境 ===")
ctx := gctx.New()
db := g.DB()
if db != nil {
_, err := db.Query(ctx, "SELECT 1")
if err == nil {
fmt.Println("✓ 数据库连接成功")
} else {
fmt.Printf("⚠️ 数据库连接失败:%v\n", err)
fmt.Println("⚠️ 将跳过数据库相关测试")
}
} else {
fmt.Println("⚠️ 数据库未初始化")
}
fmt.Println("========================")
}
func TestMockDataGeneration(t *testing.T) {
mockGen := NewMockDataGenerator()
2026-04-08 09:03:20 +08:00
req := mockGen.GenerateAccountReportRequest()
2026-04-07 09:51:32 +08:00
if req == nil {
t.Error("请求数据生成失败")
return
}
fmt.Printf("✓ Mock 请求生成成功AdvertiserID=%d\n", req.AdvertiserID)
}
func TestDataConverter(t *testing.T) {
converter := NewDataConverter()
mockGen := NewMockDataGenerator()
2026-04-08 09:03:20 +08:00
responseData := mockGen.GenerateAccountReportResponse()
2026-04-07 09:51:32 +08:00
if responseData == nil || responseData.Data.Sum == nil {
t.Fatal("Mock 数据生成失败")
}
2026-04-08 09:03:20 +08:00
sumItem := converter.ConvertToSumItem(responseData.Data.Sum, "account_report", 1)
2026-04-07 09:51:32 +08:00
if sumItem == nil {
t.Fatal("转换为汇总数据失败")
}
fmt.Printf("✓ 汇总数据转换成功:计划=%s\n", sumItem.CampaignName)
2026-04-08 09:03:20 +08:00
detailItems := converter.ConvertToDetailItems(responseData.Data.Detail, "account_report", 1)
2026-04-07 09:51:32 +08:00
if len(detailItems) == 0 {
t.Fatal("转换为明细数据失败")
}
fmt.Printf("✓ 明细数据转换成功:数量=%d\n", len(detailItems))
}
2026-04-08 09:03:20 +08:00
func TestSyncAccountReportWithDB(t *testing.T) {
2026-04-07 09:51:32 +08:00
ctx := gctx.New()
syncService := NewSyncService()
2026-04-08 09:03:20 +08:00
req := &AccountReportRequest{
2026-04-07 09:51:32 +08:00
AdvertiserID: 10001,
StartTime: time.Now().AddDate(0, 0, -30).UnixNano() / 1e6,
EndTime: time.Now().UnixNano() / 1e6,
SelectColumns: []string{"impression", "click", "cost", "t0GMV"},
GroupType: 1,
QueryVersion: 1,
}
2026-04-08 09:03:20 +08:00
result, err := syncService.SyncAccountReport(ctx, req, true)
2026-04-07 09:51:32 +08:00
if err != nil {
t.Logf("同步失败(可能是数据库问题): %v", err)
return
}
fmt.Printf("✓ 同步结果:汇总成功=%v, 汇总 ID=%d, 明细数量=%d\n",
result.SumSuccess, result.SumID, result.DetailCount)
}
//
//// TestScheduledSyncTask 测试定时同步任务(每小时执行一次,全量分页抽取)
//func TestScheduledSyncTask(t *testing.T) {
// ctx := gctx.New()
// syncService := NewSyncService()
//
2026-04-08 09:03:20 +08:00
// req := &AccountReportRequest{
2026-04-07 09:51:32 +08:00
// AdvertiserID: 10001,
// StartTime: time.Now().AddDate(0, 0, -30).UnixNano() / 1e6,
// EndTime: time.Now().UnixNano() / 1e6,
// SelectColumns: []string{"impression", "click", "cost", "t0GMV"},
// GroupType: 1,
// QueryVersion: 1,
// }
//
// logrus.Info("=== 开始执行定时同步任务 ===")
2026-04-08 09:03:20 +08:00
// result, err := syncService.SyncAccountReportWithPagination(ctx, req, true, 3)
2026-04-07 09:51:32 +08:00
// if err != nil {
// t.Logf("定时同步任务失败:%v", err)
// return
// }
//
// fmt.Printf("✓ 定时同步完成:\n")
// fmt.Printf(" 汇总数据:成功=%v, ID=%d\n", result.SumSuccess, result.SumID)
// fmt.Printf(" 明细数据:总数=%d, 成功=%d, 失败=%d\n",
// result.DetailCount, result.DetailSuccessCount, result.DetailFailCount)
//}
2026-04-08 09:03:20 +08:00
func BenchmarkSyncAccountReport(b *testing.B) {
2026-04-07 09:51:32 +08:00
ctx := gctx.New()
syncService := NewSyncService()
2026-04-08 09:03:20 +08:00
req := &AccountReportRequest{
2026-04-07 09:51:32 +08:00
AdvertiserID: 10001,
StartTime: time.Now().AddDate(0, 0, -30).UnixNano() / 1e6,
EndTime: time.Now().UnixNano() / 1e6,
SelectColumns: []string{"impression", "click", "cost"},
GroupType: 1,
QueryVersion: 1,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
2026-04-08 09:03:20 +08:00
_, _ = syncService.SyncAccountReport(ctx, req, true)
2026-04-07 09:51:32 +08:00
}
}