Skip to content

Commit

Permalink
实现profile接口 (#24)
Browse files Browse the repository at this point in the history
* email service 简单的 failvover 策略

* e2e

* test

* 增加zap日志

* 增加zap日志

* 增加zap日志

* 实现个人信息编辑接口

* 实现个人信息编辑接口

* 实现个人信息编辑接口

* 实现个人信息编辑接口-补测试

* 实现profile接口

* try

---------

Co-authored-by: frankiejun <[email protected]>
  • Loading branch information
frankiejun and frankiejun authored Nov 2, 2023
1 parent 0b3593f commit 978116c
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/repository/dao/mocks/user.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/repository/dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type UserDAO interface {
UpdateEmailVerified(ctx context.Context, email string) error
UpdateUserProfile(ctx context.Context, u User) error
FindByEmail(ctx context.Context, email string) (User, error)
FindById(ctx context.Context, id int64) (User, error)
}

type User struct {
Expand Down Expand Up @@ -85,3 +86,9 @@ func (dao *GormUserDAO) UpdateUserProfile(ctx context.Context, u User) error {
"update_time": now}).Error

}

func (dao *GormUserDAO) FindById(ctx context.Context, id int64) (User, error) {
var u User
err := dao.db.WithContext(ctx).First(&u, "id = ?", id).Error
return u, err
}
44 changes: 44 additions & 0 deletions internal/repository/dao/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,47 @@ func TestGormUserDAO_UpdateUserProfile(t *testing.T) {
})
}
}

func TestGormUserDAO_FindById(t *testing.T) {
testCases := []struct {
name string
ctx context.Context
id int64
mock func(t *testing.T) *sql.DB
wantErr error
}{
{
name: "查找成功",
ctx: context.Background(),
id: 1,
mock: func(t *testing.T) *sql.DB {
mockDB, mock, err := sqlmock.New()
require.NoError(t, err)
rows := sqlmock.NewRows([]string{"id", "email", "about_me", "nick_name", "birthday"})
rows.AddRow(1, "[email protected]", "nice man", "abc", "2000-01-01")
mock.ExpectQuery("^SELECT \\* FROM `users` WHERE id = \\?").WillReturnRows(rows)
return mockDB
},
wantErr: nil,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
db, err := gorm.Open(gormMysql.New(gormMysql.Config{
Conn: tt.mock(t),
// 如果为 false ,则GORM在初始化时,会先调用 show version
SkipInitializeWithVersion: true,
}), &gorm.Config{
// 如果为 true ,则不允许 Ping数据库
DisableAutomaticPing: true,
// 如果为 false ,则即使是单一语句,也会开启事务
SkipDefaultTransaction: true,
})
require.NoError(t, err)
dao := NewUserInfoDAO(db)
_, err = dao.FindById(tt.ctx, tt.id)
assert.Equal(t, tt.wantErr, err)
})
}

}
15 changes: 15 additions & 0 deletions internal/repository/mocks/user.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type UserRepository interface {
UpdateEmailVerified(ctx context.Context, email string) error
FindByEmail(ctx context.Context, email string) (domain.User, error)
UpdateUserProfile(ctx context.Context, u domain.User) error
FindById(ctx context.Context, id int64) (domain.User, error)
}

type UserInfoRepository struct {
Expand All @@ -35,6 +36,9 @@ func (ur *UserInfoRepository) userToDomain(u dao.User) domain.User {
EmailVerified: u.EmailVerified,
Email: u.Email,
Password: u.Password,
AboutMe: u.AboutMe.String,
Birthday: u.Birthday.String,
NickName: u.NickName.String,
}
}

Expand Down Expand Up @@ -77,3 +81,11 @@ func (ur *UserInfoRepository) UpdateUserProfile(ctx context.Context, u domain.Us
},
})
}

func (ur *UserInfoRepository) FindById(ctx context.Context, id int64) (domain.User, error) {
user, err := ur.dao.FindById(ctx, id)
if err != nil {
return domain.User{}, err
}
return ur.userToDomain(user), err
}
48 changes: 48 additions & 0 deletions internal/repository/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"context"
"database/sql"
"errors"
"testing"
"time"
Expand Down Expand Up @@ -185,3 +186,50 @@ func TestUserInfoRepository_UpdateUserProfile(t *testing.T) {
})
}
}

func TestUserInfoRepository_FindById(t *testing.T) {
testCases := []struct {
name string
mock func(*gomock.Controller) dao.UserDAO
ctx context.Context
id int64
wantErr error
}{
{
name: "查找成功!",
ctx: context.Background(),
mock: func(ctrl *gomock.Controller) dao.UserDAO {
d := daomocks.NewMockUserDAO(ctrl)
d.EXPECT().FindById(gomock.Any(), gomock.Any()).Return(dao.User{
Id: 1,
Email: "[email protected]",
NickName: sql.NullString{
String: "frankiejun",
Valid: true,
},
Birthday: sql.NullString{
String: "1999-01-01",
Valid: true,
},
AboutMe: sql.NullString{
String: "I am a good boy",
Valid: true,
},
}, nil)
return d
},
id: 1,
wantErr: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

repo := NewUserInfoRepository(tc.mock(ctrl))
_, err := repo.FindById(tc.ctx, tc.id)
assert.Equal(t, tc.wantErr, err)
})
}
}
15 changes: 15 additions & 0 deletions internal/service/mocks/user.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type UserService interface {
SendVerifyEmail(ctx context.Context, email string) error
VerifyEmail(ctx context.Context, tokenStr string) error
EditUserProfile(ctx context.Context, u domain.User) error
Profile(ctx context.Context, id int64) (domain.User, error)
}

type userService struct {
Expand Down Expand Up @@ -84,3 +85,7 @@ func (svc *userService) VerifyEmail(ctx context.Context, tokenStr string) error
func (svc *userService) EditUserProfile(ctx context.Context, u domain.User) error {
return svc.r.UpdateUserProfile(ctx, u)
}

func (svc *userService) Profile(ctx context.Context, id int64) (domain.User, error) {
return svc.r.FindById(ctx, id)
}
37 changes: 37 additions & 0 deletions internal/service/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,40 @@ func Test_userService_EditUserProfile(t *testing.T) {
})
}
}

func Test_userService_Profile(t *testing.T) {
testCases := []struct {
name string
ctx context.Context
mock func(*gomock.Controller) repository.UserRepository
id int64
wantErr error
}{
{
name: "查找成功",
ctx: context.Background(),
mock: func(ctrl *gomock.Controller) repository.UserRepository {
mock := repomocks.NewMockUserRepository(ctrl)
mock.EXPECT().FindById(gomock.Any(), gomock.Any()).Return(domain.User{
Id: 1,
NickName: "frankiejun",
Birthday: "2020-01-01",
AboutMe: "I am a good boy",
}, nil)
return mock
},
id: 1,
wantErr: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

svc := NewUserService(tc.mock(ctrl), nil)
_, err := svc.Profile(tc.ctx, tc.id)
assert.Equal(t, tc.wantErr, err)
})
}
}
1 change: 1 addition & 0 deletions internal/web/init_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ func (u *UserHandler) RegisterRoutes(server *gin.Engine) {
server.POST("/users/email/verify/:token", u.EmailVerify)
server.POST("/users/login", u.Login)
server.POST("/users/edit", u.Edit)
server.POST("/users/profile", u.Profile)
}
23 changes: 23 additions & 0 deletions internal/web/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (u *UserHandler) Login(ctx *gin.Context) {
ctx.String(http.StatusBadRequest, "系统错误")
return
}
ctx.Set("userid", uid)

ctx.String(http.StatusOK, "登陆成功")
}
Expand Down Expand Up @@ -213,3 +214,25 @@ func (c *UserHandler) Edit(ctx *gin.Context) {
}
ctx.String(http.StatusOK, "更新成功")
}

func (c *UserHandler) Profile(ctx *gin.Context) {
type Profile struct {
Email string
NickName string
Birthday string
AboutMe string
}

id := ctx.MustGet("userid").(int64)
u, err := c.svc.Profile(ctx, id)
if err != nil {
ctx.String(http.StatusOK, "系统错误")
return
}
ctx.JSON(http.StatusOK, Profile{
Email: u.Email,
NickName: u.NickName,
Birthday: u.Birthday,
AboutMe: u.AboutMe,
})
}
64 changes: 64 additions & 0 deletions internal/web/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,67 @@ func TestUserHandler_Edit(t *testing.T) {
})
}
}

func TestUserHandler_Profile(t *testing.T) {
testCases := []struct {
name string
mock func(ctrl *gomock.Controller) service.UserService
body string
wantCode int
wantBody string
}{
{
name: "查看详细资料",
mock: func(ctrl *gomock.Controller) service.UserService {
userSvc := svcmocks.NewMockUserService(ctrl)
userSvc.EXPECT().Profile(gomock.Any(), gomock.Any()).Return(domain.User{
Email: "[email protected]",
NickName: "abc",
Birthday: "2020-01-01",
AboutMe: "i am a good boy",
}, nil)
return userSvc
},
wantCode: http.StatusOK,
wantBody: `{"Email":"[email protected]","NickName":"abc","Birthday":"2020-01-01","AboutMe":"i am a good boy"}`,
},
{
name: "查不到资料",
mock: func(ctrl *gomock.Controller) service.UserService {
userSvc := svcmocks.NewMockUserService(ctrl)
userSvc.EXPECT().Profile(gomock.Any(), gomock.Any()).Return(domain.User{}, errors.New("data not found"))
return userSvc
},
wantCode: http.StatusOK,
wantBody: "系统错误",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

r := gin.Default()
h := NewUserHandler(tc.mock(ctrl))

// 添加一个 路由
r.POST("/users/profile", func(ctx *gin.Context) {
ctx.Set("userid", int64(1)) // 模拟设置用户ID
h.Profile(ctx)
})

req, err := http.NewRequest(http.MethodPost, "/users/profile", nil)

require.NoError(t, err)

req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder()

r.ServeHTTP(resp, req)

//body, err := json.Marshal(resp.Body)
assert.Equal(t, tc.wantCode, resp.Code)
assert.Equal(t, tc.wantBody, resp.Body.String())
})
}
}

0 comments on commit 978116c

Please sign in to comment.