-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
567 additions
and
27 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"github.com/west2-online/fzuhelper-server/pkg/upyun" | ||
"testing" | ||
|
||
"github.com/bytedance/mockey" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetAllCloudSetting(t *testing.T) { | ||
type testCase struct { | ||
name string // 测试用例名称 | ||
mockSettingJson *[]byte // mock返回的设置 JSON 数据 | ||
mockError error // mock返回的错误 | ||
expectedResult *[]byte // 期望返回的结果 | ||
expectingError bool // 是否期望抛出错误 | ||
expectedErrorInfo string // 期望的错误信息 | ||
mockCommentedJson string // 模拟带注释的 JSON 数据 | ||
mockCommentedError error // 模拟去掉注释过程的错误 | ||
} | ||
|
||
mockResult := []byte(`{"key": "value"}`) | ||
// 测试用例 | ||
testCases := []testCase{ | ||
{ | ||
name: "SuccessCase", | ||
mockSettingJson: &mockResult, | ||
mockError: nil, | ||
expectedResult: &mockResult, | ||
expectingError: false, | ||
expectedErrorInfo: "", | ||
mockCommentedJson: `{"key": "value"}`, | ||
mockCommentedError: nil, | ||
}, | ||
{ | ||
name: "FileNotFound", | ||
mockSettingJson: nil, | ||
mockError: fmt.Errorf("file not found"), | ||
expectedResult: nil, | ||
expectingError: true, | ||
expectedErrorInfo: "VersionService.GetAllCloudSetting error:file not found", | ||
mockCommentedJson: "", | ||
mockCommentedError: nil, | ||
}, | ||
{ | ||
name: "RemoveCommentsError", | ||
mockSettingJson: &mockResult, | ||
mockError: nil, | ||
expectedResult: nil, | ||
expectingError: true, | ||
expectedErrorInfo: "VersionService.GetAllCloudSetting error:invalid JSON format", | ||
mockCommentedJson: "", | ||
mockCommentedError: fmt.Errorf("invalid JSON format"), | ||
}, | ||
} | ||
|
||
defer mockey.UnPatchAll() // 清理所有mock | ||
|
||
for _, tc := range testCases { | ||
mockey.PatchConvey(tc.name, t, func() { | ||
// Mock upyun.URlGetFile 方法 | ||
mockey.Mock(upyun.URlGetFile).To(func(filename string) (*[]byte, error) { | ||
return tc.mockSettingJson, tc.mockError | ||
}).Build() | ||
mockey.Mock(upyun.JoinFileName).To(func(filename string) string { | ||
return filename | ||
}).Build() | ||
|
||
// Mock getJSONWithoutComments 方法 | ||
mockey.Mock(getJSONWithoutComments).To(func(json string) (string, error) { | ||
if tc.mockCommentedError != nil { | ||
return "", tc.mockCommentedError | ||
} | ||
return tc.mockCommentedJson, nil | ||
}).Build() | ||
|
||
// 初始化UrlService实例 | ||
versionService := &VersionService{} | ||
|
||
// 调用方法 | ||
result, err := versionService.GetAllCloudSetting() | ||
|
||
if tc.expectingError { | ||
// 如果期望抛错,检查错误信息 | ||
assert.NotNil(t, err) | ||
assert.EqualError(t, err, tc.expectedErrorInfo) | ||
assert.Equal(t, tc.expectedResult, result) | ||
} else { | ||
// 如果不期望抛错,验证结果 | ||
assert.Nil(t, err) | ||
assert.Equal(t, tc.expectedResult, result) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package service | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bytedance/mockey" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/west2-online/fzuhelper-server/kitex_gen/version" | ||
"github.com/west2-online/fzuhelper-server/pkg/utils" | ||
) | ||
|
||
func TestLogin(t *testing.T) { | ||
type testCase struct { | ||
name string // 测试用例名称 | ||
mockCheckPwd bool // 模拟 CheckPwd 的返回值 | ||
request *version.LoginRequest // 输入的登录请求 | ||
expectedError bool // 是否期望抛出错误 | ||
expectedErrorMsg string // 期望的错误类型或信息 | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "ValidPassword", | ||
mockCheckPwd: true, | ||
request: &version.LoginRequest{ | ||
Password: "validpassword", | ||
}, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "InvalidPassword", | ||
mockCheckPwd: false, | ||
request: &version.LoginRequest{ | ||
Password: "invalidpassword", | ||
}, | ||
expectedError: true, | ||
expectedErrorMsg: "[401] authorization failed", | ||
}, | ||
} | ||
|
||
defer mockey.UnPatchAll() // 清理所有mock | ||
|
||
for _, tc := range testCases { | ||
mockey.PatchConvey(tc.name, t, func() { | ||
// Mock utils.CheckPwd 方法 | ||
mockey.Mock(utils.CheckPwd).To(func(password string) bool { | ||
return tc.mockCheckPwd | ||
}).Build() | ||
|
||
// 初始化 UrlService 实例 | ||
versionService := &VersionService{} | ||
|
||
// 调用方法 | ||
err := versionService.Login(tc.request) | ||
|
||
if tc.expectedError { | ||
// 如果期望抛错,检查错误信息 | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), tc.expectedErrorMsg) | ||
} else { | ||
// 如果不期望抛错,验证结果 | ||
assert.Nil(t, err) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/bytedance/mockey" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/west2-online/fzuhelper-server/kitex_gen/version" | ||
"github.com/west2-online/fzuhelper-server/pkg/upyun" | ||
"github.com/west2-online/fzuhelper-server/pkg/utils" | ||
) | ||
|
||
func TestSetSetting(t *testing.T) { | ||
type testCase struct { | ||
name string // 测试用例名称 | ||
mockCheckPwd bool // 模拟 CheckPwd 的返回值 | ||
mockUploadError error // 模拟 URlUploadFile 的错误 | ||
request *version.SetCloudRequest // 输入的请求 | ||
expectedError bool // 是否期望抛出错误 | ||
expectedErrorInfo string // 期望的错误信息 | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "ValidPasswordAndSuccessfulUpload", | ||
mockCheckPwd: true, | ||
mockUploadError: nil, | ||
request: &version.SetCloudRequest{ | ||
Password: "validpassword", | ||
Setting: "{\"key\": \"value\"}", | ||
}, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "InvalidPassword", | ||
mockCheckPwd: false, | ||
mockUploadError: nil, | ||
request: &version.SetCloudRequest{ | ||
Password: "invalidpassword", | ||
Setting: "{\"key\": \"value\"}", | ||
}, | ||
expectedError: true, | ||
expectedErrorInfo: "[401] authorization failed", // 假设 buildAuthFailedError 返回这个错误信息 | ||
}, | ||
{ | ||
name: "ValidPasswordButUploadFails", | ||
mockCheckPwd: true, | ||
mockUploadError: fmt.Errorf("upload failed"), | ||
request: &version.SetCloudRequest{ | ||
Password: "validpassword", | ||
Setting: "{\"key\": \"value\"}", | ||
}, | ||
expectedError: true, | ||
expectedErrorInfo: "upload failed", | ||
}, | ||
} | ||
|
||
defer mockey.UnPatchAll() // 清理所有mock | ||
|
||
for _, tc := range testCases { | ||
mockey.PatchConvey(tc.name, t, func() { | ||
// Mock utils.CheckPwd 方法 | ||
mockey.Mock(utils.CheckPwd).To(func(password string) bool { | ||
return tc.mockCheckPwd | ||
}).Build() | ||
|
||
// Mock upyun.URlUploadFile 方法 | ||
mockey.Mock(upyun.URlUploadFile).To(func(data []byte, filename string) error { | ||
return tc.mockUploadError | ||
}).Build() | ||
mockey.Mock(upyun.JoinFileName).To(func(filename string) string { | ||
return filename | ||
}).Build() | ||
|
||
// 初始化 UrlService 实例 | ||
versionService := &VersionService{} | ||
|
||
// 调用方法 | ||
err := versionService.SetSetting(tc.request) | ||
|
||
if tc.expectedError { | ||
// 如果期望抛错,检查错误信息 | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), tc.expectedErrorInfo) | ||
} else { | ||
// 如果不期望抛错,验证结果 | ||
assert.Nil(t, err) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.