SocialRepository.Follow 和 Unfollow 返回本次是否真的写入。判重交给唯一索引 idx_social_follower_vlogger。 因此重复关注和重复取关返回 200。重复请求不再落成 500。 缓存失效与通知只在真的变更时发生。自关注返回 400。响应体新增 created 与 deleted 字段。 SocialWorker 的调用只适配新签名,职责不变。删除它由后续提交完成。 验证:go build ./... 通过,go test -count=1 ./internal/social/... ./internal/worker/... 全绿。
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
//go:build integration
|
||
|
||
package social
|
||
|
||
import (
|
||
"context"
|
||
"os"
|
||
"strings"
|
||
"testing"
|
||
|
||
"gorm.io/driver/mysql"
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/logger"
|
||
)
|
||
|
||
// 判重由唯一索引 idx_social_follower_vlogger 裁决,"这次有没有真的写入"只能对着
|
||
// 真实 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/social/
|
||
|
||
func newSocialTestDB(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(&Social{}); err != nil {
|
||
t.Fatalf("drop table: %v", err)
|
||
}
|
||
if err := db.AutoMigrate(&Social{}); err != nil {
|
||
t.Fatalf("migrate: %v", err)
|
||
}
|
||
return db
|
||
}
|
||
|
||
func TestSocialRepository_Follow_DuplicateReportsNotCreated(t *testing.T) {
|
||
db := newSocialTestDB(t)
|
||
repo := NewSocialRepository(db)
|
||
ctx := context.Background()
|
||
|
||
created, err := repo.Follow(ctx, &Social{FollowerID: 1, VloggerID: 2})
|
||
if err != nil || !created {
|
||
t.Fatalf("首次关注 = (%v, %v),想要 (true, nil)", created, err)
|
||
}
|
||
|
||
created, err = repo.Follow(ctx, &Social{FollowerID: 1, VloggerID: 2})
|
||
if err != nil || created {
|
||
t.Fatalf("重复关注 = (%v, %v),想要 (false, nil)", created, err)
|
||
}
|
||
|
||
var count int64
|
||
if err := db.Model(&Social{}).Count(&count).Error; err != nil {
|
||
t.Fatalf("count: %v", err)
|
||
}
|
||
if count != 1 {
|
||
t.Fatalf("socials 行数 = %d,想要 1", count)
|
||
}
|
||
}
|
||
|
||
func TestSocialRepository_Unfollow_SecondCallReportsNotDeleted(t *testing.T) {
|
||
db := newSocialTestDB(t)
|
||
repo := NewSocialRepository(db)
|
||
ctx := context.Background()
|
||
if _, err := repo.Follow(ctx, &Social{FollowerID: 3, VloggerID: 4}); err != nil {
|
||
t.Fatalf("准备关注失败: %v", err)
|
||
}
|
||
|
||
deleted, err := repo.Unfollow(ctx, &Social{FollowerID: 3, VloggerID: 4})
|
||
if err != nil || !deleted {
|
||
t.Fatalf("首次取关 = (%v, %v),想要 (true, nil)", deleted, err)
|
||
}
|
||
|
||
deleted, err = repo.Unfollow(ctx, &Social{FollowerID: 3, VloggerID: 4})
|
||
if err != nil || deleted {
|
||
t.Fatalf("重复取关 = (%v, %v),想要 (false, nil)", deleted, err)
|
||
}
|
||
}
|