-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* email service 简单的 failvover 策略 * e2e * test * 增加zap日志 * 增加zap日志 * 增加zap日志 * 实现个人信息编辑接口 * 实现个人信息编辑接口 * 实现个人信息编辑接口 * 实现个人信息编辑接口-补测试 * 实现profile接口 * try --------- Co-authored-by: frankiejun <[email protected]>
- Loading branch information
1 parent
0b3593f
commit 978116c
Showing
12 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
}) | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package repository | |
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
@@ -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) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
}) | ||
} | ||
} |