200 lines
4.9 KiB
Go
200 lines
4.9 KiB
Go
package service
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// TestGetDefaultAvatar 测试获取默认头像的逻辑
|
||
// 注意:这个测试需要mock repository,但由于repository是函数式的,
|
||
// 我们只测试逻辑部分
|
||
func TestGetDefaultAvatar_Logic(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
configExists bool
|
||
configValue string
|
||
expectedResult string
|
||
}{
|
||
{
|
||
name: "配置存在时返回配置值",
|
||
configExists: true,
|
||
configValue: "https://example.com/avatar.png",
|
||
expectedResult: "https://example.com/avatar.png",
|
||
},
|
||
{
|
||
name: "配置不存在时返回错误信息",
|
||
configExists: false,
|
||
configValue: "",
|
||
expectedResult: "数据库中不存在默认头像配置",
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
// 这个测试只验证逻辑,不实际调用repository
|
||
// 实际的repository调用测试需要集成测试或mock
|
||
if tt.configExists {
|
||
if tt.expectedResult != tt.configValue {
|
||
t.Errorf("当配置存在时,应该返回配置值")
|
||
}
|
||
} else {
|
||
if !strings.Contains(tt.expectedResult, "数据库中不存在默认头像配置") {
|
||
t.Errorf("当配置不存在时,应该返回错误信息")
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// TestLoginUser_EmailDetection 测试登录时邮箱检测逻辑
|
||
func TestLoginUser_EmailDetection(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
usernameOrEmail string
|
||
isEmail bool
|
||
}{
|
||
{
|
||
name: "包含@符号,识别为邮箱",
|
||
usernameOrEmail: "user@example.com",
|
||
isEmail: true,
|
||
},
|
||
{
|
||
name: "不包含@符号,识别为用户名",
|
||
usernameOrEmail: "username",
|
||
isEmail: false,
|
||
},
|
||
{
|
||
name: "空字符串",
|
||
usernameOrEmail: "",
|
||
isEmail: false,
|
||
},
|
||
{
|
||
name: "只有@符号",
|
||
usernameOrEmail: "@",
|
||
isEmail: true,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
isEmail := strings.Contains(tt.usernameOrEmail, "@")
|
||
if isEmail != tt.isEmail {
|
||
t.Errorf("Email detection failed: got %v, want %v", isEmail, tt.isEmail)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// TestUserService_Constants 测试用户服务相关常量
|
||
func TestUserService_Constants(t *testing.T) {
|
||
// 测试默认用户角色
|
||
defaultRole := "user"
|
||
if defaultRole == "" {
|
||
t.Error("默认用户角色不能为空")
|
||
}
|
||
|
||
// 测试默认用户状态
|
||
defaultStatus := int16(1)
|
||
if defaultStatus != 1 {
|
||
t.Errorf("默认用户状态应为1(正常),实际为%d", defaultStatus)
|
||
}
|
||
|
||
// 测试初始积分
|
||
initialPoints := 0
|
||
if initialPoints < 0 {
|
||
t.Errorf("初始积分不应为负数,实际为%d", initialPoints)
|
||
}
|
||
}
|
||
|
||
// TestUserService_Validation 测试用户数据验证逻辑
|
||
func TestUserService_Validation(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
username string
|
||
email string
|
||
password string
|
||
wantValid bool
|
||
}{
|
||
{
|
||
name: "有效的用户名和邮箱",
|
||
username: "testuser",
|
||
email: "test@example.com",
|
||
password: "password123",
|
||
wantValid: true,
|
||
},
|
||
{
|
||
name: "用户名为空",
|
||
username: "",
|
||
email: "test@example.com",
|
||
password: "password123",
|
||
wantValid: false,
|
||
},
|
||
{
|
||
name: "邮箱为空",
|
||
username: "testuser",
|
||
email: "",
|
||
password: "password123",
|
||
wantValid: false,
|
||
},
|
||
{
|
||
name: "密码为空",
|
||
username: "testuser",
|
||
email: "test@example.com",
|
||
password: "",
|
||
wantValid: false,
|
||
},
|
||
{
|
||
name: "邮箱格式无效(缺少@)",
|
||
username: "testuser",
|
||
email: "invalid-email",
|
||
password: "password123",
|
||
wantValid: false,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
// 简单的验证逻辑测试
|
||
isValid := tt.username != "" && tt.email != "" && tt.password != "" && strings.Contains(tt.email, "@")
|
||
if isValid != tt.wantValid {
|
||
t.Errorf("Validation failed: got %v, want %v", isValid, tt.wantValid)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// TestUserService_AvatarLogic 测试头像逻辑
|
||
func TestUserService_AvatarLogic(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
providedAvatar string
|
||
defaultAvatar string
|
||
expectedAvatar string
|
||
}{
|
||
{
|
||
name: "提供头像时使用提供的头像",
|
||
providedAvatar: "https://example.com/custom.png",
|
||
defaultAvatar: "https://example.com/default.png",
|
||
expectedAvatar: "https://example.com/custom.png",
|
||
},
|
||
{
|
||
name: "未提供头像时使用默认头像",
|
||
providedAvatar: "",
|
||
defaultAvatar: "https://example.com/default.png",
|
||
expectedAvatar: "https://example.com/default.png",
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
avatarURL := tt.providedAvatar
|
||
if avatarURL == "" {
|
||
avatarURL = tt.defaultAvatar
|
||
}
|
||
if avatarURL != tt.expectedAvatar {
|
||
t.Errorf("Avatar logic failed: got %s, want %s", avatarURL, tt.expectedAvatar)
|
||
}
|
||
})
|
||
}
|
||
}
|