Files
VLoop/backend/internal/social/service_test.go
huanghaosheng f658b7e76e fix(social): 关注与取关改为幂等写入
SocialRepository.Follow 和 Unfollow 返回本次是否真的写入。判重交给唯一索引 idx_social_follower_vlogger。

因此重复关注和重复取关返回 200。重复请求不再落成 500。

缓存失效与通知只在真的变更时发生。自关注返回 400。响应体新增 created 与 deleted 字段。

SocialWorker 的调用只适配新签名,职责不变。删除它由后续提交完成。

验证:go build ./... 通过,go test -count=1 ./internal/social/... ./internal/worker/... 全绿。
2026-11-11 16:18:49 +08:00

194 lines
5.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package social
import (
"context"
"errors"
"testing"
"feedsystem_video_go/internal/account"
)
// fakeStore 用内存行模拟 socials 表:返回值就是"这张表这次有没有变"。
type fakeStore struct {
rows map[[2]uint]bool
err error
}
func newFakeStore() *fakeStore {
return &fakeStore{rows: map[[2]uint]bool{}}
}
func (f *fakeStore) Follow(ctx context.Context, social *Social) (bool, error) {
if f.err != nil {
return false, f.err
}
key := [2]uint{social.FollowerID, social.VloggerID}
if f.rows[key] {
return false, nil
}
f.rows[key] = true
return true, nil
}
func (f *fakeStore) Unfollow(ctx context.Context, social *Social) (bool, error) {
if f.err != nil {
return false, f.err
}
key := [2]uint{social.FollowerID, social.VloggerID}
if !f.rows[key] {
return false, nil
}
delete(f.rows, key)
return true, nil
}
func (f *fakeStore) IsFollowed(ctx context.Context, social *Social) (bool, error) {
return f.rows[[2]uint{social.FollowerID, social.VloggerID}], nil
}
func (f *fakeStore) GetAllFollowers(ctx context.Context, vloggerID uint) ([]*account.Account, error) {
return nil, nil
}
func (f *fakeStore) GetAllVloggers(ctx context.Context, followerID uint) ([]*account.Account, error) {
return nil, nil
}
func (f *fakeStore) CountFollowers(ctx context.Context, vloggerID uint) (int64, error) {
return 0, nil
}
func (f *fakeStore) CountVloggers(ctx context.Context, followerID uint) (int64, error) {
return 0, nil
}
type fakeAccounts struct {
err error
}
func (f fakeAccounts) FindByID(ctx context.Context, id uint) (*account.Account, error) {
if f.err != nil {
return nil, f.err
}
return &account.Account{ID: id}, nil
}
// fakeNotifier 记录真正发出去的通知事件,而不是调用次数。
type fakeNotifier struct {
published [][2]uint
err error
}
func (f *fakeNotifier) Follow(ctx context.Context, followerID, vloggerID uint) error {
f.published = append(f.published, [2]uint{followerID, vloggerID})
return f.err
}
func newTestService(store socialStore, accounts accountFinder, notifier followNotifier) *SocialService {
return NewSocialService(store, accounts, notifier, nil)
}
func TestSocialService_Follow_SecondCallIsNoop(t *testing.T) {
store := newFakeStore()
notifier := &fakeNotifier{}
svc := newTestService(store, fakeAccounts{}, notifier)
rel := &Social{ID: 1, FollowerID: 7, VloggerID: 9}
created, err := svc.Follow(context.Background(), rel)
if err != nil || !created {
t.Fatalf("首次关注 = (%v, %v),想要 (true, nil)", created, err)
}
created, err = svc.Follow(context.Background(), &Social{FollowerID: 7, VloggerID: 9})
if err != nil || created {
t.Fatalf("重复关注 = (%v, %v),想要 (false, nil)", created, err)
}
if len(store.rows) != 1 {
t.Fatalf("socials 行数 = %d,想要 1", len(store.rows))
}
// 重复点击不该产生第二条通知。
if len(notifier.published) != 1 {
t.Fatalf("通知事件数 = %d,想要 1", len(notifier.published))
}
}
func TestSocialService_Follow_SelfIsRejected(t *testing.T) {
store := newFakeStore()
notifier := &fakeNotifier{}
svc := newTestService(store, fakeAccounts{}, notifier)
created, err := svc.Follow(context.Background(), &Social{FollowerID: 3, VloggerID: 3})
if created || !errors.Is(err, ErrCannotFollowSelf) {
t.Fatalf("自关注 = (%v, %v),想要 (false, ErrCannotFollowSelf)", created, err)
}
if len(store.rows) != 0 {
t.Fatalf("自关注写入了 %d 行,想要 0", len(store.rows))
}
if len(notifier.published) != 0 {
t.Fatalf("自关注发出了 %d 条通知,想要 0", len(notifier.published))
}
}
func TestSocialService_Follow_RepoErrorPropagates(t *testing.T) {
repoErr := errors.New("db is down")
store := newFakeStore()
store.err = repoErr
notifier := &fakeNotifier{}
svc := newTestService(store, fakeAccounts{}, notifier)
created, err := svc.Follow(context.Background(), &Social{FollowerID: 1, VloggerID: 2})
if created || !errors.Is(err, repoErr) {
t.Fatalf("仓储报错 = (%v, %v),想要 (false, db is down)", created, err)
}
if len(notifier.published) != 0 {
t.Fatalf("仓储报错仍发出了 %d 条通知,想要 0", len(notifier.published))
}
}
func TestSocialService_Follow_NotifyFailureKeepsSuccess(t *testing.T) {
store := newFakeStore()
notifier := &fakeNotifier{err: errors.New("mq is down")}
svc := newTestService(store, fakeAccounts{}, notifier)
created, err := svc.Follow(context.Background(), &Social{FollowerID: 1, VloggerID: 2})
if !created || err != nil {
t.Fatalf("通知失败时 Follow = (%v, %v),想要 (true, nil)", created, err)
}
}
func TestSocialService_Follow_AccountLookupErrorPropagates(t *testing.T) {
lookupErr := errors.New("account not found")
store := newFakeStore()
svc := newTestService(store, fakeAccounts{err: lookupErr}, &fakeNotifier{})
created, err := svc.Follow(context.Background(), &Social{FollowerID: 1, VloggerID: 2})
if created || !errors.Is(err, lookupErr) {
t.Fatalf("账号校验失败 = (%v, %v),想要 (false, account not found)", created, err)
}
if len(store.rows) != 0 {
t.Fatalf("账号校验失败仍写入了 %d 行,想要 0", len(store.rows))
}
}
func TestSocialService_Unfollow_SecondCallIsNoop(t *testing.T) {
store := newFakeStore()
notifier := &fakeNotifier{}
svc := newTestService(store, fakeAccounts{}, notifier)
if _, err := svc.Follow(context.Background(), &Social{FollowerID: 7, VloggerID: 9}); err != nil {
t.Fatalf("准备关注失败: %v", err)
}
deleted, err := svc.Unfollow(context.Background(), &Social{FollowerID: 7, VloggerID: 9})
if err != nil || !deleted {
t.Fatalf("首次取关 = (%v, %v),想要 (true, nil)", deleted, err)
}
deleted, err = svc.Unfollow(context.Background(), &Social{FollowerID: 7, VloggerID: 9})
if err != nil || deleted {
t.Fatalf("重复取关 = (%v, %v),想要 (false, nil)", deleted, err)
}
if len(store.rows) != 0 {
t.Fatalf("取关后还有 %d 行,想要 0", len(store.rows))
}
}