140 lines
3.8 KiB
Go
140 lines
3.8 KiB
Go
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()
|
||
|
||
req := mockGen.GenerateCampaignReportRequest()
|
||
if req == nil {
|
||
t.Error("请求数据生成失败")
|
||
return
|
||
}
|
||
|
||
fmt.Printf("✓ Mock 请求生成成功:AdvertiserID=%d\n", req.AdvertiserID)
|
||
}
|
||
|
||
func TestDataConverter(t *testing.T) {
|
||
converter := NewDataConverter()
|
||
mockGen := NewMockDataGenerator()
|
||
|
||
responseData := mockGen.GenerateCampaignReportResponse()
|
||
if responseData == nil || responseData.Data.Sum == nil {
|
||
t.Fatal("Mock 数据生成失败")
|
||
}
|
||
|
||
sumItem := converter.ConvertToSumItem(responseData.Data.Sum, "campaign_report")
|
||
if sumItem == nil {
|
||
t.Fatal("转换为汇总数据失败")
|
||
}
|
||
|
||
if sumItem.CampaignName == "" {
|
||
t.Error("计划名称为空")
|
||
}
|
||
|
||
fmt.Printf("✓ 汇总数据转换成功:计划=%s\n", sumItem.CampaignName)
|
||
|
||
detailItems := converter.ConvertToDetailItems(responseData.Data.Detail, "campaign_report")
|
||
if len(detailItems) == 0 {
|
||
t.Fatal("转换为明细数据失败")
|
||
}
|
||
|
||
fmt.Printf("✓ 明细数据转换成功:数量=%d\n", len(detailItems))
|
||
}
|
||
|
||
func TestSyncCampaignReportWithDB(t *testing.T) {
|
||
ctx := gctx.New()
|
||
syncService := NewSyncService()
|
||
|
||
req := &CampaignReportRequest{
|
||
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,
|
||
}
|
||
|
||
result, err := syncService.SyncCampaignReport(ctx, req, true)
|
||
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()
|
||
//
|
||
// req := &CampaignReportRequest{
|
||
// 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("=== 开始执行定时同步任务 ===")
|
||
// result, err := syncService.SyncCampaignReportWithPagination(ctx, req, true, 3)
|
||
// 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)
|
||
//}
|
||
|
||
func BenchmarkSyncCampaignReport(b *testing.B) {
|
||
ctx := gctx.New()
|
||
syncService := NewSyncService()
|
||
req := &CampaignReportRequest{
|
||
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++ {
|
||
_, _ = syncService.SyncCampaignReport(ctx, req, true)
|
||
}
|
||
}
|