145 lines
3.2 KiB
Go
145 lines
3.2 KiB
Go
|
package accountlogic
|
||
|
|
||
|
import (
|
||
|
"app-cloudep-member-server/gen_result/pb/member"
|
||
|
libMock "app-cloudep-member-server/internal/mock/lib"
|
||
|
modelMock "app-cloudep-member-server/internal/mock/model"
|
||
|
"app-cloudep-member-server/internal/svc"
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/alicebob/miniredis/v2"
|
||
|
"github.com/go-sql-driver/mysql"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
||
|
"go.uber.org/mock/gomock"
|
||
|
"google.golang.org/protobuf/proto"
|
||
|
)
|
||
|
|
||
|
func TestBindUserInfoLogic_BindUserInfo(t *testing.T) {
|
||
|
// mock
|
||
|
ctrl := gomock.NewController(t)
|
||
|
defer ctrl.Finish()
|
||
|
|
||
|
mockUserModel := modelMock.NewMockUserTableModel(ctrl)
|
||
|
mockValidate := libMock.NewMockValidate(ctrl)
|
||
|
|
||
|
r1, err := miniredis.Run()
|
||
|
assert.NoError(t, err)
|
||
|
defer r1.Close()
|
||
|
|
||
|
newRedis, err := redis.NewRedis(redis.RedisConf{
|
||
|
Host: r1.Addr(),
|
||
|
Type: redis.ClusterType,
|
||
|
Pass: "",
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
|
|
||
|
sc := svc.ServiceContext{
|
||
|
UserModel: mockUserModel,
|
||
|
Redis: *newRedis,
|
||
|
Validate: mockValidate,
|
||
|
}
|
||
|
|
||
|
l := NewBindUserInfoLogic(context.Background(), &sc)
|
||
|
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
input *member.CreateUserInfoReq
|
||
|
setupMocks func()
|
||
|
expectError bool
|
||
|
expected *member.OKResp
|
||
|
}{
|
||
|
{
|
||
|
name: "ok",
|
||
|
input: &member.CreateUserInfoReq{
|
||
|
Uid: "2024101598765",
|
||
|
VerifyType: 1,
|
||
|
AlarmType: 2,
|
||
|
Status: 3,
|
||
|
Language: "en",
|
||
|
Currency: "USDT",
|
||
|
NickName: proto.String("Test User"),
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(nil)
|
||
|
mockUserModel.EXPECT().
|
||
|
Insert(gomock.Any(), gomock.Any()).
|
||
|
Return(nil, nil)
|
||
|
},
|
||
|
expectError: false,
|
||
|
expected: &member.OKResp{},
|
||
|
},
|
||
|
{
|
||
|
name: "invalid user info",
|
||
|
input: &member.CreateUserInfoReq{
|
||
|
Uid: "",
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(fmt.Errorf("validation error"))
|
||
|
},
|
||
|
expectError: true,
|
||
|
},
|
||
|
{
|
||
|
name: "duplicate entry",
|
||
|
input: &member.CreateUserInfoReq{
|
||
|
Uid: "test_uid",
|
||
|
VerifyType: 1,
|
||
|
AlarmType: 2,
|
||
|
Status: 3,
|
||
|
Language: "en",
|
||
|
Currency: "USD",
|
||
|
NickName: proto.String("Test User"),
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(nil)
|
||
|
mockUserModel.EXPECT().
|
||
|
Insert(gomock.Any(), gomock.Any()).
|
||
|
Return(nil, &mysql.MySQLError{Number: 1062, Message: "Duplicate entry"})
|
||
|
},
|
||
|
expectError: true,
|
||
|
},
|
||
|
{
|
||
|
name: "database error",
|
||
|
input: &member.CreateUserInfoReq{
|
||
|
Uid: "test_uid",
|
||
|
VerifyType: 1,
|
||
|
AlarmType: 2,
|
||
|
Status: 3,
|
||
|
Language: "en",
|
||
|
Currency: "USD",
|
||
|
NickName: proto.String("Test User"),
|
||
|
},
|
||
|
setupMocks: func() {
|
||
|
mockValidate.EXPECT().
|
||
|
ValidateAll(gomock.Any()).
|
||
|
Return(nil)
|
||
|
mockUserModel.EXPECT().
|
||
|
Insert(gomock.Any(), gomock.Any()).
|
||
|
Return(nil, fmt.Errorf("database error"))
|
||
|
},
|
||
|
expectError: true,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
tt.setupMocks()
|
||
|
resp, err := l.BindUserInfo(tt.input)
|
||
|
if tt.expectError {
|
||
|
assert.Error(t, err)
|
||
|
} else {
|
||
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, tt.expected, resp)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|