//go:build integration package feed import ( "context" "os" "strings" "testing" "time" "feedsystem_video_go/internal/video" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" ) // 这些用例需要真实 MySQL:ListHotWindow 的排序定义(分数降序、同分按 id 降序) // 必须与 Redis 路径逐字一致,而 SQL 的 GROUP BY / CAST / ORDER BY 语义 // 用替身验证等于什么都没验证。 // // docker compose up -d mysql // MYSQL_TEST_DSN='root:123456@tcp(127.0.0.1:3306)/feedsystem_test?charset=utf8mb4&parseTime=True&loc=Local' \ // go test -tags integration -count=1 ./internal/feed/ func newFeedTestDB(t *testing.T) *gorm.DB { t.Helper() dsn := os.Getenv("MYSQL_TEST_DSN") if dsn == "" { t.Skip("MYSQL_TEST_DSN 未设置:需要真实 MySQL(见本文件顶部注释)") } if strings.Contains(dsn, "clientFoundRows") { t.Fatalf("测试 DSN 不能带 clientFoundRows:它会破坏 RowsAffected 判重") } db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }) if err != nil { t.Fatalf("connect mysql: %v", err) } if err := db.Migrator().DropTable(&video.Video{}, &video.Like{}, &video.Comment{}); err != nil { t.Fatalf("drop tables: %v", err) } if err := db.AutoMigrate(&video.Video{}, &video.Like{}, &video.Comment{}); err != nil { t.Fatalf("migrate: %v", err) } return db } func seedFeedVideo(t *testing.T, db *gorm.DB) *video.Video { t.Helper() v := &video.Video{ AuthorID: 1, Username: "author", Title: "t", PlayURL: "p", CoverURL: "c", } if err := db.Create(v).Error; err != nil { t.Fatalf("seed video: %v", err) } return v } func seedLikeAt(t *testing.T, db *gorm.DB, videoID, accountID uint, at time.Time) { t.Helper() if err := db.Create(&video.Like{VideoID: videoID, AccountID: accountID, CreatedAt: at}).Error; err != nil { t.Fatalf("seed like: %v", err) } } func seedCommentAt(t *testing.T, db *gorm.DB, videoID uint, at time.Time) { t.Helper() c := &video.Comment{Username: "alice", VideoID: videoID, AuthorID: 1, Content: "x", CreatedAt: at} if err := db.Create(c).Error; err != nil { t.Fatalf("seed comment: %v", err) } } // Redis 路径用补零 member,同分时按 member 字符串降序 —— 也就是 id 降序。 // DB 回退必须复刻同一个顺序,否则同一次请求在两条路径上看到两个榜。 func TestListHotWindow_OrdersByScoreThenIDDesc(t *testing.T) { db := newFeedTestDB(t) repo := NewFeedRepository(db) ctx := context.Background() low := seedFeedVideo(t, db) highA := seedFeedVideo(t, db) highB := seedFeedVideo(t, db) asOf := time.Now().UTC().Truncate(time.Minute).Add(-time.Minute) at := asOf.Add(-5 * time.Minute) to := asOf.Add(time.Minute) // 分数:highA = highB = 3(同分,按 id DESC 应 highB 在前),low = 1。 for i := uint(1); i <= 3; i++ { seedLikeAt(t, db, highA.ID, i, at) } seedLikeAt(t, db, highB.ID, 1, at) seedCommentAt(t, db, highB.ID, at) seedCommentAt(t, db, highB.ID, at.Add(time.Second)) seedLikeAt(t, db, low.ID, 1, at) // 窗口外的互动不得进入结果。 seedLikeAt(t, db, low.ID, 2, to) rows, err := repo.ListHotWindow(ctx, asOf.Add(-59*time.Minute), to, 10, 0) if err != nil { t.Fatalf("ListHotWindow: %v", err) } wantIDs := []uint{highB.ID, highA.ID, low.ID} if len(rows) != len(wantIDs) { t.Fatalf("rows = %+v, want %d 行", rows, len(wantIDs)) } for i, want := range wantIDs { if rows[i].VideoID != want { t.Fatalf("第 %d 行 video_id = %d, want %d(分数降序,同分 id 降序)", i, rows[i].VideoID, want) } } if rows[0].Score != 3 || rows[2].Score != 1 { t.Fatalf("分数 = %d/%d, want 3/1", rows[0].Score, rows[2].Score) } }