feat/member #1

Merged
daniel.w merged 5 commits from feat/member into main 2024-08-02 02:20:17 +00:00
34 changed files with 1816 additions and 104 deletions
Showing only changes of commit 4b1a8268a5 - Show all commits

4
go.mod
View File

@ -3,6 +3,7 @@ module member
go 1.22.3 go 1.22.3
require ( require (
github.com/alicebob/miniredis/v2 v2.33.0
github.com/bwmarrin/snowflake v0.3.0 github.com/bwmarrin/snowflake v0.3.0
github.com/go-playground/validator/v10 v10.22.0 github.com/go-playground/validator/v10 v10.22.0
github.com/go-sql-driver/mysql v1.8.1 github.com/go-sql-driver/mysql v1.8.1
@ -10,6 +11,7 @@ require (
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
github.com/zeromicro/go-zero v1.6.6 github.com/zeromicro/go-zero v1.6.6
go.uber.org/goleak v1.2.1 go.uber.org/goleak v1.2.1
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.24.0 golang.org/x/crypto v0.24.0
google.golang.org/grpc v1.65.0 google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2 google.golang.org/protobuf v1.34.2
@ -17,6 +19,7 @@ require (
require ( require (
filippo.io/edwards25519 v1.1.0 // indirect filippo.io/edwards25519 v1.1.0 // indirect
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
@ -60,6 +63,7 @@ require (
github.com/prometheus/procfs v0.12.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect
github.com/redis/go-redis/v9 v9.5.3 // indirect github.com/redis/go-redis/v9 v9.5.3 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/v3 v3.5.14 // indirect go.etcd.io/etcd/client/v3 v3.5.14 // indirect

View File

@ -399,7 +399,7 @@ func TestXBCInternal_WithStrings_ShouldHasCatResource_DetailCodeXBCInternal(t *t
}() }()
// act // act
e := XBCInternal("field A", "Error description") e := ArkInternal("field A", "Error description")
// assert // assert
assert.Equal(t, code.CatArk, e.Category()) assert.Equal(t, code.CatArk, e.Category())
@ -817,7 +817,10 @@ func TestStructErr_WithInternalErr_ShouldIsFuncReportCorrectly(t *testing.T) {
// act with error chain: InvalidFormat -> layer 2 err -> layer 1 err // act with error chain: InvalidFormat -> layer 2 err -> layer 1 err
e := InvalidFormat("field A", "Error description") e := InvalidFormat("field A", "Error description")
e.Wrap(layer2Err) err := e.Wrap(layer2Err)
if err != nil {
t.Fatalf("Failed to wrap error: %v", err)
}
// assert // assert
assert.Equal(t, code.CatInput, e.Category()) assert.Equal(t, code.CatInput, e.Category())
@ -840,7 +843,10 @@ func TestStructErr_WithInternalErr_ShouldErrorOutputChainErrMessage(t *testing.T
layer1Err := fmt.Errorf("layer 1 error") layer1Err := fmt.Errorf("layer 1 error")
// act with error chain: InvalidFormat -> layer 1 err // act with error chain: InvalidFormat -> layer 1 err
e := InvalidFormat("field A", "Error description") e := InvalidFormat("field A", "Error description")
e.Wrap(layer1Err) err := e.Wrap(layer1Err)
if err != nil {
t.Fatalf("Failed to wrap error: %v", err)
}
// assert // assert
assert.Equal(t, "invalid format: field A Error description: layer 1 error", e.Error()) assert.Equal(t, "invalid format: field A Error description: layer 1 error", e.Error())
@ -865,7 +871,10 @@ func TestStructErr_WithInternalErr_ShouldAsFuncReportCorrectly(t *testing.T) {
// act with error chain: InvalidFormat -> layer 2 err -> testErr // act with error chain: InvalidFormat -> layer 2 err -> testErr
e := InvalidFormat("field A", "Error description") e := InvalidFormat("field A", "Error description")
e.Wrap(layer2Err) err := e.Wrap(layer2Err)
if err != nil {
t.Fatalf("Failed to wrap error: %v", err)
}
// assert // assert
assert.Equal(t, code.CatInput, e.Category()) assert.Equal(t, code.CatInput, e.Category())
@ -906,7 +915,10 @@ func Benchmark_ErrorsIs_TwoLayerError(b *testing.B) {
// act with error chain: InvalidFormat(layer 2) -> testErr(layer 1) // act with error chain: InvalidFormat(layer 2) -> testErr(layer 1)
layer2Err := InvalidFormat("field A", "Error description") layer2Err := InvalidFormat("field A", "Error description")
layer2Err.Wrap(layer1Err) err := layer2Err.Wrap(layer1Err)
if err != nil {
b.Fatalf("Failed to wrap error: %v", err)
}
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
@ -921,7 +933,10 @@ func Benchmark_ErrorsIs_FourLayerError(b *testing.B) {
layer3Err := fmt.Errorf("layer 3: %w", layer2Err) layer3Err := fmt.Errorf("layer 3: %w", layer2Err)
// act with error chain: InvalidFormat(layer 4) -> Error(layer 3) -> Error(layer 2) -> testErr(layer 1) // act with error chain: InvalidFormat(layer 4) -> Error(layer 3) -> Error(layer 2) -> testErr(layer 1)
layer4Err := InvalidFormat("field A", "Error description") layer4Err := InvalidFormat("field A", "Error description")
layer4Err.Wrap(layer3Err) err := layer4Err.Wrap(layer3Err)
if err != nil {
b.Fatalf("Failed to wrap error: %v", err)
}
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
@ -947,7 +962,10 @@ func Benchmark_ErrorsAs_TwoLayerError(b *testing.B) {
// act with error chain: InvalidFormat(layer 2) -> testErr(layer 1) // act with error chain: InvalidFormat(layer 2) -> testErr(layer 1)
layer2Err := InvalidFormat("field A", "Error description") layer2Err := InvalidFormat("field A", "Error description")
layer2Err.Wrap(layer1Err) err := layer2Err.Wrap(layer1Err)
if err != nil {
b.Fatalf("Failed to wrap error: %v", err)
}
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
@ -963,7 +981,10 @@ func Benchmark_ErrorsAs_FourLayerError(b *testing.B) {
layer3Err := fmt.Errorf("layer 3: %w", layer2Err) layer3Err := fmt.Errorf("layer 3: %w", layer2Err)
// act with error chain: InvalidFormat(layer 4) -> Error(layer 3) -> Error(layer 2) -> testErr(layer 1) // act with error chain: InvalidFormat(layer 4) -> Error(layer 3) -> Error(layer 2) -> testErr(layer 1)
layer4Err := InvalidFormat("field A", "Error description") layer4Err := InvalidFormat("field A", "Error description")
layer4Err.Wrap(layer3Err) err := layer4Err.Wrap(layer3Err)
if err != nil {
b.Fatalf("Failed to wrap error: %v", err)
}
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()

View File

@ -2,6 +2,9 @@ package required
import ( import (
"fmt" "fmt"
"github.com/zeromicro/go-zero/core/logx"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
) )
@ -40,9 +43,8 @@ func MustValidator(option ...Option) Validate {
V: validator.New(), V: validator.New(),
} }
err := v.BindToValidator(option...) if err := v.BindToValidator(option...); err != nil {
if err != nil { logx.Error("failed to bind validator")
// log
} }
return v return v

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"member/internal/domain" "member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/internal/model" "member/internal/model"
"strconv" "strconv"
@ -37,10 +36,9 @@ type bindLoginUserReq struct {
// BindAccount 綁定帳號 -> account bind to UID // BindAccount 綁定帳號 -> account bind to UID
func (l *BindAccountLogic) BindAccount(in *member.BindingUserReq) (*member.Response, error) { func (l *BindAccountLogic) BindAccount(in *member.BindingUserReq) (*member.Response, error) {
// 驗證資料 // 驗證資料
err := required.ValidateAll(l.svcCtx.Validate, &bindLoginUserReq{ if err := l.svcCtx.Validate.ValidateAll(&bindLoginUserReq{
Account: in.GetLoginId(), Account: in.GetLoginId(),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }
@ -50,16 +48,14 @@ func (l *BindAccountLogic) BindAccount(in *member.BindingUserReq) (*member.Respo
uid = strconv.FormatInt(int64(l.svcCtx.SnackFlowGen.Generate()), 10) uid = strconv.FormatInt(int64(l.svcCtx.SnackFlowGen.Generate()), 10)
} }
// 先確定有這個Account // 先確定有這個Account
_, err = l.svcCtx.AccountModel.FindOneByAccount(l.ctx, in.GetLoginId()) if _, err := l.svcCtx.AccountModel.FindOneByAccount(l.ctx, in.GetLoginId()); err != nil {
if err != nil {
return nil, ers.ResourceNotFound(fmt.Sprintf("failed to get account : %s ", in.GetLoginId())) return nil, ers.ResourceNotFound(fmt.Sprintf("failed to get account : %s ", in.GetLoginId()))
} }
_, err = l.svcCtx.AccountToUidModel.Insert(l.ctx, &model.AccountToUid{ if _, err := l.svcCtx.AccountToUidModel.Insert(l.ctx, &model.AccountToUid{
Account: in.LoginId, Account: in.LoginId,
Uid: uid, Uid: uid,
}) }); err != nil {
if err != nil {
return nil, ers.DBError(err.Error()) return nil, ers.DBError(err.Error())
} }

View File

@ -0,0 +1,179 @@
package logic
import (
"context"
"errors"
"fmt"
"member/gen_result/pb/member"
"member/internal/domain"
libMock "member/internal/mock/lib"
modelMock "member/internal/mock/model"
svcMock "member/internal/mock/svc"
"member/internal/model"
"member/internal/svc"
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/bwmarrin/snowflake"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/stores/redis"
"go.uber.org/mock/gomock"
)
func TestBindAccountLogic_BindAccount(t *testing.T) {
// mock
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockAccountModel := modelMock.NewMockAccountModel(ctrl)
mockAccountToUidModel := modelMock.NewMockAccountToUidModel(ctrl)
mockSnackFlow := svcMock.NewMockSnackFlow(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{
AccountModel: mockAccountModel,
AccountToUidModel: mockAccountToUidModel,
SnackFlowGen: mockSnackFlow,
Redis: *newRedis,
Validate: mockValidate,
}
l := NewBindAccountLogic(context.Background(), &sc)
tests := []struct {
name string
input *member.BindingUserReq
setupMocks func()
expectError bool
expected *member.Response
}{
{
name: "ok",
input: &member.BindingUserReq{
LoginId: "cat1@30cm.net",
Uid: "12345",
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(nil)
mockAccountModel.EXPECT().
FindOneByAccount(gomock.Any(), "cat1@30cm.net").
Return(&model.Account{Account: "cat1@30cm.net"}, nil)
mockAccountToUidModel.EXPECT().
Insert(gomock.Any(), gomock.Any()).
Return(nil, nil)
},
expectError: false,
expected: &member.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
},
},
{
name: "valid account without UID",
input: &member.BindingUserReq{
LoginId: "cat1@30cm.net",
Uid: "",
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(nil)
mockAccountModel.EXPECT().
FindOneByAccount(gomock.Any(), "cat1@30cm.net").
Return(&model.Account{Account: "cat1@30cm.net"}, nil)
mockSnackFlow.EXPECT().
Generate().
Return(snowflake.ID(202408013345678))
mockAccountToUidModel.EXPECT().
Insert(gomock.Any(), &model.AccountToUid{Account: "cat1@30cm.net", Uid: "202408013345678"}).
Return(nil, nil)
},
expectError: false,
expected: &member.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
},
},
{
name: "account not found",
input: &member.BindingUserReq{
LoginId: "cat2@30cm.net",
Uid: "12345",
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(nil)
mockAccountModel.EXPECT().
FindOneByAccount(gomock.Any(), "cat2@30cm.net").
Return(nil, errors.New("account not found"))
},
expectError: true,
},
{
name: "insert account to UID failed",
input: &member.BindingUserReq{
LoginId: "cat2@30cm.net",
Uid: "202408013345679",
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(nil)
mockAccountModel.EXPECT().
FindOneByAccount(gomock.Any(), "cat2@30cm.net").
Return(&model.Account{Account: "cat2@30cm.net"}, nil)
mockAccountToUidModel.EXPECT().
Insert(gomock.Any(), &model.AccountToUid{Account: "cat2@30cm.net", Uid: "202408013345679"}).
Return(nil, errors.New("insert failed"))
},
expectError: true,
},
{
name: "failed to validate param",
input: &member.BindingUserReq{
LoginId: "cat3",
Uid: "202408013345679",
},
setupMocks: func() {
// mock validate returns
mockValidate.EXPECT().
ValidateAll(gomock.Any()).Return(fmt.Errorf("invalid format: failed to get correct formate loginID "))
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupMocks()
resp, err := l.BindAccount(tt.input)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, resp)
}
})
}
}

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"member/internal/domain" "member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/internal/model" "member/internal/model"
"time" "time"
@ -47,7 +46,7 @@ type createUserInfo struct {
// BindUserInfo 初次,綁定 User Info // BindUserInfo 初次,綁定 User Info
func (l *BindUserInfoLogic) BindUserInfo(in *member.CreateUserInfoReq) (*member.Response, error) { func (l *BindUserInfoLogic) BindUserInfo(in *member.CreateUserInfoReq) (*member.Response, error) {
// 驗證資料 // 驗證資料
err := required.ValidateAll(l.svcCtx.Validate, &createUserInfo{ if err := l.svcCtx.Validate.ValidateAll(&createUserInfo{
Uid: in.GetUid(), Uid: in.GetUid(),
VerifyType: int32(in.GetVerifyType()), VerifyType: int32(in.GetVerifyType()),
AlarmType: int32(in.GetAlarmType()), AlarmType: int32(in.GetAlarmType()),
@ -58,13 +57,12 @@ func (l *BindUserInfoLogic) BindUserInfo(in *member.CreateUserInfoReq) (*member.
NickName: in.GetNickName(), NickName: in.GetNickName(),
Gender: int8(in.GetGender()), Gender: int8(in.GetGender()),
Birthday: in.GetBirthday(), Birthday: in.GetBirthday(),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }
now := time.Now().UTC().Unix() now := time.Now().UTC().Unix()
_, err = l.svcCtx.UserModel.Insert(l.ctx, &model.UserTable{ if _, err := l.svcCtx.UserModel.Insert(l.ctx, &model.UserTable{
Uid: in.GetUid(), Uid: in.GetUid(),
VerifyType: int64(in.GetVerifyType()), VerifyType: int64(in.GetVerifyType()),
AlarmType: int64(in.GetAlarmType()), AlarmType: int64(in.GetAlarmType()),
@ -77,8 +75,7 @@ func (l *BindUserInfoLogic) BindUserInfo(in *member.CreateUserInfoReq) (*member.
Birthday: in.GetBirthday(), Birthday: in.GetBirthday(),
CreateTime: now, CreateTime: now,
UpdateTime: now, UpdateTime: now,
}) }); err != nil {
if err != nil {
// 新增進去 // 新增進去
var mysqlErr *mysql.MySQLError var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 {

View File

@ -0,0 +1,164 @@
package logic
import (
"context"
"fmt"
"member/gen_result/pb/member"
"member/internal/domain"
libMock "member/internal/mock/lib"
modelMock "member/internal/mock/model"
svcMock "member/internal/mock/svc"
"member/internal/svc"
"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)
mockSnackFlow := svcMock.NewMockSnackFlow(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,
SnackFlowGen: mockSnackFlow,
Redis: *newRedis,
Validate: mockValidate,
}
l := NewBindUserInfoLogic(context.Background(), &sc)
tests := []struct {
name string
input *member.CreateUserInfoReq
setupMocks func()
expectError bool
expected *member.Response
}{
{
name: "ok",
input: &member.CreateUserInfoReq{
Uid: "2024101598765",
VerifyType: 1,
AlarmType: 2,
Status: 3,
RoleId: "cat_01",
Language: "en",
Currency: "USDT",
NickName: proto.String("Test User"),
Gender: proto.Uint32(1),
Birthday: proto.Int64(1722327098),
},
setupMocks: func() {
mockValidate.EXPECT().
ValidateAll(gomock.Any()).
Return(nil)
mockUserModel.EXPECT().
Insert(gomock.Any(), gomock.Any()).
Return(nil, nil)
},
expectError: false,
expected: &member.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
},
},
{
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,
RoleId: "test_role",
Language: "en",
Currency: "USD",
NickName: proto.String("Test User"),
Gender: proto.Uint32(1),
Birthday: proto.Int64(1622517800),
},
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,
RoleId: "test_role",
Language: "en",
Currency: "USD",
NickName: proto.String("Test User"),
Gender: proto.Uint32(1),
Birthday: proto.Int64(1622517800),
},
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)
}
})
}
}

View File

@ -5,8 +5,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"member/gen_result/pb/member" "member/gen_result/pb/member"
"member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/internal/model" "member/internal/model"
"member/internal/svc" "member/internal/svc"
"member/internal/utils" "member/internal/utils"
@ -36,19 +36,21 @@ type createLoginUserReq struct {
Token string `json:"token" validate:"required"` Token string `json:"token" validate:"required"`
} }
// HasPasswordFunc 這樣方便測試
var HasPasswordFunc = utils.HashPassword
// CreateUserAccount 建立帳號與密碼 -> 可登入,但可不可以做其他事情看業務流程,也可以只註冊就好 // CreateUserAccount 建立帳號與密碼 -> 可登入,但可不可以做其他事情看業務流程,也可以只註冊就好
func (l *CreateUserAccountLogic) CreateUserAccount(in *member.CreateLoginUserReq) (*member.Response, error) { func (l *CreateUserAccountLogic) CreateUserAccount(in *member.CreateLoginUserReq) (*member.Response, error) {
// 驗證資料 // 驗證資料
err := required.ValidateAll(l.svcCtx.Validate, &createLoginUserReq{ if err := l.svcCtx.Validate.ValidateAll(&createLoginUserReq{
LoginId: in.GetLoginId(), LoginId: in.GetLoginId(),
Platform: in.GetPlatform(), Platform: in.GetPlatform(),
Token: in.GetToken(), Token: in.GetToken(),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }
token, err := utils.HashPassword(in.GetToken(), l.svcCtx.Config.Bcrypt.Cost) token, err := HasPasswordFunc(in.GetToken(), l.svcCtx.Config.Bcrypt.Cost)
if err != nil { if err != nil {
return nil, ers.ArkInternal(fmt.Sprintf("failed to encrypt err: %v", err.Error())) return nil, ers.ArkInternal(fmt.Sprintf("failed to encrypt err: %v", err.Error()))
} }
@ -73,5 +75,11 @@ func (l *CreateUserAccountLogic) CreateUserAccount(in *member.CreateLoginUserReq
return nil, ers.DBError(err.Error()) return nil, ers.DBError(err.Error())
} }
return nil, nil return &member.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
}, nil
} }

View File

@ -0,0 +1,166 @@
package logic
import (
"context"
"fmt"
"member/gen_result/pb/member"
"member/internal/domain"
libMock "member/internal/mock/lib"
modelMock "member/internal/mock/model"
svcMock "member/internal/mock/svc"
"member/internal/svc"
"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"
)
func TestCreateUserAccountLogic_CreateUserAccount(t *testing.T) {
// mock
ctrl := gomock.NewController(t)
defer ctrl.Finish()
modelAccountModel := modelMock.NewMockAccountModel(ctrl)
mockSnackFlow := svcMock.NewMockSnackFlow(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{
AccountModel: modelAccountModel,
SnackFlowGen: mockSnackFlow,
Redis: *newRedis,
Validate: mockValidate,
}
l := NewCreateUserAccountLogic(context.Background(), &sc)
tests := []struct {
name string
input *member.CreateLoginUserReq
setupMocks func()
expectError bool
expected *member.Response
}{
{
name: "ok",
input: &member.CreateLoginUserReq{
LoginId: "test_user",
Platform: 1,
Token: "password123",
},
setupMocks: func() {
mockValidate.EXPECT().
ValidateAll(gomock.Any()).
Return(nil)
modelAccountModel.EXPECT().
Insert(gomock.Any(), gomock.Any()).
Return(nil, nil)
},
expectError: false,
expected: &member.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
},
},
{
name: "validation failure",
input: &member.CreateLoginUserReq{
LoginId: "test_user",
Platform: 1,
Token: "password123",
},
setupMocks: func() {
mockValidate.EXPECT().
ValidateAll(gomock.Any()).
Return(fmt.Errorf("validation error"))
},
expectError: true,
},
{
name: "password hash failure",
input: &member.CreateLoginUserReq{
LoginId: "test_user",
Platform: 1,
Token: "password123",
},
setupMocks: func() {
mockValidate.EXPECT().
ValidateAll(gomock.Any()).
Return(nil)
HasPasswordFunc = func(password string, cost int) (string, error) {
return "", fmt.Errorf("hash error")
}
},
expectError: true,
},
{
name: "duplicate entry error",
input: &member.CreateLoginUserReq{
LoginId: "test_user",
Platform: 1,
Token: "password123",
},
setupMocks: func() {
mockValidate.EXPECT().
ValidateAll(gomock.Any()).
Return(nil)
HasPasswordFunc = func(password string, cost int) (string, error) {
return "hashedpassword", nil
}
modelAccountModel.EXPECT().
Insert(gomock.Any(), gomock.Any()).
Return(nil, &mysql.MySQLError{Number: 1062, Message: "Duplicate entry"})
},
expectError: true,
},
{
name: "database error",
input: &member.CreateLoginUserReq{
LoginId: "test_user",
Platform: 1,
Token: "password123",
},
setupMocks: func() {
mockValidate.EXPECT().
ValidateAll(gomock.Any()).
Return(nil)
HasPasswordFunc = func(password string, cost int) (string, error) {
return "hashedpassword", nil
}
modelAccountModel.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.CreateUserAccount(tt.input)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, resp)
}
})
}
}

View File

@ -8,7 +8,6 @@ import (
"member/gen_result/pb/member" "member/gen_result/pb/member"
"member/internal/domain" "member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/internal/svc" "member/internal/svc"
"strconv" "strconv"
@ -75,11 +74,11 @@ func generateVerifyCode(digits int) (string, error) {
// GenerateRefreshCode 這個帳號驗證碼(十分鐘),通用的 // GenerateRefreshCode 這個帳號驗證碼(十分鐘),通用的
func (l *GenerateRefreshCodeLogic) GenerateRefreshCode(in *member.GenerateRefreshCodeReq) (*member.GenerateRefreshCodeResp, error) { func (l *GenerateRefreshCodeLogic) GenerateRefreshCode(in *member.GenerateRefreshCodeReq) (*member.GenerateRefreshCodeResp, error) {
err := required.ValidateAll(l.svcCtx.Validate, &generateRefreshCodeReq{ // 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&generateRefreshCodeReq{
Account: in.GetAccount(), Account: in.GetAccount(),
CodeType: in.GetCodeType(), CodeType: in.GetCodeType(),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }

View File

@ -0,0 +1,51 @@
package logic
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_getCodeNameByCode(t *testing.T) {
type args struct {
code int32
}
tests := []struct {
name string
args args
want string
want1 bool
}{
{
name: "email",
args: args{
code: 1,
},
want: "email",
want1: true,
},
{
name: "phone",
args: args{
code: 2,
},
want: "phone",
want1: true,
},
{
name: "none",
args: args{
code: 3,
},
want: "",
want1: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := getCodeNameByCode(tt.args.code)
assert.Equalf(t, tt.want, got, "getCodeNameByCode(%v)", tt.args.code)
assert.Equalf(t, tt.want1, got1, "getCodeNameByCode(%v)", tt.args.code)
})
}
}

View File

@ -2,11 +2,9 @@ package logic
import ( import (
"context" "context"
"member/gen_result/pb/member"
"member/internal/domain" "member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/gen_result/pb/member"
"member/internal/svc" "member/internal/svc"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
@ -32,10 +30,10 @@ type getUidByAccountReq struct {
// GetUidByAccount 用帳號換取 UID // GetUidByAccount 用帳號換取 UID
func (l *GetUidByAccountLogic) GetUidByAccount(in *member.GetUIDByAccountReq) (*member.GetUidByAccountResp, error) { func (l *GetUidByAccountLogic) GetUidByAccount(in *member.GetUIDByAccountReq) (*member.GetUidByAccountResp, error) {
err := required.ValidateAll(l.svcCtx.Validate, &getUidByAccountReq{ // 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&getUidByAccountReq{
Account: in.GetAccount(), Account: in.GetAccount(),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }

View File

@ -5,7 +5,6 @@ import (
"member/gen_result/pb/member" "member/gen_result/pb/member"
"member/internal/domain" "member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/internal/svc" "member/internal/svc"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
@ -31,11 +30,10 @@ type getUserAccountReq struct {
// GetUserAccountInfo 取得帳號密碼資料 // GetUserAccountInfo 取得帳號密碼資料
func (l *GetUserAccountInfoLogic) GetUserAccountInfo(in *member.GetUIDByAccountReq) (*member.GetAccountInfoResp, error) { func (l *GetUserAccountInfoLogic) GetUserAccountInfo(in *member.GetUIDByAccountReq) (*member.GetAccountInfoResp, error) {
// 驗證輸入資料 // 驗證資料
err := required.ValidateAll(l.svcCtx.Validate, &getUserAccountReq{ if err := l.svcCtx.Validate.ValidateAll(getUserAccountReq{
LoginId: in.GetAccount(), LoginId: in.GetAccount(),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }

View File

@ -24,10 +24,6 @@ func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUs
} }
} }
type getUserInfoReq struct {
Uid string `json:"account" validate:"account"`
}
// GetUserInfo 取得會員資訊 // GetUserInfo 取得會員資訊
func (l *GetUserInfoLogic) GetUserInfo(in *member.GetUserInfoReq) (*member.GetUserInfoResp, error) { func (l *GetUserInfoLogic) GetUserInfo(in *member.GetUserInfoReq) (*member.GetUserInfoResp, error) {
var entity *model.UserTable var entity *model.UserTable

View File

@ -2,11 +2,9 @@ package logic
import ( import (
"context" "context"
"member/gen_result/pb/member"
"member/internal/domain" "member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/gen_result/pb/member"
"member/internal/svc" "member/internal/svc"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
@ -33,15 +31,15 @@ type updateStatusReq struct {
// UpdateStatus 修改狀態 // UpdateStatus 修改狀態
func (l *UpdateStatusLogic) UpdateStatus(in *member.UpdateStatusReq) (*member.Response, error) { func (l *UpdateStatusLogic) UpdateStatus(in *member.UpdateStatusReq) (*member.Response, error) {
err := required.ValidateAll(l.svcCtx.Validate, &updateStatusReq{ // 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&updateStatusReq{
UID: in.GetUid(), UID: in.GetUid(),
Status: int32(in.GetStatus()), Status: int32(in.GetStatus()),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }
err = l.svcCtx.UserModel.UpdateStatus(l.ctx, in.GetUid(), int32(in.GetStatus())) err := l.svcCtx.UserModel.UpdateStatus(l.ctx, in.GetUid(), int32(in.GetStatus()))
if err != nil { if err != nil {
return nil, ers.DBError(err.Error()) return nil, ers.DBError(err.Error())
} }

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"member/internal/domain" "member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/internal/utils" "member/internal/utils"
"member/gen_result/pb/member" "member/gen_result/pb/member"
@ -35,11 +34,11 @@ type updateUserTokenReq struct {
// UpdateUserToken 更新密碼 // UpdateUserToken 更新密碼
func (l *UpdateUserTokenLogic) UpdateUserToken(in *member.UpdateTokenReq) (*member.Response, error) { func (l *UpdateUserTokenLogic) UpdateUserToken(in *member.UpdateTokenReq) (*member.Response, error) {
err := required.ValidateAll(l.svcCtx.Validate, &updateUserTokenReq{ // 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&updateUserTokenReq{
LoginId: in.GetAccount(), LoginId: in.GetAccount(),
Token: in.GetToken(), Token: in.GetToken(),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }

View File

@ -3,11 +3,9 @@ package logic
import ( import (
"context" "context"
"fmt" "fmt"
"member/gen_result/pb/member"
"member/internal/domain" "member/internal/domain"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"member/internal/lib/required"
"member/gen_result/pb/member"
"member/internal/svc" "member/internal/svc"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
@ -29,11 +27,11 @@ func NewVerifyRefreshCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext)
// VerifyRefreshCode 驗證忘記密碼 token // VerifyRefreshCode 驗證忘記密碼 token
func (l *VerifyRefreshCodeLogic) VerifyRefreshCode(in *member.VerifyRefreshCodeReq) (*member.Response, error) { func (l *VerifyRefreshCodeLogic) VerifyRefreshCode(in *member.VerifyRefreshCodeReq) (*member.Response, error) {
err := required.ValidateAll(l.svcCtx.Validate, &generateRefreshCodeReq{ // 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&generateRefreshCodeReq{
Account: in.GetAccount(), Account: in.GetAccount(),
CodeType: in.GetCodeType(), CodeType: in.GetCodeType(),
}) }); err != nil {
if err != nil {
return nil, ers.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }

View File

@ -0,0 +1,72 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./validate.go
//
// Generated by this command:
//
// mockgen -source=./validate.go -destination=../../mock/lib/validate.go -package=lib
//
// Package lib is a generated GoMock package.
package lib
import (
required "member/internal/lib/required"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockValidate is a mock of Validate interface.
type MockValidate struct {
ctrl *gomock.Controller
recorder *MockValidateMockRecorder
}
// MockValidateMockRecorder is the mock recorder for MockValidate.
type MockValidateMockRecorder struct {
mock *MockValidate
}
// NewMockValidate creates a new mock instance.
func NewMockValidate(ctrl *gomock.Controller) *MockValidate {
mock := &MockValidate{ctrl: ctrl}
mock.recorder = &MockValidateMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockValidate) EXPECT() *MockValidateMockRecorder {
return m.recorder
}
// BindToValidator mocks base method.
func (m *MockValidate) BindToValidator(opts ...required.Option) error {
m.ctrl.T.Helper()
varargs := []any{}
for _, a := range opts {
varargs = append(varargs, a)
}
ret := m.ctrl.Call(m, "BindToValidator", varargs...)
ret0, _ := ret[0].(error)
return ret0
}
// BindToValidator indicates an expected call of BindToValidator.
func (mr *MockValidateMockRecorder) BindToValidator(opts ...any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BindToValidator", reflect.TypeOf((*MockValidate)(nil).BindToValidator), opts...)
}
// ValidateAll mocks base method.
func (m *MockValidate) ValidateAll(obj any) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ValidateAll", obj)
ret0, _ := ret[0].(error)
return ret0
}
// ValidateAll indicates an expected call of ValidateAll.
func (mr *MockValidateMockRecorder) ValidateAll(obj any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateAll", reflect.TypeOf((*MockValidate)(nil).ValidateAll), obj)
}

View File

@ -0,0 +1,129 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./account_model.go
//
// Generated by this command:
//
// mockgen -source=./account_model.go -destination=../mock/model/account_model.go -package=model
//
// Package model is a generated GoMock package.
package model
import (
context "context"
sql "database/sql"
model "member/internal/model"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockAccountModel is a mock of AccountModel interface.
type MockAccountModel struct {
ctrl *gomock.Controller
recorder *MockAccountModelMockRecorder
}
// MockAccountModelMockRecorder is the mock recorder for MockAccountModel.
type MockAccountModelMockRecorder struct {
mock *MockAccountModel
}
// NewMockAccountModel creates a new mock instance.
func NewMockAccountModel(ctrl *gomock.Controller) *MockAccountModel {
mock := &MockAccountModel{ctrl: ctrl}
mock.recorder = &MockAccountModelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockAccountModel) EXPECT() *MockAccountModelMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockAccountModel) Delete(ctx context.Context, id int64) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", ctx, id)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockAccountModelMockRecorder) Delete(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockAccountModel)(nil).Delete), ctx, id)
}
// FindOne mocks base method.
func (m *MockAccountModel) FindOne(ctx context.Context, id int64) (*model.Account, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOne", ctx, id)
ret0, _ := ret[0].(*model.Account)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOne indicates an expected call of FindOne.
func (mr *MockAccountModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockAccountModel)(nil).FindOne), ctx, id)
}
// FindOneByAccount mocks base method.
func (m *MockAccountModel) FindOneByAccount(ctx context.Context, account string) (*model.Account, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOneByAccount", ctx, account)
ret0, _ := ret[0].(*model.Account)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOneByAccount indicates an expected call of FindOneByAccount.
func (mr *MockAccountModelMockRecorder) FindOneByAccount(ctx, account any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneByAccount", reflect.TypeOf((*MockAccountModel)(nil).FindOneByAccount), ctx, account)
}
// Insert mocks base method.
func (m *MockAccountModel) Insert(ctx context.Context, data *model.Account) (sql.Result, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Insert", ctx, data)
ret0, _ := ret[0].(sql.Result)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Insert indicates an expected call of Insert.
func (mr *MockAccountModelMockRecorder) Insert(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockAccountModel)(nil).Insert), ctx, data)
}
// Update mocks base method.
func (m *MockAccountModel) Update(ctx context.Context, data *model.Account) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Update", ctx, data)
ret0, _ := ret[0].(error)
return ret0
}
// Update indicates an expected call of Update.
func (mr *MockAccountModelMockRecorder) Update(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockAccountModel)(nil).Update), ctx, data)
}
// UpdateTokenByLoginID mocks base method.
func (m *MockAccountModel) UpdateTokenByLoginID(ctx context.Context, account, token string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpdateTokenByLoginID", ctx, account, token)
ret0, _ := ret[0].(error)
return ret0
}
// UpdateTokenByLoginID indicates an expected call of UpdateTokenByLoginID.
func (mr *MockAccountModelMockRecorder) UpdateTokenByLoginID(ctx, account, token any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateTokenByLoginID", reflect.TypeOf((*MockAccountModel)(nil).UpdateTokenByLoginID), ctx, account, token)
}

View File

@ -0,0 +1,115 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./account_model_gen.go
//
// Generated by this command:
//
// mockgen -source=./account_model_gen.go -destination=../mock/model/account_model_gen.go -package=model
//
// Package model is a generated GoMock package.
package model
import (
context "context"
sql "database/sql"
model "member/internal/model"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockaccountModel is a mock of accountModel interface.
type MockaccountModel struct {
ctrl *gomock.Controller
recorder *MockaccountModelMockRecorder
}
// MockaccountModelMockRecorder is the mock recorder for MockaccountModel.
type MockaccountModelMockRecorder struct {
mock *MockaccountModel
}
// NewMockaccountModel creates a new mock instance.
func NewMockaccountModel(ctrl *gomock.Controller) *MockaccountModel {
mock := &MockaccountModel{ctrl: ctrl}
mock.recorder = &MockaccountModelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockaccountModel) EXPECT() *MockaccountModelMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockaccountModel) Delete(ctx context.Context, id int64) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", ctx, id)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockaccountModelMockRecorder) Delete(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockaccountModel)(nil).Delete), ctx, id)
}
// FindOne mocks base method.
func (m *MockaccountModel) FindOne(ctx context.Context, id int64) (*model.Account, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOne", ctx, id)
ret0, _ := ret[0].(*model.Account)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOne indicates an expected call of FindOne.
func (mr *MockaccountModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockaccountModel)(nil).FindOne), ctx, id)
}
// FindOneByAccount mocks base method.
func (m *MockaccountModel) FindOneByAccount(ctx context.Context, account string) (*model.Account, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOneByAccount", ctx, account)
ret0, _ := ret[0].(*model.Account)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOneByAccount indicates an expected call of FindOneByAccount.
func (mr *MockaccountModelMockRecorder) FindOneByAccount(ctx, account any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneByAccount", reflect.TypeOf((*MockaccountModel)(nil).FindOneByAccount), ctx, account)
}
// Insert mocks base method.
func (m *MockaccountModel) Insert(ctx context.Context, data *model.Account) (sql.Result, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Insert", ctx, data)
ret0, _ := ret[0].(sql.Result)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Insert indicates an expected call of Insert.
func (mr *MockaccountModelMockRecorder) Insert(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockaccountModel)(nil).Insert), ctx, data)
}
// Update mocks base method.
func (m *MockaccountModel) Update(ctx context.Context, data *model.Account) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Update", ctx, data)
ret0, _ := ret[0].(error)
return ret0
}
// Update indicates an expected call of Update.
func (mr *MockaccountModelMockRecorder) Update(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockaccountModel)(nil).Update), ctx, data)
}

View File

@ -0,0 +1,115 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./account_to_uid_model.go
//
// Generated by this command:
//
// mockgen -source=./account_to_uid_model.go -destination=../mock/model/account_to_uid_model.go -package=model
//
// Package model is a generated GoMock package.
package model
import (
context "context"
sql "database/sql"
model "member/internal/model"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockAccountToUidModel is a mock of AccountToUidModel interface.
type MockAccountToUidModel struct {
ctrl *gomock.Controller
recorder *MockAccountToUidModelMockRecorder
}
// MockAccountToUidModelMockRecorder is the mock recorder for MockAccountToUidModel.
type MockAccountToUidModelMockRecorder struct {
mock *MockAccountToUidModel
}
// NewMockAccountToUidModel creates a new mock instance.
func NewMockAccountToUidModel(ctrl *gomock.Controller) *MockAccountToUidModel {
mock := &MockAccountToUidModel{ctrl: ctrl}
mock.recorder = &MockAccountToUidModelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockAccountToUidModel) EXPECT() *MockAccountToUidModelMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockAccountToUidModel) Delete(ctx context.Context, id int64) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", ctx, id)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockAccountToUidModelMockRecorder) Delete(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockAccountToUidModel)(nil).Delete), ctx, id)
}
// FindOne mocks base method.
func (m *MockAccountToUidModel) FindOne(ctx context.Context, id int64) (*model.AccountToUid, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOne", ctx, id)
ret0, _ := ret[0].(*model.AccountToUid)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOne indicates an expected call of FindOne.
func (mr *MockAccountToUidModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockAccountToUidModel)(nil).FindOne), ctx, id)
}
// FindOneByAccount mocks base method.
func (m *MockAccountToUidModel) FindOneByAccount(ctx context.Context, account string) (*model.AccountToUid, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOneByAccount", ctx, account)
ret0, _ := ret[0].(*model.AccountToUid)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOneByAccount indicates an expected call of FindOneByAccount.
func (mr *MockAccountToUidModelMockRecorder) FindOneByAccount(ctx, account any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneByAccount", reflect.TypeOf((*MockAccountToUidModel)(nil).FindOneByAccount), ctx, account)
}
// Insert mocks base method.
func (m *MockAccountToUidModel) Insert(ctx context.Context, data *model.AccountToUid) (sql.Result, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Insert", ctx, data)
ret0, _ := ret[0].(sql.Result)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Insert indicates an expected call of Insert.
func (mr *MockAccountToUidModelMockRecorder) Insert(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockAccountToUidModel)(nil).Insert), ctx, data)
}
// Update mocks base method.
func (m *MockAccountToUidModel) Update(ctx context.Context, data *model.AccountToUid) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Update", ctx, data)
ret0, _ := ret[0].(error)
return ret0
}
// Update indicates an expected call of Update.
func (mr *MockAccountToUidModelMockRecorder) Update(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockAccountToUidModel)(nil).Update), ctx, data)
}

View File

@ -0,0 +1,115 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./account_to_uid_model_gen.go
//
// Generated by this command:
//
// mockgen -source=./account_to_uid_model_gen.go -destination=../mock/model/account_to_uid_model_gen.go -package=model
//
// Package model is a generated GoMock package.
package model
import (
context "context"
sql "database/sql"
model "member/internal/model"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockaccountToUidModel is a mock of accountToUidModel interface.
type MockaccountToUidModel struct {
ctrl *gomock.Controller
recorder *MockaccountToUidModelMockRecorder
}
// MockaccountToUidModelMockRecorder is the mock recorder for MockaccountToUidModel.
type MockaccountToUidModelMockRecorder struct {
mock *MockaccountToUidModel
}
// NewMockaccountToUidModel creates a new mock instance.
func NewMockaccountToUidModel(ctrl *gomock.Controller) *MockaccountToUidModel {
mock := &MockaccountToUidModel{ctrl: ctrl}
mock.recorder = &MockaccountToUidModelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockaccountToUidModel) EXPECT() *MockaccountToUidModelMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockaccountToUidModel) Delete(ctx context.Context, id int64) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", ctx, id)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockaccountToUidModelMockRecorder) Delete(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockaccountToUidModel)(nil).Delete), ctx, id)
}
// FindOne mocks base method.
func (m *MockaccountToUidModel) FindOne(ctx context.Context, id int64) (*model.AccountToUid, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOne", ctx, id)
ret0, _ := ret[0].(*model.AccountToUid)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOne indicates an expected call of FindOne.
func (mr *MockaccountToUidModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockaccountToUidModel)(nil).FindOne), ctx, id)
}
// FindOneByAccount mocks base method.
func (m *MockaccountToUidModel) FindOneByAccount(ctx context.Context, account string) (*model.AccountToUid, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOneByAccount", ctx, account)
ret0, _ := ret[0].(*model.AccountToUid)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOneByAccount indicates an expected call of FindOneByAccount.
func (mr *MockaccountToUidModelMockRecorder) FindOneByAccount(ctx, account any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneByAccount", reflect.TypeOf((*MockaccountToUidModel)(nil).FindOneByAccount), ctx, account)
}
// Insert mocks base method.
func (m *MockaccountToUidModel) Insert(ctx context.Context, data *model.AccountToUid) (sql.Result, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Insert", ctx, data)
ret0, _ := ret[0].(sql.Result)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Insert indicates an expected call of Insert.
func (mr *MockaccountToUidModelMockRecorder) Insert(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockaccountToUidModel)(nil).Insert), ctx, data)
}
// Update mocks base method.
func (m *MockaccountToUidModel) Update(ctx context.Context, data *model.AccountToUid) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Update", ctx, data)
ret0, _ := ret[0].(error)
return ret0
}
// Update indicates an expected call of Update.
func (mr *MockaccountToUidModelMockRecorder) Update(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockaccountToUidModel)(nil).Update), ctx, data)
}

View File

@ -0,0 +1,115 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./machine_node_model.go
//
// Generated by this command:
//
// mockgen -source=./machine_node_model.go -destination=../mock/model/machine_node_model.go -package=model
//
// Package model is a generated GoMock package.
package model
import (
context "context"
sql "database/sql"
model "member/internal/model"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockMachineNodeModel is a mock of MachineNodeModel interface.
type MockMachineNodeModel struct {
ctrl *gomock.Controller
recorder *MockMachineNodeModelMockRecorder
}
// MockMachineNodeModelMockRecorder is the mock recorder for MockMachineNodeModel.
type MockMachineNodeModelMockRecorder struct {
mock *MockMachineNodeModel
}
// NewMockMachineNodeModel creates a new mock instance.
func NewMockMachineNodeModel(ctrl *gomock.Controller) *MockMachineNodeModel {
mock := &MockMachineNodeModel{ctrl: ctrl}
mock.recorder = &MockMachineNodeModelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockMachineNodeModel) EXPECT() *MockMachineNodeModelMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockMachineNodeModel) Delete(ctx context.Context, id int64) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", ctx, id)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockMachineNodeModelMockRecorder) Delete(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockMachineNodeModel)(nil).Delete), ctx, id)
}
// FindOne mocks base method.
func (m *MockMachineNodeModel) FindOne(ctx context.Context, id int64) (*model.MachineNode, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOne", ctx, id)
ret0, _ := ret[0].(*model.MachineNode)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOne indicates an expected call of FindOne.
func (mr *MockMachineNodeModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockMachineNodeModel)(nil).FindOne), ctx, id)
}
// FindOneByHostName mocks base method.
func (m *MockMachineNodeModel) FindOneByHostName(ctx context.Context, hostName string) (*model.MachineNode, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOneByHostName", ctx, hostName)
ret0, _ := ret[0].(*model.MachineNode)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOneByHostName indicates an expected call of FindOneByHostName.
func (mr *MockMachineNodeModelMockRecorder) FindOneByHostName(ctx, hostName any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneByHostName", reflect.TypeOf((*MockMachineNodeModel)(nil).FindOneByHostName), ctx, hostName)
}
// Insert mocks base method.
func (m *MockMachineNodeModel) Insert(ctx context.Context, data *model.MachineNode) (sql.Result, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Insert", ctx, data)
ret0, _ := ret[0].(sql.Result)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Insert indicates an expected call of Insert.
func (mr *MockMachineNodeModelMockRecorder) Insert(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockMachineNodeModel)(nil).Insert), ctx, data)
}
// Update mocks base method.
func (m *MockMachineNodeModel) Update(ctx context.Context, data *model.MachineNode) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Update", ctx, data)
ret0, _ := ret[0].(error)
return ret0
}
// Update indicates an expected call of Update.
func (mr *MockMachineNodeModelMockRecorder) Update(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockMachineNodeModel)(nil).Update), ctx, data)
}

View File

@ -0,0 +1,100 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./machine_node_model_gen.go
//
// Generated by this command:
//
// mockgen -source=./machine_node_model_gen.go -destination=../mock/model/machine_node_model_gen.go -package=model
//
// Package model is a generated GoMock package.
package model
import (
context "context"
sql "database/sql"
model "member/internal/model"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockmachineNodeModel is a mock of machineNodeModel interface.
type MockmachineNodeModel struct {
ctrl *gomock.Controller
recorder *MockmachineNodeModelMockRecorder
}
// MockmachineNodeModelMockRecorder is the mock recorder for MockmachineNodeModel.
type MockmachineNodeModelMockRecorder struct {
mock *MockmachineNodeModel
}
// NewMockmachineNodeModel creates a new mock instance.
func NewMockmachineNodeModel(ctrl *gomock.Controller) *MockmachineNodeModel {
mock := &MockmachineNodeModel{ctrl: ctrl}
mock.recorder = &MockmachineNodeModelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockmachineNodeModel) EXPECT() *MockmachineNodeModelMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockmachineNodeModel) Delete(ctx context.Context, id int64) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", ctx, id)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockmachineNodeModelMockRecorder) Delete(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockmachineNodeModel)(nil).Delete), ctx, id)
}
// FindOne mocks base method.
func (m *MockmachineNodeModel) FindOne(ctx context.Context, id int64) (*model.MachineNode, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOne", ctx, id)
ret0, _ := ret[0].(*model.MachineNode)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOne indicates an expected call of FindOne.
func (mr *MockmachineNodeModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockmachineNodeModel)(nil).FindOne), ctx, id)
}
// Insert mocks base method.
func (m *MockmachineNodeModel) Insert(ctx context.Context, data *model.MachineNode) (sql.Result, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Insert", ctx, data)
ret0, _ := ret[0].(sql.Result)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Insert indicates an expected call of Insert.
func (mr *MockmachineNodeModelMockRecorder) Insert(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockmachineNodeModel)(nil).Insert), ctx, data)
}
// Update mocks base method.
func (m *MockmachineNodeModel) Update(ctx context.Context, data *model.MachineNode) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Update", ctx, data)
ret0, _ := ret[0].(error)
return ret0
}
// Update indicates an expected call of Update.
func (mr *MockmachineNodeModelMockRecorder) Update(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockmachineNodeModel)(nil).Update), ctx, data)
}

View File

@ -0,0 +1,189 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./user_table_model.go
//
// Generated by this command:
//
// mockgen -source=./user_table_model.go -destination=../mock/model/user_table_model.go -package=model
//
// Package model is a generated GoMock package.
package model
import (
context "context"
sql "database/sql"
member "member/gen_result/pb/member"
model "member/internal/model"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockUserTableModel is a mock of UserTableModel interface.
type MockUserTableModel struct {
ctrl *gomock.Controller
recorder *MockUserTableModelMockRecorder
}
// MockUserTableModelMockRecorder is the mock recorder for MockUserTableModel.
type MockUserTableModelMockRecorder struct {
mock *MockUserTableModel
}
// NewMockUserTableModel creates a new mock instance.
func NewMockUserTableModel(ctrl *gomock.Controller) *MockUserTableModel {
mock := &MockUserTableModel{ctrl: ctrl}
mock.recorder = &MockUserTableModelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockUserTableModel) EXPECT() *MockUserTableModelMockRecorder {
return m.recorder
}
// Count mocks base method.
func (m *MockUserTableModel) Count(ctx context.Context) (int64, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Count", ctx)
ret0, _ := ret[0].(int64)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Count indicates an expected call of Count.
func (mr *MockUserTableModelMockRecorder) Count(ctx any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Count", reflect.TypeOf((*MockUserTableModel)(nil).Count), ctx)
}
// Delete mocks base method.
func (m *MockUserTableModel) Delete(ctx context.Context, id int64) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", ctx, id)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockUserTableModelMockRecorder) Delete(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockUserTableModel)(nil).Delete), ctx, id)
}
// FindOne mocks base method.
func (m *MockUserTableModel) FindOne(ctx context.Context, id int64) (*model.UserTable, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOne", ctx, id)
ret0, _ := ret[0].(*model.UserTable)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOne indicates an expected call of FindOne.
func (mr *MockUserTableModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockUserTableModel)(nil).FindOne), ctx, id)
}
// FindOneByNickName mocks base method.
func (m *MockUserTableModel) FindOneByNickName(ctx context.Context, uid string) (*model.UserTable, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOneByNickName", ctx, uid)
ret0, _ := ret[0].(*model.UserTable)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOneByNickName indicates an expected call of FindOneByNickName.
func (mr *MockUserTableModelMockRecorder) FindOneByNickName(ctx, uid any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneByNickName", reflect.TypeOf((*MockUserTableModel)(nil).FindOneByNickName), ctx, uid)
}
// FindOneByUid mocks base method.
func (m *MockUserTableModel) FindOneByUid(ctx context.Context, uid string) (*model.UserTable, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOneByUid", ctx, uid)
ret0, _ := ret[0].(*model.UserTable)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOneByUid indicates an expected call of FindOneByUid.
func (mr *MockUserTableModelMockRecorder) FindOneByUid(ctx, uid any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneByUid", reflect.TypeOf((*MockUserTableModel)(nil).FindOneByUid), ctx, uid)
}
// Insert mocks base method.
func (m *MockUserTableModel) Insert(ctx context.Context, data *model.UserTable) (sql.Result, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Insert", ctx, data)
ret0, _ := ret[0].(sql.Result)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Insert indicates an expected call of Insert.
func (mr *MockUserTableModelMockRecorder) Insert(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockUserTableModel)(nil).Insert), ctx, data)
}
// ListMembers mocks base method.
func (m *MockUserTableModel) ListMembers(ctx context.Context, params *model.UserQueryParams) ([]*model.UserTable, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ListMembers", ctx, params)
ret0, _ := ret[0].([]*model.UserTable)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// ListMembers indicates an expected call of ListMembers.
func (mr *MockUserTableModelMockRecorder) ListMembers(ctx, params any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListMembers", reflect.TypeOf((*MockUserTableModel)(nil).ListMembers), ctx, params)
}
// Update mocks base method.
func (m *MockUserTableModel) Update(ctx context.Context, data *model.UserTable) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Update", ctx, data)
ret0, _ := ret[0].(error)
return ret0
}
// Update indicates an expected call of Update.
func (mr *MockUserTableModelMockRecorder) Update(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockUserTableModel)(nil).Update), ctx, data)
}
// UpdateSome mocks base method.
func (m *MockUserTableModel) UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpdateSome", ctx, newData)
ret0, _ := ret[0].(error)
return ret0
}
// UpdateSome indicates an expected call of UpdateSome.
func (mr *MockUserTableModelMockRecorder) UpdateSome(ctx, newData any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateSome", reflect.TypeOf((*MockUserTableModel)(nil).UpdateSome), ctx, newData)
}
// UpdateStatus mocks base method.
func (m *MockUserTableModel) UpdateStatus(ctx context.Context, uid string, status int32) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "UpdateStatus", ctx, uid, status)
ret0, _ := ret[0].(error)
return ret0
}
// UpdateStatus indicates an expected call of UpdateStatus.
func (mr *MockUserTableModelMockRecorder) UpdateStatus(ctx, uid, status any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateStatus", reflect.TypeOf((*MockUserTableModel)(nil).UpdateStatus), ctx, uid, status)
}

View File

@ -0,0 +1,115 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./user_table_model_gen.go
//
// Generated by this command:
//
// mockgen -source=./user_table_model_gen.go -destination=../mock/model/user_table_model_gen.go -package=model
//
// Package model is a generated GoMock package.
package model
import (
context "context"
sql "database/sql"
model "member/internal/model"
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockuserTableModel is a mock of userTableModel interface.
type MockuserTableModel struct {
ctrl *gomock.Controller
recorder *MockuserTableModelMockRecorder
}
// MockuserTableModelMockRecorder is the mock recorder for MockuserTableModel.
type MockuserTableModelMockRecorder struct {
mock *MockuserTableModel
}
// NewMockuserTableModel creates a new mock instance.
func NewMockuserTableModel(ctrl *gomock.Controller) *MockuserTableModel {
mock := &MockuserTableModel{ctrl: ctrl}
mock.recorder = &MockuserTableModelMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockuserTableModel) EXPECT() *MockuserTableModelMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockuserTableModel) Delete(ctx context.Context, id int64) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", ctx, id)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockuserTableModelMockRecorder) Delete(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockuserTableModel)(nil).Delete), ctx, id)
}
// FindOne mocks base method.
func (m *MockuserTableModel) FindOne(ctx context.Context, id int64) (*model.UserTable, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOne", ctx, id)
ret0, _ := ret[0].(*model.UserTable)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOne indicates an expected call of FindOne.
func (mr *MockuserTableModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockuserTableModel)(nil).FindOne), ctx, id)
}
// FindOneByUid mocks base method.
func (m *MockuserTableModel) FindOneByUid(ctx context.Context, uid string) (*model.UserTable, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "FindOneByUid", ctx, uid)
ret0, _ := ret[0].(*model.UserTable)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// FindOneByUid indicates an expected call of FindOneByUid.
func (mr *MockuserTableModelMockRecorder) FindOneByUid(ctx, uid any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneByUid", reflect.TypeOf((*MockuserTableModel)(nil).FindOneByUid), ctx, uid)
}
// Insert mocks base method.
func (m *MockuserTableModel) Insert(ctx context.Context, data *model.UserTable) (sql.Result, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Insert", ctx, data)
ret0, _ := ret[0].(sql.Result)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Insert indicates an expected call of Insert.
func (mr *MockuserTableModelMockRecorder) Insert(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockuserTableModel)(nil).Insert), ctx, data)
}
// Update mocks base method.
func (m *MockuserTableModel) Update(ctx context.Context, data *model.UserTable) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Update", ctx, data)
ret0, _ := ret[0].(error)
return ret0
}
// Update indicates an expected call of Update.
func (mr *MockuserTableModelMockRecorder) Update(ctx, data any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockuserTableModel)(nil).Update), ctx, data)
}

View File

@ -0,0 +1,68 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: ./machine_node.go
//
// Generated by this command:
//
// mockgen -source=./machine_node.go -destination=../mock/svc/machine_node.go -package=svc
//
// Package svc is a generated GoMock package.
package svc
import (
reflect "reflect"
snowflake "github.com/bwmarrin/snowflake"
gomock "go.uber.org/mock/gomock"
)
// MockSnackFlow is a mock of SnackFlow interface.
type MockSnackFlow struct {
ctrl *gomock.Controller
recorder *MockSnackFlowMockRecorder
}
// MockSnackFlowMockRecorder is the mock recorder for MockSnackFlow.
type MockSnackFlowMockRecorder struct {
mock *MockSnackFlow
}
// NewMockSnackFlow creates a new mock instance.
func NewMockSnackFlow(ctrl *gomock.Controller) *MockSnackFlow {
mock := &MockSnackFlow{ctrl: ctrl}
mock.recorder = &MockSnackFlowMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockSnackFlow) EXPECT() *MockSnackFlowMockRecorder {
return m.recorder
}
// Generate mocks base method.
func (m *MockSnackFlow) Generate() snowflake.ID {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Generate")
ret0, _ := ret[0].(snowflake.ID)
return ret0
}
// Generate indicates an expected call of Generate.
func (mr *MockSnackFlowMockRecorder) Generate() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Generate", reflect.TypeOf((*MockSnackFlow)(nil).Generate))
}
// GetSnackFlowNode mocks base method.
func (m *MockSnackFlow) GetSnackFlowNode() *snowflake.Node {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetSnackFlowNode")
ret0, _ := ret[0].(*snowflake.Node)
return ret0
}
// GetSnackFlowNode indicates an expected call of GetSnackFlowNode.
func (mr *MockSnackFlowMockRecorder) GetSnackFlowNode() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSnackFlowNode", reflect.TypeOf((*MockSnackFlow)(nil).GetSnackFlowNode))
}

View File

@ -8,6 +8,7 @@ import (
var _ AccountToUidModel = (*customAccountToUidModel)(nil) var _ AccountToUidModel = (*customAccountToUidModel)(nil)
var ( var (
// nolint:unused
cacheAccountPrefix = "cache:accountToUid:account:" cacheAccountPrefix = "cache:accountToUid:account:"
) )

View File

@ -2,6 +2,7 @@ package model
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
@ -38,10 +39,10 @@ func (m *defaultMachineNodeModel) FindOneByHostName(ctx context.Context, hostNam
query := fmt.Sprintf("select %s from %s where `host_name` = ? limit 1", machineNodeRows, m.table) query := fmt.Sprintf("select %s from %s where `host_name` = ? limit 1", machineNodeRows, m.table)
return conn.QueryRowCtx(ctx, v, query, hostName) return conn.QueryRowCtx(ctx, v, query, hostName)
}) })
switch err { switch {
case nil: case err == nil:
return &resp, nil return &resp, nil
case sqlc.ErrNotFound: case errors.Is(err, sqlc.ErrNotFound):
return nil, ErrNotFound return nil, ErrNotFound
default: default:
return nil, err return nil, err

View File

@ -4,12 +4,13 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"member/gen_result/pb/member" "member/gen_result/pb/member"
"strings" "strings"
"time" "time"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
) )
var _ UserTableModel = (*customUserTableModel)(nil) var _ UserTableModel = (*customUserTableModel)(nil)

View File

@ -26,6 +26,7 @@ var (
) )
type ( type (
// go:generate mockgen -source=./user_table_model_gen.go -destination=../../mock/model/user_table_model_gen.go -package=model
userTableModel interface { userTableModel interface {
Insert(ctx context.Context, data *UserTable) (sql.Result, error) Insert(ctx context.Context, data *UserTable) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*UserTable, error) FindOne(ctx context.Context, id int64) (*UserTable, error)

View File

@ -2,7 +2,6 @@ package svc
import ( import (
"context" "context"
sf "member/internal/lib/snackflow"
"member/internal/model" "member/internal/model"
"os" "os"
"time" "time"
@ -10,18 +9,29 @@ import (
"github.com/bwmarrin/snowflake" "github.com/bwmarrin/snowflake"
) )
type machineNode struct { type SnackFlow interface {
MachineNodeID int64 `json:"machine_node_id"` GetSnackFlowNode() *snowflake.Node
Generate() snowflake.ID
} }
type MachineNodeCreateParams struct { type SnackFlowNode struct {
HostName string *snowflake.Node
} }
func NewMachineNode(node model.MachineNodeModel) int64 { func NewSnackFlow(node model.MachineNodeModel) (SnackFlow, error) {
nodeID := newMachineNodeID(node)
ringNodeID := getMachineNodeID(nodeID)
s, err := snowflake.NewNode(ringNodeID)
if err != nil {
return nil, err
}
return &SnackFlowNode{Node: s}, nil
}
func newMachineNodeID(node model.MachineNodeModel) int64 {
ctx := context.Background() ctx := context.Background()
nodeName := os.Getenv("POD_NAME") nodeName := os.Getenv("POD_NAME")
if os.Getenv("POD_NAME") == "" { if nodeName == "" {
nodeName = "default_node" nodeName = "default_node"
} }
@ -38,14 +48,13 @@ func NewMachineNode(node model.MachineNodeModel) int64 {
if err != nil { if err != nil {
return 1 return 1
} }
return id return id
} }
return machine.Id return machine.Id
} }
func GetMachineNodeID(machineNodeID int64) int64 { func getMachineNodeID(machineNodeID int64) int64 {
// Snowflake 公式,工作機器 ID 佔用 10bit最多容納 1024 節點, // Snowflake 公式,工作機器 ID 佔用 10bit最多容納 1024 節點,
// 故用 % 1024 取餘數做 ring // 故用 % 1024 取餘數做 ring
const nodeMax = 1024 const nodeMax = 1024
@ -53,15 +62,10 @@ func GetMachineNodeID(machineNodeID int64) int64 {
return machineNodeID % nodeMax return machineNodeID % nodeMax
} }
func newSnackFlowNode(node model.MachineNodeModel) (*snowflake.Node, error) { func (n *SnackFlowNode) GetSnackFlowNode() *snowflake.Node {
return n.Node
nodeID := NewMachineNode(node)
ringNodeID := GetMachineNodeID(nodeID)
s := sf.New(sf.WithMachineNodeID(ringNodeID))
n, err := s.NewNode()
if err != nil {
return nil, err
} }
return n, nil func (n *SnackFlowNode) Generate() snowflake.ID {
return n.Node.Generate()
} }

View File

@ -8,11 +8,8 @@ import (
"github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/core/stores/redis"
"github.com/bwmarrin/snowflake"
ers "member/internal/lib/error" ers "member/internal/lib/error"
"github.com/go-playground/validator/v10"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
) )
@ -20,11 +17,11 @@ type ServiceContext struct {
Config config.Config Config config.Config
Redis redis.Redis Redis redis.Redis
Validate *validator.Validate Validate required.Validate
AccountModel model.AccountModel AccountModel model.AccountModel
UserModel model.UserTableModel UserModel model.UserTableModel
AccountToUidModel model.AccountToUidModel AccountToUidModel model.AccountToUidModel
SnackFlowGen *snowflake.Node SnackFlowGen SnackFlow
} }
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
@ -32,7 +29,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
// 設置 // 設置
ers.Scope = domain.Scope ers.Scope = domain.Scope
n, err := newSnackFlowNode(model.NewMachineNodeModel(sqlConn, c.Cache)) n, err := NewSnackFlow(model.NewMachineNodeModel(sqlConn, c.Cache))
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -35,7 +35,7 @@ func main() {
}) })
defer s.Stop() defer s.Stop()
// 加入中間件 // 加入中間件
s.AddUnaryInterceptors(middleware.TimeoutMiddleware) s.AddUnaryInterceptors(middleware.TimeoutMiddleware)
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)