Files
hi-server/internal/logic/public/user/getInviteRecordsLogic_test.go
T
shanshanzhong147 197fed7d12
Build docker and publish / build (20.15.1) (push) Failing after 10m56s
Build docker and publish / build (20.15.1) (pull_request) Successful in 8m50s
新功能(#102): 新增邀请记录接口并删除旧邀请销售接口
Co-authored-by: multica-agent <github@multica.ai>
2026-05-27 22:50:16 -07:00

158 lines
5.3 KiB
Go

package user
import (
"context"
"fmt"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
modeluser "github.com/perfect-panel/server/internal/model/user"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/constant"
"github.com/perfect-panel/server/pkg/hash"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func TestGetInviteRecordsInviter(t *testing.T) {
svcCtx, mock, cleanup := newInviteRecordsTestSvc(t)
defer cleanup()
mock.ExpectQuery("count(*)").
WithArgs(34, int64(100), "邀请赠送").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
mock.ExpectQuery("SELECT id, object_id, content").
WithArgs(34, int64(100), "邀请赠送", 10).
WillReturnRows(sqlmock.NewRows([]string{"id", "object_id", "content", "created_at"}).
AddRow(1, 100, `{"order_no":"order-1","amount":7,"remark":"邀请赠送"}`, 1779934580000))
mock.ExpectQuery("SELECT order_no, user_id FROM `order`").
WithArgs("order-1").
WillReturnRows(sqlmock.NewRows([]string{"order_no", "user_id"}).AddRow("order-1", 200))
resp, err := NewGetInviteRecordsLogic(inviteRecordsContext(100, 0), svcCtx).GetInviteRecords(&types.GetInviteRecordsRequest{Page: 1, Size: 10})
if err != nil {
t.Fatalf("GetInviteRecords returned error: %v", err)
}
assertInviteRecordResponse(t, resp, types.InviteRecord{
Role: inviteRecordRoleInviter,
PeerHash: hash.InvitePeerHash(200),
GiftDays: 7,
OrderNo: "order-1",
CreatedAt: 1779934580000,
})
assertInviteRecordsExpectations(t, mock)
}
func TestGetInviteRecordsInvitee(t *testing.T) {
svcCtx, mock, cleanup := newInviteRecordsTestSvc(t)
defer cleanup()
mock.ExpectQuery("count(*)").
WithArgs(34, int64(200), "邀请赠送").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
mock.ExpectQuery("SELECT id, object_id, content").
WithArgs(34, int64(200), "邀请赠送", 10).
WillReturnRows(sqlmock.NewRows([]string{"id", "object_id", "content", "created_at"}).
AddRow(2, 200, `{"order_no":"order-2","amount":7,"remark":"邀请赠送"}`, 1779934590000))
mock.ExpectQuery("SELECT order_no, user_id FROM `order`").
WithArgs("order-2").
WillReturnRows(sqlmock.NewRows([]string{"order_no", "user_id"}).AddRow("order-2", 200))
resp, err := NewGetInviteRecordsLogic(inviteRecordsContext(200, 100), svcCtx).GetInviteRecords(&types.GetInviteRecordsRequest{Page: 1, Size: 10})
if err != nil {
t.Fatalf("GetInviteRecords returned error: %v", err)
}
assertInviteRecordResponse(t, resp, types.InviteRecord{
Role: inviteRecordRoleInvitee,
PeerHash: hash.InvitePeerHash(100),
GiftDays: 7,
OrderNo: "order-2",
CreatedAt: 1779934590000,
})
assertInviteRecordsExpectations(t, mock)
}
func TestGetInviteRecordsMissingOrderReturnsDirtyRecord(t *testing.T) {
svcCtx, mock, cleanup := newInviteRecordsTestSvc(t)
defer cleanup()
mock.ExpectQuery("count(*)").
WithArgs(34, int64(100), "邀请赠送").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
mock.ExpectQuery("SELECT id, object_id, content").
WithArgs(34, int64(100), "邀请赠送", 10).
WillReturnRows(sqlmock.NewRows([]string{"id", "object_id", "content", "created_at"}).
AddRow(3, 100, `{"order_no":"missing-order","amount":7,"remark":"邀请赠送"}`, 1779934600000))
mock.ExpectQuery("SELECT order_no, user_id FROM `order`").
WithArgs("missing-order").
WillReturnRows(sqlmock.NewRows([]string{"order_no", "user_id"}))
resp, err := NewGetInviteRecordsLogic(inviteRecordsContext(100, 0), svcCtx).GetInviteRecords(&types.GetInviteRecordsRequest{Page: 1, Size: 10})
if err != nil {
t.Fatalf("GetInviteRecords returned error: %v", err)
}
assertInviteRecordResponse(t, resp, types.InviteRecord{
Role: inviteRecordRoleInviter,
GiftDays: 7,
OrderNo: "missing-order",
CreatedAt: 1779934600000,
})
assertInviteRecordsExpectations(t, mock)
}
func newInviteRecordsTestSvc(t *testing.T) (*svc.ServiceContext, sqlmock.Sqlmock, func()) {
t.Helper()
sqlDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherFunc(func(expectedSQL, actualSQL string) error {
if strings.Contains(actualSQL, expectedSQL) {
return nil
}
return fmt.Errorf("actual sql %q does not contain %q", actualSQL, expectedSQL)
})))
if err != nil {
t.Fatalf("create sqlmock: %v", err)
}
db, err := gorm.Open(mysql.New(mysql.Config{Conn: sqlDB, SkipInitializeWithVersion: true}), &gorm.Config{})
if err != nil {
_ = sqlDB.Close()
t.Fatalf("open gorm db: %v", err)
}
return &svc.ServiceContext{DB: db}, mock, func() {
_ = sqlDB.Close()
}
}
func inviteRecordsContext(userId, refererId int64) context.Context {
return context.WithValue(context.Background(), constant.CtxKeyUser, &modeluser.User{
Id: userId,
RefererId: refererId,
})
}
func assertInviteRecordResponse(t *testing.T, resp *types.GetInviteRecordsResponse, want types.InviteRecord) {
t.Helper()
if resp == nil {
t.Fatal("response is nil")
}
if resp.Total != 1 {
t.Fatalf("Total = %d, want 1", resp.Total)
}
if len(resp.List) != 1 {
t.Fatalf("len(List) = %d, want 1", len(resp.List))
}
if got := resp.List[0]; got != want {
t.Fatalf("record = %+v, want %+v", got, want)
}
}
func assertInviteRecordsExpectations(t *testing.T, mock sqlmock.Sqlmock) {
t.Helper()
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sql expectations: %v", err)
}
}