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) } }) } }